【问题标题】:What is the simplest way to SSH using Python?使用 Python 进行 SSH 的最简单方法是什么?
【发布时间】:2010-11-17 01:07:28
【问题描述】:

如何从本地 Python (3.0) 脚本简单地通过 SSH 连接到远程服务器、提供登录名/密码、执行命令并将输出打印到 Python 控制台?

我宁愿不使用任何大型外部库或在远程服务器上安装任何东西。

【问题讨论】:

    标签: python linux unix ssh


    【解决方案1】:

    您可以按照上面的建议使用 Paramiko 自己编写代码。或者,您可以查看 Fabric,这是一个 Python 应用程序,可以完成您所询问的所有事情:

    Fabric 是一个 Python 库,并且 命令行工具旨在 简化部署应用程序或 执行系统管理任务 通过 SSH 协议。它提供 用于运行任意 shell 的工具 命令(作为正常登录 用户,或通过 sudo),上传和 下载文件等。

    我认为这符合您的需求。它也不是一个大型库,不需要安装服务器,尽管它确实依赖于 paramiko 和 pycrypt,需要在客户端安装。

    该应用以前是here。现在可以找到here

    * The official, canonical repository is git.fabfile.org
    * The official Github mirror is GitHub/bitprophet/fabric
    

    有几篇关于它的好文章,但你应该小心,因为它在过去六个月内发生了变化:

    Deploying Django with Fabric

    Tools of the Modern Python Hacker: Virtualenv, Fabric and Pip

    Simple & Easy Deployment with Fabric and Virtualenv


    后来:Fabric 不再需要安装 paramiko:

    $ pip install fabric
    Downloading/unpacking fabric
      Downloading Fabric-1.4.2.tar.gz (182Kb): 182Kb downloaded
      Running setup.py egg_info for package fabric
        warning: no previously-included files matching '*' found under directory 'docs/_build'
        warning: no files found matching 'fabfile.py'
    Downloading/unpacking ssh>=1.7.14 (from fabric)
      Downloading ssh-1.7.14.tar.gz (794Kb): 794Kb downloaded
      Running setup.py egg_info for package ssh
    Downloading/unpacking pycrypto>=2.1,!=2.4 (from ssh>=1.7.14->fabric)
      Downloading pycrypto-2.6.tar.gz (443Kb): 443Kb downloaded
      Running setup.py egg_info for package pycrypto
    Installing collected packages: fabric, ssh, pycrypto
      Running setup.py install for fabric
        warning: no previously-included files matching '*' found under directory 'docs/_build'
        warning: no files found matching 'fabfile.py'
        Installing fab script to /home/hbrown/.virtualenvs/fabric-test/bin
      Running setup.py install for ssh
      Running setup.py install for pycrypto
    ...
    Successfully installed fabric ssh pycrypto
    Cleaning up...
    

    这主要是装饰性的,但是:ssh 是 paramiko 的一个分支,两个库的维护者是相同的(Jeff Forcier,也是 Fabric 的作者)和 the maintainer has plans to reunite paramiko and ssh under the name paramiko。 (此更正来自pbanka。)

    【讨论】:

    • 由于这似乎是一个有趣的链接,我想更新它,因为您的链接现在已损坏。请使用:clemesha.org/blog/…
    • 提问者没有说明他不想使用“大型外部库”吗?当作者真正要求的只是一个简单的一次性 ssh 配方时,Paramiko 和 Fabric 都太过分了。
    • @Zoran Pavlovic:所有答案都是安装本地包(paramiko、fabric、ssh、libssh2)或使用子进程运行 ssh。后者是一种免安装解决方案,但我认为生成 ssh 不是一个好主意,OP 也没有,因为他选择了安装 ssh 模块的答案。那些文档说:“ssh.py 提供了三种常见的 SSH 操作,get、put 和 execute。它是对 Paramiko 的高级抽象。”因此,除非您偏爱 libssh2,它对编码的要求很高,否则没有符合标准的建议。当无法合理满足 OP 的条件时,我倾向于给出一个好的解决方案。
    【解决方案2】:

    我还没有尝试过,但是这个pysftp 模块可能会有所帮助,它又使用了 paramiko。我相信一切都是客户端。

    有趣的命令可能是.execute(),它在远程机器上执行任意命令。 (该模块还具有 .get().put 方法,更多地暗示了它的 FTP 字符)。

    更新:

    在我最初链接到的博客文章不再可用后,我重新编写了答案。一些引用此答案旧版本的 cmets 现在看起来很奇怪。

    【讨论】:

    • 好发现!只要您不关心自定义错误响应,这种额外的抽象就会非常有用。
    • ssh 模块成功了。简单且工作正常。无需通过 Paramiko API 进行搜索。
    • 您提供的链接中的 ssh.py 文件的链接已损坏:/
    • 是的,请给我们一个新链接。我在 github 上找到了 ssh.py,但是不一样(也不是那么好)
    • pysftp 包只提供 SFTP。远非 SSH 客户端。
    【解决方案3】:

    如果你想避免任何额外的模块,你可以使用子进程模块来运行

    ssh [host] [command]
    

    并捕获输出。

    尝试类似:

    process = subprocess.Popen("ssh example.com ls", shell=True,
        stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output,stderr = process.communicate()
    status = process.poll()
    print output
    

    处理用户名和密码,可以使用子进程与ssh进程交互,也可以在服务器上安装公钥,避免密码提示。

    【讨论】:

    • 但是如果客户端在 Windows 上呢?
    • 通过管道向ssh 子进程提供密码可能很困难。见Why not just use a pipe (popen())?。您可能需要ptypexpect 模块来解决它。
    • 似乎不适用于字符串 'ssh somecomputer; python -c "import numpy; print numpy.__version__"' 它说它不知道命令“import”
    • @usethedeathstar:将整个远程命令用引号括起来:ssh somecomputer 'python -c "import this; print this"'
    【解决方案4】:

    我写了Python bindings for libssh2。 Libssh2 是一个实现 SSH2 协议的客户端库。

    import socket
    import libssh2
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(('exmaple.com', 22))
    
    session = libssh2.Session()
    session.startup(sock)
    session.userauth_password('john', '******')
    
    channel = session.channel()
    channel.execute('ls -l')
    
    print channel.read(1024)
    

    【讨论】:

    • 好像很低级。例如(您自己的示例),您必须明确表示您使用 IPv4 或 IPv6(您不必与 OpenSSH 命令行客户端相关)。另外,我没有找到如何使它与 ssh-agent 一起工作。
    • pylibssh2 的好处是它传输文件的速度比任何本地 python 实现的 ssh (如 paramiko)都要快。
    【解决方案5】:

    您对“最简单”的定义在这里很重要 - 简单的代码意味着使用模块(尽管“大型外部库”有点夸张)。

    我相信最新(积极开发)的模块是paramiko。它在下载中附带演示脚本,并具有详细的在线 API 文档。您也可以尝试PxSSH,它包含在pexpect 中。在第一个链接中有一个简短的示例以及文档。

    关于简单性,请注意,良好的错误检测总是会使您的代码看起来更复杂,但您应该能够重用示例脚本中的大量代码,然后忘记它。

    【讨论】:

      【解决方案6】:

      喜欢hughdbrown,我喜欢Fabric。请注意,虽然它实现了自己的声明性脚本(用于进行部署等),但它也可以作为 Python 模块导入并在您的程序中使用,而无需编写 Fabric 脚本。

      Fabric 有一个新的维护者,正在重写;这意味着您(当前)在网络上找到的大多数教程都不适用于当前版本。此外,Google 仍将旧的 Fabric 页面显示为第一个结果。

      有关最新文档,您可以查看:http://docs.fabfile.org

      【讨论】:

      【解决方案7】:

      我发现 paramiko 有点太低级了,而且 Fabric 并不是特别适合用作库,所以我将自己的库放在一起,名为 spur,它使用 paramiko 实现了一个更好的接口:

      import spur
      
      shell = spur.SshShell(hostname="localhost", username="bob", password="password1")
      result = shell.run(["echo", "-n", "hello"])
      print result.output # prints hello
      

      您还可以选择在程序运行时打印程序的输出,如果您想在程序退出之前查看长时间运行的命令的输出,这很有用:

      result = shell.run(["echo", "-n", "hello"], stdout=sys.stdout)
      

      【讨论】:

      • 不支持运行非标准命令,例如在某些路由器 (MikroTik) 上,命令以 '/' 为前缀,此库会引发错误。不过对于标准的 linux 主机来说,这似乎还不错。
      • 当我将 IP 地址传递给主机名时,它会抛出一个错误,指出在 known_hosts 中找不到 IP...
      • @rexbelia 这是 SSH 的正常行为:为了确保您正在与正确的服务器通信,SSH 只会接受来自主机的密钥(如果它是已知的)。解决方案是将相关密钥添加到 known_hosts,或者将 missing_host_key 参数设置为适当的值,如文档中所述。
      【解决方案8】:

      为了那些到达这里谷歌搜索 python ssh 示例的人的利益。 原来的问题和答案现在几乎是旧的解码了。 似乎 paramiko 获得了一些功能(好吧。我承认 - 这里纯粹是猜测 - 我是 Python 新手),您可以直接使用 paramiko 创建 ssh 客户端。

      import base64
      import paramiko
      
      client = paramiko.SSHClient()
      
      client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      client.connect('192.168.1.1', username='user', password='password')
      stdin, stdout, stderr = client.exec_command('cat /proc/meminfo')
      for line in stdout:
          print('... ' + line.strip('\n'))
      client.close()
      

      此代码改编自 https://github.com/paramiko/paramiko 的演示 它对我有用。

      【讨论】:

        【解决方案9】:

        这对我有用

        import subprocess
        import sys
        HOST="IP"
        COMMAND="ifconfig"
        
        def passwordless_ssh(HOST):
                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
                        return "error"
                else:
                        return result
        

        【讨论】:

        • 这是最好的解决方案。为我工作。
        【解决方案10】:

        please refer to paramiko.org, its very useful while doing ssh using python.

        import paramiko
        
        import time
        
        ssh = paramiko.SSHClient() #SSHClient() is the paramiko object</n>
        
        #Below lines adds the server key automatically to know_hosts file.use anyone one of the below
        
        ssh.load_system_host_keys() 
        
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        
        try:
        
        #Here we are actually connecting to the server.
        
        ssh.connect('10.106.104.24', port=22, username='admin', password='')
        
        time.sleep(5)
        
        #I have mentioned time because some servers or endpoint prints there own information after 
        #loggin in e.g. the version, model and uptime information, so its better to give some time 
        #before executing the command.
        
        #Here we execute the command, stdin for input, stdout for output, stderr for error
        
        stdin, stdout, stderr = ssh.exec_command('xstatus Time')
        
        #Here we are reading the lines from output.
        
        output = stdout.readlines() 
        
        print(output)
        
        
        #Below all are the Exception handled by paramiko while ssh. Refer to paramiko.org for more information about exception.
        
        
        except (BadHostKeyException, AuthenticationException,  
            SSHException, socket.error) as e:           
        
        print(e)
        

        【讨论】:

          【解决方案11】:

          看看spurplus,它是我们为管理远程机器和执行文件操作而开发的spurparamiko 的封装。

          Spurplus 提供了一个开箱即用的check_output() 函数:

          import spurplus
          with spurplus.connect_with_retries(
                  hostname='some-machine.example.com', username='devop') as shell:
               out = shell.check_output(['/path/to/the/command', '--some_argument']) 
               print(out)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-02-05
            • 1970-01-01
            • 2020-12-29
            • 1970-01-01
            • 1970-01-01
            • 2010-12-07
            • 1970-01-01
            相关资源
            最近更新 更多