I intend to run several commands on the remote host using paramiko, but the ssh session is closed after running the command.
Code below:
from paramiko import SSHClient import paramiko ssh = SSHClient() ssh.load_system_host_keys() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, 22, user, passwd, timeout=3) stdin, stdout, stderr = ssh.exec_command('uname -a')
So, is there a way to stop the ssh session from closing? Or any alternatives to paramiko?
Update :
I was able to support the exec_command call on my Macbook when connected to a Linux server, but the ssh session was automatically closed after exec_command once on the Linux server when connected to the switch and SSHException: paramiko.ssh_exception.SSHException: SSH session not active
>>> print ssh.get_transport() >>> <paramiko.Transport at 0xf00216d0L (unconnected)> >>> print ssh.get_transport().is_active() >>> False >>> print ssh.get_transport().is_authenticated() >>> False
Is there a way to keep paramiko ssh session active all the time?
Paramiko debug mode information is returned as follows:
initial stream (client mode): 0x2657e10L
Connected (version 1.99, Comware-5.20 client)
kex algos: [u'diffie-hellman-group-exchange-sha1 ', u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1 '] server key: [u'ssh-rsa'] the client encrypts: [u'aes128-cbc ', u'3des-cbc', u'des-cbc '] the server encrypts: [u'aes128-cbc', u'3des-cbc ', u'des-cbc'] mac client: [u'hmac-sha1 ', u'hmac-sha1-96', u'hmac-md5 ', u'hmac-md5-96'] mac server: [u'hmac-sha1 ', u'hmac -sha1-96 ', u'hmac-md5', u'hmac-md5-96 '] client compress: [u'none'] server compress: [u'none '] client lang: [u' '] server lang : [u ''] kex follows? False
Ciphers agreed: local = aes128-cbc, remote = aes128-cbc
using kex diffie-hellman-group14-sha1; ssh-rsa server key type cipher: local aes128-cbc, remote aes128-cbc; mac: local hmac-sha1, remote hmac-sha1; compression: local no, remote no
Switch to new keys ...
userauth ok
Authentication (password) is successful!
[chan 0] Maximum packet in: 32768 bytes
[chan 1] Maximum packet in: 32768 bytes
[chan 0] Maximum packet: 32496 bytes
Secsh channel is open.
Secsh channel 2 is open FAILED:
Lack of resources: lack of resources
[chan 0] Sesch channel 0 request ok
[chan 0] EOF sent (0)