Each Answer to this Q is separated by one/two green lines.
Using the python interactive shell and openssh running locally, I keep getting an “No existing session” exception using paramiko. My code is below.
import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('localhost',username=name,password=pw)
No handlers could be found for logger "paramiko.transport" Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.6/dist-packages/paramiko-22.214.171.124-py2.6.egg/paramiko/client.py", line 332, in connect self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys) File "/usr/local/lib/python2.6/dist-packages/paramiko-126.96.36.199-py2.6.egg/paramiko/client.py", line 493, in _auth raise saved_exception paramiko.SSHException: No existing session
I was able to connect previously, but had been trying to adjust this to allow for key based authorization. That failed, and since then I have not been able to connect locally. I have tried restarting openssh, and have connected to another server successfully. After searching through here, all I have found are mentions of authorization exceptions, which does not appear to be the case here.
As you already have password you don’t need to talk to agent or look for private keys stored on your machine. So try passing extra parameters
I had a spare public key with a key pass phrase in my ssh-add list. Once I removed it, I was able to execute my paramiko based script properly.
To delete all:
Which OS are you using?
Maybe you can check your env variable:
for “connect”, it will try to use ssh agent.
self.conn = None self.keys = () if ('SSH_AUTH_SOCK' in os.environ) and (sys.platform != 'win32'): conn = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: conn.connect(os.environ['SSH_AUTH_SOCK']) except: # probably a dangling env var: the ssh agent is gone return self.conn = conn elif sys.platform == 'win32': import win_pageant if win_pageant.can_talk_to_agent(): self.conn = win_pageant.PageantConnection() else: return
Just got the same error
ERROR:SSHException('No existing session',) but since it was in a clean docker container, there was no ssh-agent.
After some hours of debugging, I found a different solution: it can happen when there is a timeout during key exchange! In my case, the ssh server is a router over GSM link, which is very slow.
You can enable debug on paramiko with:
And if you see the exception between the
Connected and the
Switch to new keys ..., it means the timeout was during the key exchange. In this case, you have to set timeout to a bigger value! (documentation says timeout is only for TCP connect, but in fact, it is also for the whole negotiation before auth!)
In my case ,I have try use allow_agent=False,look_for_keys=False, but not work .
I ssh to a 2G device, so timeout=10 is Ok, timeout=3 get” Unable to establish SSH connection: No existing session”. Not timeout except.
So try timeout= a long time,if connect to a not establish ssh.
try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip,22,username,passwd,timeout=10,allow_agent=True,look_for_keys=True) print ('%s\tOK\n'%(ip) ) except socket.timeout: print ("%s time out"%(ip)) except paramiko.AuthenticationException: print("Authentication failed, please verify your credentials: %s"%(ip)) except paramiko.SSHException as sshException: print("Unable to establish SSH connection: %s" %(sshException)) except paramiko.BadHostKeyException as badHostKeyException: print("Unable to verify server's host key: %s" %(badHostKeyException)) finally: ssh.close()
Replace ‘localhost’ by ‘127.0.0.1’.