【问题标题】:open a putty window and run ssh commands - Python打开一个腻子窗口并运行 ssh 命令 - Python
【发布时间】:2021-02-15 15:12:43
【问题描述】:

我是 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())

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

编辑

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

【问题讨论】:

    标签: python python-2.7 ssh


    【解决方案1】:

    使用powershell调用putty打开新窗口

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

    【讨论】:

      【解决方案2】:

      使用 paramiko 库 python 使用 -

      建立 SSH 连接
      ssh = paramiko.SSHClient()
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh.connect(hostname,username, password)
      

      使用 - 检查连接是否处于活动状态

      status =  ssh.get_transport().is_active()
      #returns True if connection is alive/active
      

      ssh.exec_command() 基本上是一个会话。使用 exec_command(command1;command2) 在一个会话中执行多个命令

      此外,您可以使用它在单个会话中执行多个命令

      channel = ssh.invoke_shell()
      stdin = channel.makefile('wb')
      stdout = channel.makefile('rb')
      stdin.write('''
        Command 1
        Command 2
        ''')
      
      print stdout.read()
      

      【讨论】:

      • 我需要弹出一个新窗口(putty)并且必须登录。在所有服务器(桌面 -> 1.32 -> 0.20 -> 3.26)登录完成后,不要关闭腻子窗口(我有一些手动工作要检查)。
      【解决方案3】:

      python 有一个 SSHv2 协议实现:http://www.paramiko.org/。您可以使用 pip 轻松安装它:

      pip install paramiko
      

      然后你可以创建 ssh 客户端,连接到你的主机并执行命令:

      import paramiko
      ssh_client = paramiko.SSHClient()
      ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh_client.connect('hostname', username='login', password='pwd')
      stdin, stdout, stderr = ssh_client.exec_command('command')
      

      【讨论】:

        【解决方案4】:

        我在 Windows 上创建了一个 bat 文件,它引用了 putty 和 putty 会话特定的信息。这个bat文件可以在windows上自行运行。要从 python 调用,我使用了 subprocess.run() -- python 3.5+。

        名为 putty.bat 的 bat 文件示例:

        start c:\app\PuTTy\putty.exe -load 192.168.1.230-node1-logs -l <logon user> -pw <logon user password for putty session>

        分解bat文件:

        1. 它以窗口的命令“start”开始。
        2. c:\app\PuTTy\putty.exe --> 是 Windows 上包含 putty.exe 的 putty 目录。
        3. -load --> 告诉 putty 加载一个 putty 配置文件。配置文件是您在 putty 客户端上“已保存的会话”下看到的名称。
        4. 192.168.1.230-node1-logs --> 我的 putty 会话特定配置文件。
        5. -l 用于登录 --> 后跟 putty 登录用户。
        6. -pw 是登录密码 --> 后跟 putty 登录密码。 “putty.bat”的内容到此结束。

        在 python 中,使用 subprocess.run() 命令。

        例子:

        import subprocess
        ...
        ...
                        try:
        
                            process = subprocess.run(["putty.bat"], check=True, stdout=subprocess.PIPE, universal_newlines=True)
        
                            print(process.stdout)
        
                        except Exception as e:
        
                            print("subprocess call error in open putty command")
                            
                            print(str(e))
        

        希望对你有帮助

        【讨论】:

          猜你喜欢
          • 2013-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-26
          相关资源
          最近更新 更多