打开一个腻子窗口并运行 ssh 命令 - Python

新手上路,请多包涵

我是 python 的新手。我需要每天登录服务器(桌面 -> 1.32 -> 0.20 -> 3.26)。为此,我需要打开 putty 并使用我正在登录的 ssh 连接。为此,我想使用 python 编写一个脚本。

通过使用谷歌,我认为 subprocess.Popen 会做到这一点。但它不能正常工作。

第一条路线:

 import subprocess
pid = subprocess.Popen("putty.exe user@xxx.xx.x.32 -pw password").pid

它工作正常(打开窗口登录到 .32)。但无法提供意见。我开始知道要为同一过程提供输入,我们需要使用管道。

第二条路线:

 from subprocess import Popen, PIPE, STDOUT
p = Popen("putty.exe user@xxx.xx.x.32 -pw password", stdout=PIPE, stdin=PIPE, stderr=STDOUT)
grep_stdout = p.communicate(input=b'ssh xx.xx.x.20\n')[0]
print(grep_stdout.decode())

通过使用它我也无法登录第一台服务器。登录到所有服务器后,我需要终端处于活动状态。这该怎么做???

编辑

我需要在新的腻子窗口中执行此操作。登录后不要关闭窗口。我有一些体力活要做。

原文由 Swapna murahari 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 473
1 个回答

使用 powershell 调用 putty 以打开一个新窗口

from subprocess import Popen
Popen("powershell putty.exe user@host -pw mypassword")

原文由 Mark 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题