python_paramiko_SSHException Invalid requirement, parse error at


  • 不加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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM