Python paramiko SSHException 解决办法
Python 中的 paramiko模块是一个非常方便好用的连接远端 ssh 通道的方法。使用起来也很方便:
- import paramiko
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect('localhost',username=name,password=pw)
实践中,遇到一些异常,奇怪的是,同样的代码,某些服务器上面是好的,而某些服务器上面是坏的。
- File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 337, in connect
- self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)
- File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 528, in _auth
- raise saved_exception
- AuthenticationException: Authentication failed.
- File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 337, in connect
- self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)
- File "/usr/local/lib/python2.7/site-packages/paramiko/client.py", line 528, in _auth
- raise saved_exception
- paramiko.SSHException: No existing session
最终找到的解决方法很简单,在connect 的时候加入参数 allow_agent, look_for_keys 就可以了。
- ssh.connect('localhost',username=name,password=pw,allow_agent=False,look_for_keys=False)