下面是我正在调试的代码过程的一部分。该脚本使用子进程模块来调用 oracle sqlplus。我在 uid 之前设置了 gid,但 sqlplus 仍然以用户 (oracle) 甚至不关联的组 id 启动。
def demote(self, user_gid, user_uid):
def result():
os.setgid(user_gid)
os.setuid(user_uid)
a= os.getgid()
b =os.getuid()
print (a, b)
print(os.getgroups())
return result
def run_sqlplus(self):
pw_record = pwd.getpwnam('oracle')
user_gid = pw_record.pw_gid
user_uid = pw_record.pw_uid
print(user_gid)
print(user_uid)
p = subprocess.Popen(["sqlplus", "/ as sysdba"], preexec_fn=CONFIG.demote(user_gid, user_uid),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env={'ORACLE_SID': '{0}'.format(vinstance_name), 'ORACLE_HOME': '{0}'.format(ORACLE_HOME),
'PATH': '{0}/bin'.format(ORACLE_HOME)})
(stdout, stderr) = p.communicate(sqlplus_script.encode('utf-8'))
stdout_lines = stdout.decode('utf-8').split("\n")
return stdout_lines
sqlplus_script = """
STARTUP NOMOUNT;
exit;
任何解决方案都将受到高度赞赏。
麦克风
答案1
我最终对脚本进行了如下修改,并且效果很好:
p = subprocess.Popen(["sqlplus", "/ as sysdba", "@{0}/admin/{1}/Scripts/Create.sql".format(ORACLE_BASE,vdb_unique_name)], preexec_fn=CONFIG.demote(user_gid, user_uid), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env={'ORACLE_SID': '{0}'.format(vinstance_name), 'ORACLE_HOME': '{0}'.format(ORACLE_HOME), 'PATH': '{0}/bin'.format(ORACLE_HOME)})