使用subprocess.popen进行SSH时无法输入密码
我必须使用SUBPROCESS来进行SSH,因为我不能更改Python版本(我现在用的是3.1,这个版本几乎不支持SSH库),这是项目的限制。
我已经在本地和远程机器上安装了openSSH,都是在Windows系统上运行。
问题是:我可以尝试进行SSH连接,但无法输入密码和后续的命令,比如dir,也无法获取结果。
proc = subprocess.Popen("ssh -t -t remote_ip_here\n", shell = False, stdin = subprocess.PIPE, stdout=subprocess.PIPE)
#Entering password
proc.stdin.write(bytes("abc123\n","utf-8"))
#entering command
self.text , self.error = proc.communicate() // here I tried readlines() but it didnt returned any response.
print(self.text.decode("utf-8"))
proc.stdin.write(bytes("dir\n","utf-8"))
有什么线索吗?
1 个回答
2
如果你想在Windows上使用,可以看看这个叫做pexpect的工具的一个版本:https://bitbucket.org/geertj/winpexpect/wiki/Home