EOF error with both exec_command and invoke_shell method in paramiko

2024/11/15 22:30:06

I am trying to execute a command on linux server from my windows machine using python paramiko , I used both of the methods

1.exec_command

2.invoke_shell

Both of them giving EOF error.

import paramiko
import time
def ConnectSSH1(host, port, username, password):ssh = paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(host, int(port), username, password)chan = ssh.invoke_shell() # Throws EOF error here #stdin, stdout, stderr = ssh.exec_command("ls")   #Throws EOF error here with exec_commandchan.send("ls -a")time.sleep(5)output = chan.recv(9999)print (output)print (command)return (output)

Error in exec_command

C:\RobotFramework>python paramiko_solution1.py
Traceback (most recent call last):File "paramiko_solution1.py", line 15, in <module>chan.exec_command(command)File "C:\Python37\lib\site-packages\paramiko\channel.py", line 72, in _checkreturn func(self, *args, **kwds)File "C:\Python37\lib\site-packages\paramiko\channel.py", line 257, in exec_commandself._wait_for_event()File "C:\Python37\lib\site-packages\paramiko\channel.py", line 1226, in _wait_for_eventraise eFile "C:\Python37\lib\site-packages\paramiko\transport.py", line 2055, in runptype, m = self.packetizer.read_message()File "C:\Python37\lib\site-packages\paramiko\packet.py", line 459, in read_messageheader = self.read_all(self.__block_size_in, check_rekey=True)File "C:\Python37\lib\site-packages\paramiko\packet.py", line 303, in read_allraise EOFError()
EOFError

Error in invoke_shell command

>>> channel.get_pty()
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "C:\Python37\lib\site-packages\paramiko\channel.py", line 72, in _checkreturn func(self, *args, **kwds)File "C:\Python37\lib\site-packages\paramiko\channel.py", line 203, in get_ptyself._wait_for_event()File "C:\Python37\lib\site-packages\paramiko\channel.py", line 1226, in _wait_for_eventraise eFile "C:\Python37\lib\site-packages\paramiko\transport.py", line 2055, in runptype, m = self.packetizer.read_message()File "C:\Python37\lib\site-packages\paramiko\packet.py", line 459, in read_messageheader = self.read_all(self.__block_size_in, check_rekey=True)File "C:\Python37\lib\site-packages\paramiko\packet.py", line 303, in read_allraise EOFError()
EOFError
>>>

Parmakio logs

CRITICAL:root:None
DEBUG:paramiko.transport:starting thread (client mode): 0x8133c488
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.7.1
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-SSH
INFO:paramiko.transport:Connected (version 2.0, client SSH)
DEBUG:paramiko.transport:kex algos:['diffie-hellman-group-exchange-sha256', 'diffie-hellman-group1-sha1', 'diffie-hellman-group14-sha1'] server key:['ssh-rsa'] client encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', '3des-cbc', 'arcfour'] server encrypt:['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-cbc', 'blowfish-cbc', 'cast128-cbc', 'aes192-cbc', 'aes256-cbc', '3des-cbc', 'arcfour'] client mac:['hmac-md5', 'hmac-md5-96', 'hmac-sha1', 'hmac-sha1-96', 'hmac-sha2-256', 'hmac-sha2-512'] server mac:['hmac-md5', 'hmac-md5-96', 'hmac-sha1', 'hmac-sha1-96', 'hmac-sha2-256', 'hmac-sha2-512'] 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-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256
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 12.22.124.66: b'16d9d878229cssdcc837d33a64'
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
CRITICAL:root:None
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

Logs from SSH windows

