Skip to main content
 首页 » 编程设计

python之SSH 使用 subprocess.popen,无法输入密码

2024年10月25日8xxx_UU

<分区>

由于项目限制,我必须为 SSH 使用 SUBPROCESS,因为不能从 python 3.1 更改(不幸的是,它几乎不支持 ssh 库)。

我已经在运行在 Windows 上的本地和远程机器上安装了 openSSH。

问题:我可以尝试 ssh 但无法输入密码和后续命令,例如目录和获取结果。

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")) 

有什么线索吗?