【问题标题】:SSH using subprocess.popen, unable to enter password [duplicate]SSH使用subprocess.popen,无法输入密码[重复]
【发布时间】: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


【解决方案1】:

Sending a password over SSH or SCP with subprocess.Popen

如果你想使用 windows,你应该查看一个 pexpect 端口:https://bitbucket.org/geertj/winpexpect/wiki/Home

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2013-02-16
    • 2014-05-19
    • 1970-01-01
    • 2012-07-28
    • 2012-08-25
    相关资源
    最近更新 更多