![Python os.setgid 选择了错误的组 ID](https://linux22.com/image/1597393/Python%20os.setgid%20%E9%80%89%E6%8B%A9%E4%BA%86%E9%94%99%E8%AF%AF%E7%9A%84%E7%BB%84%20ID.png)
下面是我正在调试的代码过程的一部分。该脚本使用子进程模块来调用 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)})