【发布时间】:2014-09-29 04:57:33
【问题描述】:
由于项目限制,我必须使用 SUBPROCESS 进行 SSH '因为无法从 python 3.1 更改(不幸的是,它几乎不支持 ssh 库)。
我已经在通过 Windows 运行的本地和远程机器上安装了 openSSH。
问题:我可以尝试 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"))
有什么线索吗?
【问题讨论】:
-
我会推荐 pexpect 用于此类场景
-
@Chirag 为什么不只是
ssh remote_address remote_cmd并添加一个 SSH 密钥对? -
谢谢 gmfreak,我正在尝试 pexpect。
-
@Salem:对密码有要求。
-
如果适用于 Linix 而不是 Windows,则显示为 p-expect。
标签: python ssh subprocess popen