使用paramiko中 not found in known_hosts的错误解决
2017-09-30 08:08
417 查看
执行代码如下
$ cat paramiko-demo1.py
#!/usr/bin/env python
import paramiko
hostname='192.168.4.71'
username='username'
password='password'
paramiko.util.log_to_file('syslogin.log')
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(hostname=hostname,username=username,password=password)
stdin,stdout,stderr=ssh.exec_command('free -m')
print stdout.read()
ssh.close
执行报错
$ python paramiko-demo1.py
Traceback (most recent call last):
File "paramiko-demo1.py", line 10, in <module>
ssh.connect(hostname=hostname,username=username,password=password)
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 361, in connect
server_key)
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 648, in missing_host_key
raise SSHException('Server %r not found in known_hosts' % hostname)
paramiko.ssh_exception.SSHException: Server '192.168.4.71' not found in known_hosts
进入python环境,调试代码如下
$ python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
>>> ssh=paramiko.SSHClient()
>>> ssh.connect(hostname='192.168.4.71',username='username',password='password')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 361, in connect
server_key)
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 648, in missing_host_key
raise SSHException('Server %r not found in known_hosts' % hostname)
paramiko.ssh_exception.SSHException: Server '192.168.4.71' not found in known_hosts
>>> ssh.load_system_host_keys()
>>> ssh.connect(hostname='192.168.4.71',username='clusteruser',password='clusteruser6se4')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 361, in connect
server_key)
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 648, in missing_host_key
raise SSHException('Server %r not found in known_hosts' % hostname)
paramiko.ssh_exception.SSHException: Server '192.168.4.71' not found in known_hosts
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect(hostname='192.168.4.71',port=22,username='username',password='password',compress=True)
>>> stdin,stdout,stderr=ssh.exec_command('hostname;uptime')
>>> print stdout.read()
192.168.4.71
21:49:19 up 15 days, 10:27, 0 users, load average: 0.01, 0.03, 0.05
>>> ssh.close()
显然加入
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='192.168.4.71',port=22,username='username',password='password',compress=True)
这两条代码后,完成了功能。
set_missing_host_key_policy方法,是制定连接远程主机没有本地密钥或HostKeys对象是的策略,有三种策略:
1、AutoAddPolicy,自动添加主机名及主机密钥到本地HostKeys对象,并保存,不依赖load_system_host_keys()的配置,即使~/.ssh/known_hosts不存在也不产生影响。
2、WarningPolicy,用于记录一个未知的主机密钥的python警告,并接受它,功能上与AutoAddPolicy相似,但未知主机会有告警
3、RejectPolicy,自动拒绝不知的主机名和密钥,依赖 load_system_host_keys()的配置
$ cat paramiko-demo1.py
#!/usr/bin/env python
import paramiko
hostname='192.168.4.71'
username='username'
password='password'
paramiko.util.log_to_file('syslogin.log')
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(hostname=hostname,username=username,password=password)
stdin,stdout,stderr=ssh.exec_command('free -m')
print stdout.read()
ssh.close
执行报错
$ python paramiko-demo1.py
Traceback (most recent call last):
File "paramiko-demo1.py", line 10, in <module>
ssh.connect(hostname=hostname,username=username,password=password)
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 361, in connect
server_key)
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 648, in missing_host_key
raise SSHException('Server %r not found in known_hosts' % hostname)
paramiko.ssh_exception.SSHException: Server '192.168.4.71' not found in known_hosts
进入python环境,调试代码如下
$ python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
>>> ssh=paramiko.SSHClient()
>>> ssh.connect(hostname='192.168.4.71',username='username',password='password')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 361, in connect
server_key)
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 648, in missing_host_key
raise SSHException('Server %r not found in known_hosts' % hostname)
paramiko.ssh_exception.SSHException: Server '192.168.4.71' not found in known_hosts
>>> ssh.load_system_host_keys()
>>> ssh.connect(hostname='192.168.4.71',username='clusteruser',password='clusteruser6se4')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 361, in connect
server_key)
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 648, in missing_host_key
raise SSHException('Server %r not found in known_hosts' % hostname)
paramiko.ssh_exception.SSHException: Server '192.168.4.71' not found in known_hosts
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect(hostname='192.168.4.71',port=22,username='username',password='password',compress=True)
>>> stdin,stdout,stderr=ssh.exec_command('hostname;uptime')
>>> print stdout.read()
192.168.4.71
21:49:19 up 15 days, 10:27, 0 users, load average: 0.01, 0.03, 0.05
>>> ssh.close()
显然加入
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='192.168.4.71',port=22,username='username',password='password',compress=True)
这两条代码后,完成了功能。
set_missing_host_key_policy方法,是制定连接远程主机没有本地密钥或HostKeys对象是的策略,有三种策略:
1、AutoAddPolicy,自动添加主机名及主机密钥到本地HostKeys对象,并保存,不依赖load_system_host_keys()的配置,即使~/.ssh/known_hosts不存在也不产生影响。
2、WarningPolicy,用于记录一个未知的主机密钥的python警告,并接受它,功能上与AutoAddPolicy相似,但未知主机会有告警
3、RejectPolicy,自动拒绝不知的主机名和密钥,依赖 load_system_host_keys()的配置
相关文章推荐
- 使用paramiko中 Server not found in known_hosts的错误解决
- 解决Linux下部署使用JasperReport的项目的错误:net.sf.jasperreports.engine.util.JRFontNotFoundException: Font '宋体' i
- git 出现The current branch is not configured for pull No value for key branch.master.merge found in configuration错误的解决办法
- 解决node-ffi在Windows XP上使用出现“Error: The specified procedure could not be found”的错误
- OpenCV4Android SDK sample运行中ndk-build not found in PATH的错误解决
- Unity3D使用Animator报:"Animator.GotoState: State could not be found invalid layer index"错误解决方
- 使用 EasyBCD 安装Ubuntu 14.04 Error 15: file not found错误的解决方法
- Linux deepin系统下配置nginx+php-fpm+PHPstorm并解决403/404/502/file not found错误
- 使用Spring提供的Open Session In View而引起Write operations are not allowed in read-only mode (FlushMode.NEVER) 错误解决
- python 报 python 2.7 is required ,which is not found in registry 的错误的解决办法
- Nginx 提示host not found in upstream 错误解决方法
- 在使用AndroidStudio打包apk时,遇到“xxx is not translated in xxx”错误的解决办法
- php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解决方法
- [解决]在安装Python PIL 时出现错误: Python version 2.7 required, which was not found in the registry
- 在使用AndroidStudio打包apk时,遇到“xxx is not translated in xxx”错误的解决办法
- Webpack错误解决(一):ERROR in Entry module not found
- 使用tar解压文件到指定目录时出现Not found in archive的解决方法
- php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解决方法
- 服务器部署_nginx的host not found in upstream "tomcat_www.bojinne" 错误解决办法
- web.xml配置错误:java.lang.ClassNotFoundException: com.shiro.web.InitServlet解决!~