SSH - 带有 paramiko 问题的 Python [英] SSH - Python with paramiko issue

查看:59
本文介绍了SSH - 带有 paramiko 问题的 Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使其正常工作,但不断收到相同的错误.我试过主机的fqdn和ip.我试图通过凭据和不通过它.我查看了错误消息中指出的行.搜索了谷歌,但无法弄清楚为什么这不起作用:

I've been trying to get this to work but keep getting the same errors. I've tried the fqdn and ip of the host. I've tried to pass it with credentials and without. I've looked at the lines indicated in the error message. Searched google, but cannot figure out why this is not working:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('host', username='loginname')
stdin, stdout, stderr = ssh.exec_command("pwd")
stdout.readlines()

错误:

Traceback (most recent call last):
  File "audit.py", line 7, in <module>
    ssh.connect('host', username='loginname')
  File "/usr/lib/python2.6/site-packages/paramiko/client.py", line 338, in connect
    self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)
  File "/usr/lib/python2.6/site-packages/paramiko/client.py", line 520, in _auth
    raise SSHException('No authentication methods available')

  • 我能够通过 ssh 毫无问题地连接到主机.
  • ssh 版本:OpenSSH_5.3p1、OpenSSL 1.0.0-fips 2010 年 3 月 29 日
  • 注意:我正在尝试创建一种在多个远程服务器上运行一系列命令的方法.我正在使用 sys import argv 运行诸如 python audit.py host1 host2 host3 之类的脚本,然后该脚本将运行这些特定主机的审计.我已经创建了一个 bash 脚本来完成此任务,但我想要一种通过 Python 实现的更好方法.
    • I am able to connect to the host with no issue via ssh.
    • ssh version: OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
    • To note: I'm trying to create a way of running a series of commands on several remote servers. I'm using sys import argv to run the script such as python audit.py host1 host2 host3, and then the script will run through the audit for those particular hosts. I've already created a bash script that accomplishes this but I wanted a better way of doing it via Python.
    • 推荐答案

      您应该提供密码私钥(或两者),否则SSH客户端不知道如何使用登录数据进行身份验证.

      You should provide either a password or a private key (or both), otherwise the SSH client does not know how to authenticate with the login data.

      这是我的代码示例,供您参考.

      Here is my code example for your reference.

      #!/usr/bin/python
      
      from StringIO import StringIO
      import paramiko 
      
      class SshClient:
          "A wrapper of paramiko.SSHClient"
          TIMEOUT = 4
      
          def __init__(self, host, port, username, password, key=None, passphrase=None):
              self.username = username
              self.password = password
              self.client = paramiko.SSHClient()
              self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
              if key is not None:
                  key = paramiko.RSAKey.from_private_key(StringIO(key), password=passphrase)
              self.client.connect(host, port, username=username, password=password, pkey=key, timeout=self.TIMEOUT)
      
          def close(self):
              if self.client is not None:
                  self.client.close()
                  self.client = None
      
          def execute(self, command, sudo=False):
              feed_password = False
              if sudo and self.username != "root":
                  command = "sudo -S -p '' %s" % command
                  feed_password = self.password is not None and len(self.password) > 0
              stdin, stdout, stderr = self.client.exec_command(command)
              if feed_password:
                  stdin.write(self.password + "\n")
                  stdin.flush()
              return {'out': stdout.readlines(), 
                      'err': stderr.readlines(),
                      'retval': stdout.channel.recv_exit_status()}
      
      if __name__ == "__main__":
          client = SshClient(host='host', port=22, username='username', password='password') 
          try:
             ret = client.execute('dmesg', sudo=True)
             print "  ".join(ret["out"]), "  E ".join(ret["err"]), ret["retval"]
          finally:
            client.close() 
      

      这篇关于SSH - 带有 paramiko 问题的 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