python paramiko使用
生活随笔
收集整理的這篇文章主要介紹了
python paramiko使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
paramiko 安裝
| 1 2 | yum?install?python-paramiko?-y yum?install?openssh-server?openssh-clients?-y |
報錯
| 1 2 3 4 5 6 7 8 | Traceback?(most?recent?call?last): ??File?"ssh.py",?line?10,?in?<module> ????ssh.connect(hostname=hostname,port=port,username=username,password=password) ??File?"/usr/lib/python2.6/site-packages/paramiko/client.py",?line?306,?in?connect ????self._policy.missing_host_key(self,?hostname,?server_key) ??File?"/usr/lib/python2.6/site-packages/paramiko/client.py",?line?83,?in?missing_host_key ????raise?SSHException('Unknown?server?%s'?%?hostname) paramiko.SSHException:?Unknown?server?192.168.2.11 |
解決方法一
1、創(chuàng)建 ~/.ssh/known_hosts
2、遠程連接一次
eg
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/usr/bin/env?python import?paramiko hostname='192.168.2.11' username='root' password='oracle' port=22 ?? ssh=paramiko.SSHClient() ssh.load_system_host_keys() ssh.connect(hostname=hostname,port=port,username=username,password=password) stdin,stdout,stderr=ssh.exec_command('ls?/') print?stdout.read() ssh.close() |
解決方法二
將ssh.load_system_host_keys()替換為set_missing_host_key_policy(paramiko.AutoAddPolicy())
eg
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #!/usr/bin/env?python import?paramiko hostname='10.13.106.36' port=22 username='root' password='centos' if?__name__=='__main__': ????????paramiko.util.log_to_file('paramiko.log') ????????s=paramiko.SSHClient() ????????s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ????????s.connect(hostname,port,username,password) ????????stdin,stdout,stderr=s.exec_command('ifconfig') ????????print?stdout.read() ????????s.close() |
參考博文:http://emrys411876027.qj67.wshost.cc/python/69.html
? ? ?本文轉(zhuǎn)自1321385590 51CTO博客,原文鏈接:http://blog.51cto.com/linux10000/1771943,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的python paramiko使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: List集合分组
- 下一篇: websocket python爬虫_p