【问题标题】:Execute a command on Remote Machine in Python用 Python 在远程机器上执行命令
【发布时间】:2015-02-09 14:24:02
【问题描述】:

我正在Ubuntu上用python编写一个程序,在RaspberryPi上执行命令ls -l,连接网络。

谁能指导我如何做到这一点?

【问题讨论】:

    标签: python ubuntu terminal tkinter paramiko


    【解决方案1】:

    当然,有几种方法可以做到!

    假设您在 raspberry.lan 主机上有一个 Raspberry Pi,您的用户名是 irfan

    子进程

    它是运行命令的默认 Python 库。
    你可以让它运行 ssh 并在远程服务器上做任何你需要的事情。

    草稿has it covered in his answer。如果您不想使用任何第三方库,则绝对应该这样做。

    您还可以使用pexpect 自动输入密码/密码。

    帕拉米科

    paramiko 是一个添加了 SSH 协议支持的第三方库,因此它可以像 SSH 客户端一样工作。

    将连接到服务器、执行并获取ls -l 命令结果的示例代码如下所示:

    import paramiko
    
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('raspberry.lan', username='irfan', password='my_strong_password')
    
    stdin, stdout, stderr = client.exec_command('ls -l')
    
    for line in stdout:
        print line.strip('\n')
    
    client.close()
    

    面料

    您也可以使用fabric 来实现它。
    Fabric 是一个在远程服务器上执行各种命令的部署工具。

    它通常用于在远程服务器上运行东西,因此您可以轻松放置最新版本的 Web 应用程序、重新启动 Web 服务器等等,只需一个命令。实际上,您可以在多个服务器上运行相同的命令,这太棒了!

    虽然它是作为一个部署和远程管理工具,你仍然可以使用它来执行基本的命令。

    # fabfile.py
    from fabric.api import *
    
    def list_files():
        with cd('/'):  # change the directory to '/'
            result = run('ls -l')  # run a 'ls -l' command
            # you can do something with the result here,
            # though it will still be displayed in fabric itself.
    

    这就像在远程服务器中输入cd /ls -l,因此您将获得根文件夹中的目录列表。

    然后在shell中运行:

    fab list_files
    

    会提示输入服务器地址:

    No hosts found. Please specify (single) host string for connection: irfan@raspberry.lan
    

    快速说明:您还可以在fab 命令中分配用户名和主机权限:

    fab list_files -U irfan -H raspberry.lan
    

    或者您可以将主机放入 fabfile 中的 env.hosts 变量中。 Here's how to do it.


    然后会提示您输入 SSH 密码:

    [irfan@raspberry.lan] run: ls -l
    [irfan@raspberry.lan] Login password for 'irfan':
    

    然后命令就会成功运行。

    [irfan@raspberry.lan] out: total 84
    [irfan@raspberry.lan] out: drwxr-xr-x   2 root root  4096 Feb  9 05:54 bin
    [irfan@raspberry.lan] out: drwxr-xr-x   3 root root  4096 Dec 19 08:19 boot
    ...
    

    【讨论】:

    • @IrfanGhaffar7 您可以使用pipeasy_install 安装第三方Python 库。所以应该是pip install fabric。检查文档(我链接到 fabric 和 paramiko 文档),它有快速入门和教程!
    • 我的远程机器主机名是Pi@192.168.2.34,密码是raspberrypi。我如何在 Fabric 上提及这些信息
    • @Fahadkalis 当它提示您输入主机名时,您可以将Pi@192.168.2.34 放入其中。此外,您可以像这样运行fab 命令:fab list_files -u Pi -H 192.168.2.34
    • @IrfanGhaffar7 和 paramiko 您可以运行实际的 python 脚本本身。
    • @IrfanGhaffar7 如果你使用 paramiko,你应该这样连接:client.connect('192.168.2.40', username='pi', password='raspberry')
    【解决方案2】:

    来自here的简单示例:

    import subprocess
    import sys
    
    HOST="www.example.org"
    # Ports are handled in ~/.ssh/config since we use OpenSSH
    COMMAND="uname -a"
    
    ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
                           shell=False,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)
    result = ssh.stdout.readlines()
    if result == []:
        error = ssh.stderr.readlines()
        print >>sys.stderr, "ERROR: %s" % error
    else:
        print result
    

    它完全符合您的要求:通过 ssh 连接、执行命令、返回输出。无需第三方库。

    【讨论】:

    • 当我使用这个 COMMAND="ls -l" 时,它的结果是一行,意味着不可读但它正在工作
    • @IrfanGhaffar7 这是因为result 是一个列表,而不是一个字符串。你可以改为print ''.join(result) 让它看起来可读。
    • 如果我们想运行像tailf /some/file/name这样的命令怎么办。在这种情况下,它正在启动该进程,然后将其终止或进入等待状态。但是当 tailf 仍在运行时,我还有一些命令要输入。
    • @manojprashantk - 你可以试试other command &; tail -f /some/file
    【解决方案3】:

    您可以在 linux/ Unix 的内置 ssh 命令中使用以下方法。

       import os
       os.system('ssh username@ip  bash < local_script.sh >> /local/path/output.txt 2>&1')
       os.system('ssh username@ip  python < local_program.py >> /local/path/output.txt 2>&1')
    

    【讨论】:

      【解决方案4】:

      Paramiko 模块可用于通过调用 shell 来运行多个命令。这里我创建了类来调用 ssh shell

      类ShellHandler:

      def __init__(self, host, user, psw):
          logger.debug("Initialising instance of ShellHandler host:{0}".format(host))
          try:
              self.ssh = paramiko.SSHClient()
              self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
              self.ssh.connect(host, username=user, password=psw, port=22)
              self.channel = self.ssh.invoke_shell()
          except:
              logger.error("Error Creating ssh connection to {0}".format(host))
              logger.error("Exiting ShellHandler")
              return
          self.psw=psw
          self.stdin = self.channel.makefile('wb')
          self.stdout = self.channel.makefile('r')
          self.host=host
          time.sleep(2)
      
          while not self.channel.recv_ready():
              time.sleep(2)
          self.initialprompt=""
          while self.channel.recv_ready():
      
              rl, wl, xl = select.select([ self.stdout.channel ], [ ], [ ], 0.0)
              if len(rl) > 0:
                  tmp = self.stdout.channel.recv(24)
                  self.initialprompt=self.initialprompt+str(tmp.decode())
      
      
      
      def __del__(self):
          self.ssh.close()
          logger.info("closed connection to {0}".format(self.host))
      
      def execute(self, cmd):
          cmd = cmd.strip('\n')
          self.stdin.write(cmd + '\n')
          #self.stdin.write(self.psw +'\n')
          self.stdin.flush()
          time.sleep(1)
          while not self.stdout.channel.recv_ready():
              time.sleep(2)
              logger.debug("Waiting for recv_ready")
      
          output=""
          while self.channel.recv_ready():
              rl, wl, xl = select.select([ self.stdout.channel ], [ ], [ ], 0.0)
              if len(rl) > 0:
                  tmp = self.stdout.channel.recv(24)
                  output=output+str(tmp.decode())
          return output
      

      如果每次创建不同的shell对你来说并不重要,那么你可以使用下面的方法。

      def run_cmd(self,cmd):
          try:
              cmd=cmd+'\n'
              #self.ssh.settimeout(60)
              stdin,stdout,stderr=self.ssh.exec_command(cmd)
              while not stdout.channel.eof_received:
                 time.sleep(3)
                 logger.debug("Waiting for eof_received")
              out=""
              while stdout.channel.recv_ready():
                  err=stderr.read()
                  if err:
                      print("Error: ",my_hostname, str(err))
                      return False 
      
                  out=out+stdout.read()
              if out:
                     return out 
      
          except:
              error=sys.exc_info()
              logger.error(error)
              return False 
      

      【讨论】:

        猜你喜欢
        • 2012-07-27
        • 2012-04-06
        • 1970-01-01
        • 2013-06-06
        • 2015-03-19
        • 1970-01-01
        • 2012-11-17
        • 2021-08-08
        • 1970-01-01
        相关资源
        最近更新 更多