【发布时间】:2020-04-14 16:12:29
【问题描述】:
因此,简而言之,我创建了一个脚本(名为 main.py),其中有一段时间我在文件中写入内容。它运作良好。但是,该文件的权限必须是 rwxrwxrw-,因此任何人都可以修改服务器上的文件。那不是我想要的。于是我把权限改成了 rwxrwxr-- 然后我改了 main.py 的代码:
#!/usr/bin/python
import subprocess
text = "I want this text to appear in my file"
command = subprocess.Popen(["python", "modificateFile.py", str('"')+text+str('"')], stderr=subprocess.PIPE, stdout=subprocess.PIPE) #I run another file that will do the task
for i in command.stderr:
print(i.decode("utf-8")) #check if there is any error
modificateFile.py 的代码
#!/usr/bin/python
import platform
import os
import sys
UID = 1080 #my UID.. I don't really know if it's the right way to program this
if __name__ == "__main__":
system = platform.system()
if system == "Linux": #ok it may be useless
os.setuid(UID) # /!\ I THINK THAT'S WHERE THE PROBLEM IS /!\
if len(sys.argv) > 1:
with open("file.txt", "w", encoding="utf-8") as f:
f.write(sys.argv[1])
else:
sys.stderr.write("not enough parameters to work") #didn't know which error I could raise..
#so as I already imported sys, I used this function
exit(-1)
else:
sys.stderr.write("wrong OS : program only work on linux")
exit(-1)
当我创建这个时,老实说,我真的不知道自己在做什么……我正在学习编程。 错误信息:
Traceback(最近一次调用最后一次):文件“modificateFile.py”,第 11 行, 在 os.setuid(UID) PermissionError: [Errno 1] Operation not allowed
我听说过 SUID.. 但我没有 root 权限。
有人可以解释什么是错的,我能做些什么吗? (如果您需要更多元素,请告诉我)
【问题讨论】:
标签: python python-3.x linux permissions