ssh -vv [email protected] /usr/bin/who
OpenSSH_for_Windows_8.1p1, LibreSSL 2.9.2
debug2: resolve_canonicalize: hostname 12.22.124.66 is address
debug2: ssh_connect_direct
debug1: Connecting to 12.22.124.66 [12.22.124.66] port 22.
debug1: Connection established.
debug1: identity file C:\\Users\\eijm/.ssh/id_rsa type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_rsa-cert type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_dsa type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_dsa-cert type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_ecdsa type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_ecdsa-cert type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_ed25519 type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_ed25519-cert type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_xmss type -1
debug1: identity file C:\\Users\\eijm/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_8.1
debug1: Remote protocol version 2.0, remote software version SSH
debug1: no match: SSH
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to 12.22.124.66:22 as 'eijmto'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c
debug2: host key algorithms: [email protected],[email protected],[email protected],rsa-sha2-512,rsa-sha2-256,ssh-rsa,[email protected],[email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519
debug2: ciphers ctos: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: ciphers stoc: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: MACs ctos: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,[email protected],zlib
debug2: compression stoc: none,[email protected],zlib
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: diffie-hellman-group-exchange-sha256,diffie-hellman-group1-sha1,diffie-hellman-group14-sha1
debug2: host key algorithms: ssh-rsa
debug2: ciphers ctos: aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,3des-cbc,arcfour
debug2: ciphers stoc: aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,3des-cbc,arcfour
debug2: MACs ctos: hmac-md5,hmac-md5-96,hmac-sha1,hmac-sha1-96,hmac-sha2-256,hmac-sha2-512
debug2: MACs stoc: hmac-md5,hmac-md5-96,hmac-sha1,hmac-sha1-96,hmac-sha2-256,hmac-sha2-512
debug2: compression ctos: none
debug2: compression stoc: none
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug1: kex: algorithm: diffie-hellman-group-exchange-sha256
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(2048<8192<8192) sent
debug1: got SSH2_MSG_KEX_DH_GEX_GROUP
debug2: bits set: 4078/8192
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: got SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: ssh-rsa SHA256:gj6tlHPgeFidgXKMwymWz55UiuGnFwFw38GwSToCQMk
debug1: Host '12.22.124.66' is known and matches the RSA host key.
debug1: Found key in C:\\Users\\eijm/.ssh/known_hosts:1
debug2: bits set: 4015/8192
debug2: set_newkeys: mode 1
debug1: rekey out after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 4294967296 blocks
debug1: pubkey_prepare: ssh_get_authentication_socket: No such file or directory
debug1: Will attempt key: C:\\Users\\eijm/.ssh/id_rsa
debug1: Will attempt key: C:\\Users\\eijm/.ssh/id_dsa
debug1: Will attempt key: C:\\Users\\eijm/.ssh/id_ecdsa
debug1: Will attempt key: C:\\Users\\eijm/.ssh/id_ed25519
debug1: Will attempt key: C:\\Users\\eijm/.ssh/id_xmss
debug2: pubkey_prepare: done
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: C:\\Users\\eijm/.ssh/id_rsa
debug1: Trying private key: C:\\Users\\eijm/.ssh/id_dsa
debug1: Trying private key: C:\\Users\\eijm/.ssh/id_ecdsa
debug1: Trying private key: C:\\Users\\eijm/.ssh/id_ed25519
debug1: Trying private key: C:\\Users\\eijm/.ssh/id_xmss
debug2: we did not send a packet, disable method
debug1: Next authentication method: password
debug1: read_passphrase: can't open /dev/tty: No such file or directory
[email protected]'s password:
debug2: we sent a password packet, wait for reply
debug1: Authentication succeeded (password).
Authenticated to 12.22.124.66 ([12.22.124.66]:22).
debug1: channel 0: new [client-session]
debug2: channel 0: send open
debug1: Entering interactive session.
debug1: pledge: network
debug2: channel_input_open_confirmation: channel 0: callback start
debug2: fd 3 setting TCP_NODELAY
debug2: client_session2_setup: id 0
debug1: Sending command: /usr/bin/who
debug2: channel 0: request exec confirm 1
debug2: channel_input_open_confirmation: channel 0: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
debug2: channel_input_status_confirm: type 99 id 0
debug2: exec request accepted on channel 0
debug2: channel 0: rcvd eof
debug2: channel 0: output open -> drain
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug2: channel 0: rcvd close
debug2: channel 0: chan_shutdown_read (i0 o1 sock -1 wfd 4 efd 6 [write])
debug2: channel 0: input open -> closed
ansda pts/9        2020-05-13 09:28 (185.39.130.179)
moroka pts/13       2019-07-29 17:04 (185.39.130.181)
eijmto pts/15       2020-05-13 09:35 (172.30.170.1)
debug2: channel 0: obuf empty
debug2: channel 0: chan_shutdown_write (i3 o1 sock -1 wfd 5 efd 6 [write])
debug2: channel 0: output drain -> closed
debug2: channel 0: almost dead
debug2: channel 0: gc: notify user
debug2: channel 0: gc: user detached
debug2: channel 0: send close
debug2: channel 0: is dead
debug2: channel 0: garbage collecting
ion, nchannels 1
conds

Kindly let me know , what i am missing.

Paramiko version is 2.7.1

Answer

Use timeout in exec_command()

SSHClient.exec_command(command, bufsize=-1, timeout=None, get_pty=False) Execute a command on the SSH server. A new .Channel is opened and the requested command is executed. The command’s input and output streams are returned as Python file-like objects representing stdin, stdout, and stderr.

Parameters: command (str) – the command to execute bufsize (int) – interpreted the same way as by the built-in file() function in Python timeout (int) – set command’s channel timeout. See Channel.settimeout.settimeout Returns:
the stdin, stdout, and stderr of the executing command, as a 3-tuple

Raises SSHException:

if the server fails to execute the command

https://en.xdnf.cn/q/119264.html

Related Q&A

Trying to simulate an Overwatch loot box opening program in Python

So basically I am trying to recreate opening a loot box in Overwatch into a runnable program in python. Im trying to make it take four random items in an array and display them each time the user types…

How to remove extra commas from data in Python

I have a CSV file through which I am trying to load data into my SQL table containing 2 columns. I have 2 columns and the data is separated by commas, which identify the next field. The second column c…

How to linearize the sum of a product of two decision variables in LP?

Being new to Linear Programming and Gurobi, I am dealing with an Integer Linear program where I have two binary decision variables defined as B[u_v, x_y] and A[u_x], I am trying to implement this const…

Basic calculator program in python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Why am I receiving ERROR 404 - when attempting to use Python Flask

I have been following this tutorial: https://kb.objectrocket.com/postgresql/scrape-a-website-to-postgres-with-python-938 My app.py file looks like this (taken from the above tutorial): from flask impor…

Merge the dataframes dynamically

I am extracting data from APIs to generate a report. But the number of APIs are dynamic for each report. It can be 1,2,3,5 etc. Once we get the data , we need to store the data as dataframe, to generat…

Read an xml element using python

I have an xml file. I want to search for a specific word in the file, and if i find it- i want to copy all of the xml element the word was in it.for example:<Actions><ActionGroup enabled="…

Using .rsplit() not giving desired results

I want to manipulate a string using .rsplit() so that anything after the last comma in a string of data is split using commas. As an example:,000...should be changed to:,0,0,0,In order to this I am usi…

Concatenate numbers in binary [duplicate]

This question already has answers here:Convert int to binary string in Python(36 answers)Closed 7 years ago.When converting a number in binary in Python what you get is the following:b = bin(77) print(…

AttributeError: DataFrame object has no attribute path

Im trying incrementally to build a financial statement database. The first steps center around collecting 10-Ks from the SECs EDGAR database. I have code for pulling the relevant 8-Ks, 10-Ks, and 10-Qs…