paramiko no existing session exception

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()

Results in:

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-", 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-", 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 allow_agent, look_for_keys:


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 list:

ssh-add -l

To delete all:

ssh-add -D

To re-add:

ssh-add /FULL/PATH/TO/id_rsa

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)
     # probably a dangling env var: the ssh agent is gone
         self.conn = conn
 elif sys.platform == 'win32':
     import win_pageant
     if win_pageant.can_talk_to_agent():
         self.conn = win_pageant.PageantConnection()

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.

     ssh = paramiko.SSHClient()               
                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))

Replace ‘localhost’ by ‘’.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .