Reputation: 3042
I am trying to write an SSH Client that will be used in order to communicate with the devices in my LAB. I used paramiko module in order to connect to the device and it seems that I did manage to connect to the device. But, I don't seem to manage to send a certain command over to the device. As these are the first lines that I write in this kind of application (SSH with paramiko) I can't tell where did I fail.
I attached the code along with Debug in order to get the professionals help
>>> import paramiko
>>> Client_235 = paramiko.SSHClient()
>>> Client_235.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> Client_235.load_system_host_keys()
>>> Client_235.connect('50.0.0.235', username='admin', password='admin')
DEBUG:paramiko.transport:starting thread (client mode): 0x4abddd8
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.4.0
DEBUG:paramiko.transport:Remote version/idstring: SSH-1.99-IPSSH-6.8.0
INFO:paramiko.transport:Connected (version 1.99, client IPSSH-6.8.0)
DEBUG:paramiko.transport:kex algos:['diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1', 'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group-exchange-sha256'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-cbc', 'aes192-cbc', 'aes256-cbc', 'blowfish-cbc', 'cast128-cbc', '3des-cbc', 'des-cbc', 'des-cbc', 'arcfour128', 'arcfour'] server encrypt:['aes128-cbc', 'aes192-cbc', 'aes256-cbc', 'blowfish-cbc', 'cast128-cbc', '3des-cbc', 'des-cbc', 'des-cbc','arcfour128', 'arcfour'] client mac:['hmac-sha1', 'hmac-sha1-96', 'hmac-md5', 'hmac-md5-96'] server mac:['hmac-sha1', 'hmac-sha1-96', 'hmac-md5', 'hmac-md5-96'] client compress:['none'] server compress:['none'] client lang:[''] server lang:[''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: diffie-hellman-group-exchange-sha256
DEBUG:paramiko.transport:HostKey agreed: ssh-rsa
DEBUG:paramiko.transport:Cipher agreed: aes128-cbc
DEBUG:paramiko.transport:MAC agreed: hmac-sha1
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:Got server p (2048 bits)
DEBUG:paramiko.transport:kex engine KexGexSHA256 specified hash_algo <built-in function openssl_sha256>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ssh-rsa host key for 50.0.0.235: b'9acccee326cb2423aba3216cee6f6ba9'
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
>>> stdin, stdout, stderr = Client_235.exec_command('c i eth i')
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:paramiko.transport:EOF in transport thread
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Program Files\Python35\lib\site-packages\paramiko\client.py", line 8
6, in exec_command chan.exec_command(command)
File "C:\Program Files\Python35\lib\site-packages\paramiko\channel.py", line 63, in _check return func(self, *args, **kwds)
File "C:\Program Files\Python35\lib\site-packages\paramiko\channel.py", line 241, in exec_command self._wait_for_event()
File "C:\Program Files\Python35\lib\site-packages\paramiko\channel.py", line 1198, in _wait_for_event raise e
File "C:\Program Files\Python35\lib\site-packages\paramiko\transport.py", line 1872, in run ptype, m = self.packetizer.read_message()
File "C:\Program Files\Python35\lib\site-packages\paramiko\packet.py", line 426, in read_message header = self.read_all(self.__block_size_in, check_rekey=True)
File "C:\Program Files\Python35\lib\site-packages\paramiko\packet.py", line 276, in read_all raise EOFError()
EOFError
>>>
What am I doing wrong ? What is the right way to send commands over an SSH connection ?
Upvotes: 2
Views: 10353
Reputation: 20698
You SSH server may not support or allow exec_command()
. You can verify this by manually running the following ssh command (e.g. from a shell):
ssh user@host c i eth i
With manual
ssh user@host c i eth i
"I get the following "Connection to 50.0.0.235 closed by remote host."
You need to start an interative shell in paramiko:
ssh = paramiko.SSHClient()
ssh.connect(...)
chan = ssh.invoke_shell()
then you can use APIs like Channel.send()
and Channel.recv()
to send commands and get outputs. For more details, see paramiko's doc about Channel.
(Here's a simple example.)
Upvotes: 3