SergeySD
SergeySD

Reputation: 83

paramiko.ssh_exception.SSHException: No existing session

I don't understand behavior of paramiko module, could you please help me with it: example with incorrect behavior is very simple. I have 2 modules:

module_for_importing.py:

import paramiko 
_host = "scl.example.com"
_user = "root"
_password = "ready"
_timeout = 10

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(_host, username=_user, password=_password, timeout=_timeout)

and second module(contains only one line of import)

runner.py:

from module_for_importing import *

so, magic start when I try to run python2.7 runner.py I got an exception in method ssh.connect():

Traceback (most recent call last):
  File "E:/testing/runner.py", line 1, in <module>
    from module_for_importing import *
  File "E:\testing\module_for_importing.py", line 13, in <module>
    ssh.connect(_host, username=_user, password=_password, timeout=_timeout)
  File "C:\Python27\lib\site-packages\paramiko\client.py", line 398, in connect
    server_key = t.get_remote_server_key()
  File "C:\Python27\lib\site-packages\paramiko\transport.py", line 720, in get_remote_server_key
    raise SSHException('No existing session')
paramiko.ssh_exception.SSHException: No existing session

but if start python module: python2.7 module_for_importing.py

all work fine.

Could you please help me to understand root cause of the exception? for python 3, there is no any errors.

Upvotes: 1

Views: 4040

Answers (1)

Brozo
Brozo

Reputation: 337

You want to create a proper object for ssh:

import paramiko


class SSH(object):

    def __init__(self):
        pass

    def est_conn(self, ip, username, password):
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.WarningPolicy())
        ssh.connect(ip, username=username, password=password)
        print("Asserting that ssh connection has been established...")
        assert ssh

        return ssh

ssh = SSH()

and then in your runner.py use:

from module_for_importing import ssh

Hope this helps.

Upvotes: 2

Related Questions