Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paramiko no existing session exception

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)

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-1.7.7.1-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-1.7.7.1-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.

like image 363
Sam Johnson Avatar asked Jul 26 '11 15:07

Sam Johnson


People also ask

How do I get stdout from Paramiko?

Using exec_command() in Paramiko returns a tuple (stdin, stdout, stderr) . Most of the time, you just want to read stdout and ignore stdin and stderr . You can get the output the command by using stdout. read() (returns a string) or stdout.

What is Paramiko transport?

An SSH Transport attaches to a stream (usually a socket), negotiates an encrypted session, authenticates, and then creates stream tunnels, called channels , across the session. Multiple channels can be multiplexed across a single session (and often are, in the case of port forwardings).

What can you do with Paramiko?

Paramiko helps you automate repetitive system administration tasks on remote servers. More advanced Paramiko programs send the lines of a script one at a time. It does this rather than transacting all of a command, such as df or last , synchronously to completion.


2 Answers

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:

ssh.connect('localhost',username=name,password=pw,allow_agent=False,look_for_keys=False)
like image 200
Prashant Borde Avatar answered Oct 14 '22 18:10

Prashant Borde


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
like image 12
Cory Ringdahl Avatar answered Oct 14 '22 16:10

Cory Ringdahl