python - SSH - 带有 paramiko 问题的 Python

标签 python ssh paramiko

我一直在努力让它工作,但总是出现同样的错误。我试过主机的 fqdn 和 ip。我尝试过使用凭据和不使用凭据来传递它。我查看了错误消息中指示的行。搜索谷歌,但无法弄清楚为什么这不起作用:

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 完成此操作的更好方法。

最佳答案

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

这是我的代码示例,供您引用。

#!/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() 

关于python - SSH - 带有 paramiko 问题的 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14326665/

相关文章:

python - 通过 Xampp 在 Windows 上用于 Python 的 MySQL

python - 在 Python 中访问非全局变量

ssh - gitolite 如何将远程用户映射到存储库用户?

git - Git .ssh 的默认路径是什么?

Python - 刺激 ssh 命令搞乱文件路径

python - Pytest:根据假设生成总和为 1 的 float 列表

Python:model.fit() 错误,不支持 None 值

ubuntu - "Write Failed: Broken Pipe"尝试通过特定用户通过 ssh 登录时

python - 从 cyptography.hazmat.bindings._constant_time 导入库导入错误

Python paramiko/sshtunnel 代码在 Linux 下工作正常,但在 Windows 下失败