• 不加sleep(0.5)会出现SSHException: Invalid requirement, parse error at " '' "问题,原因暂时未知。

  • 结论如下

  1. 如果不使用多线程,则不会出现问题
  2. 使用多线程一定要sleep(0.2),也就是多个线程之间一定要有时间间隙
#!/usr/bin/env python
import paramiko
import threading
import sys
import time def ssh(host, user, cmd):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
pkey_file = '/Users/admin/.ssh/id_rsa'
# key = paramiko.RSAKey.from_private_key_file( pkey_file, password='' )
key = paramiko.RSAKey.from_private_key_file( pkey_file, password='' )
try:
ssh.connect( hostname=host, username=user, pkey=key, timeout=5 )
except:
print 'connection has some problems...'
return -1
stdin, stdout, stderr = ssh.exec_command( cmd )
stdout = stdout.read()
stderr = stderr.read()
if stdout:
print '%s: %s' % (host, stdout),
ssh.close()
else:
print '%s: %s' % (host, stderr),
ssh.close()
if __name__ == '__main__':
hosts = ['192.168.31.114', '192.168.31.115', '192.168.31.116', '192.168.31.117']
try:
cmd = sys.argv[1]
except IndexError:
print '%s follow a command must be' % __file__
sys.exit(-1)
for host in hosts:
t = threading.Thread( target=ssh, args=(host, 'root', 'uptime') )
t.start()
time.sleep(0.5) #如果不加此行则会出现下面描述的异常
# ssh(host, 'root', 'uptime')

执行结果如下,有时候就没有异常,有时候就有 ,我网在也找不到合适的结论

192.168.31.116:  11:51:53 up  1:03,  1 user,  load average: 0.07, 0.26, 0.18
192.168.31.114: 11:51:53 up 1:03, 1 user, load average: 0.07, 0.26, 0.18
192.168.31.115: 11:51:53 up 1:03, 1 user, load average: 0.07, 0.26, 0.18
Exception in thread Thread-3:
Traceback (most recent call last):
File "/Users/admin/.pyenv/versions/2.7.13/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/Users/admin/.pyenv/versions/2.7.13/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "parallel.py", line 12, in ssh
key = paramiko.RSAKey.from_private_key_file( pkey_file, password='' )
File "/Users/admin/.pyenv/versions/2.7.13/lib/python2.7/site-packages/paramiko/pkey.py", line 196, in from_private_key_file
key = cls(filename=filename, password=password)
File "/Users/admin/.pyenv/versions/2.7.13/lib/python2.7/site-packages/paramiko/rsakey.py", line 46, in __init__
self._from_private_key_file(filename, password)
File "/Users/admin/.pyenv/versions/2.7.13/lib/python2.7/site-packages/paramiko/rsakey.py", line 174, in _from_private_key_file
self._decode_key(data)
File "/Users/admin/.pyenv/versions/2.7.13/lib/python2.7/site-packages/paramiko/rsakey.py", line 186, in _decode_key
raise SSHException(str(e))
SSHException: Invalid requirement, parse error at "''"
192.168.31.117: 11:51:54 up 1:03, 1 user, load average: 0.07, 0.25, 0.18
192.168.31.115: 11:51:54 up 1:03, 1 user, load average: 0.07, 0.25, 0.18
192.168.31.114: 11:51:54 up 1:03, 1 user, load average: 0.07, 0.25, 0.18
192.168.31.115: 11:51:55 up 1:03, 1 user, load average: 0.07, 0.25, 0.18
192.168.31.116: 11:51:55 up 1:03, 1 user, load average: 0.07, 0.25, 0.18
192.168.31.114: 11:51:55 up 1:03, 1 user, load average: 0.07, 0.25, 0.18
192.168.31.117: 11:51:55 up 1:03, 1 user, load average: 0.07, 0.25, 0.18

最新文章

  1. OpenERP 使用与开发笔记(一)
  2. CMD命令大全
  3. SQL Server Lock Escalation - 锁升级
  4. 用 phylomatic 软件生成的进化树
  5. String的一些蹊跷
  6. mysql 各种运算对于null值的处理
  7. 通过开发MSBuild ,引发的注册表相关知识
  8. UIScrollView 期本使用方法
  9. 检查主机是否存活的shell脚本
  10. CSS基础知识之float
  11. php获取服务器地址
  12. ural1987 Nested Segments
  13. emmet(快速开发)的使用
  14. 【渗透课程】第二篇下-HTTP协议的请求与响应深度剖析
  15. JWT
  16. awk、sed、date命令使用
  17. [POJ2287][Tyvj1048]田忌赛马 (贪心+DP)
  18. HashMap、HashTable
  19. Linux 下Tomcat服务器响应越来越慢分分析
  20. 前后端分离——token超时刷新策略

热门文章

  1. IOS--jenkins ,app,reengine
  2. iOS常用基础框架
  3. pandas1
  4. PL/SQL EXCEPTION捕获抛出异常
  5. XPath轴
  6. XMR恶意挖矿案例简析
  7. 项目发布脚本-go
  8. C:\WINDOWS\system32\drivers\etc\hosts 文件的作用
  9. Java编写验证码
  10. ubuntu shell脚本出错 dash