无法使用 paramiko 连接到远程主机?

Unable to connect to remote host using paramiko?

我想使用 scp 在两个 Ubuntu 服务器之间传输文件,我已经在两个系统之间测试了 scp,它运行良好 fine.So 我不想每次都需要执行命令文件,所以我想写一个 python 脚本,它使用 scp 自动从其他主机下载文件。

在网上搜索时,我发现了这个 Paramiko 模块,我在安装这个模块时遇到了问题,我已经使用模块 cryptography 纠正了这个问题。现在,真正的问题在下面的终端中得到了解释。

>>> from paramiko import SSHClient
>>> from scp import SCPClient
>>> ssh = SSHClient()
>>> ssh
<paramiko.client.SSHClient object at 0x1a41c90>
>>> ssh.load_system_host_keys()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
>>> ssh.connect('somename@192.168.100.100')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 296, in c                                                                                    onnect
    to_try = list(self._families_and_addresses(hostname, port))
  File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 200, in _                                                                                    families_and_addresses
    addrinfos = socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_S                                                                                    TREAM)
socket.gaierror: [Errno -2] Name or service not known
>>> ssh.connect('192.168.100.100')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 361, in c                                                                                    onnect
    server_key)
  File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 672, in m                                                                                    issing_host_key
    raise SSHException('Server %r not found in known_hosts' % hostname)
paramiko.ssh_exception.SSHException: Server '192.168.100.100' not found in known_hos                                                                                    ts

为了安全使用,我已经更改了 ip 和用户名 somename is replaced,但我已经尝试使用 original username。所以我尝试了几次,但仍然出现相同的错误。

关于这个问题有什么建议吗?请帮忙。

也许你错过了 missing_host_key_policy

这个怎么样:

proxy = None
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host['hostname'], username=host['user'], sock=proxy)

此处有更多示例:www.programcreek.com

试试这个:

ssh.connect('host', username='username',password='password')

如果您想跳过密码并在不提供密码的情况下连接,您还可以将 public 密钥添加到服务器中的已知主机。 在那种情况下使用:

ssh.connect('host', username='username')

对我来说,解决方案是:

client = SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect(host, username=user,password=password)