Python SSH Paramiko EOFError - python

I have just simple script like that to connect via SSH on Nokia router and execute command "show time":
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('adres ip', port=22, username='username', password='password')
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('show time')
output = stdout.readlines()
print '\n'.join(output)
ssh.close()
Login to the node is successful. I see myself on the router, but executing command are not gonna work. I get error like that:
Traceback (most recent call last): File "C:\Users\pkudalsk\Desktop\pyt.py", line 6, in <module>
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('show time') File "C:\Users\pkudalsk\Desktop\paramiko\client.py", line 479, in exec_command
chan.exec_command(command) File "C:\Users\pkudalsk\Desktop\paramiko\channel.py", line 63, in _check
return func(self, *args, **kwds) File "C:\Users\pkudalsk\Desktop\paramiko\channel.py", line 241, in exec_comman d
self._wait_for_event() File "C:\Users\pkudalsk\Desktop\paramiko\channel.py", line 1198, in
_wait_for_ event
raise e EOFError
Does anyone know what can cause this problem? I tried on python 3.6 and 2.7. The same result.
Thanks

Related

How to run a program in Paramiko's SSH ProxyCommand

I'm pretty new with Paramiko and python in general. I'm trying to connect to a server and execute some commands on it by ssh through the use of Paramiko's API and a program called Pomerium. My ssh config file is as shown below
Host *.stg-id-proxy.lab.com
ProxyCommand pomerium-cli tcp --listen - %h:%p
User placeholder
and my python code in file "ssh4.py" is as follows
import paramiko
host = "place.stg-id-proxy.lab.com"
port = 22
username = "placeholder"
password = "pass#!#"
command = "pwd"
proxy = paramiko.ProxyCommand("pomerium-cli tcp --listen - %h:%p")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password, allow_agent=False, look_for_keys=False, sock=proxy, banner_timeout=60)
stdin, stdout, stderr = ssh.exec_command(command)
lines = stdout.readlines()
print(lines)
but when I tried running:
python3 ssh4.py
I keep getting the following error
Exception (client): Error reading SSH protocol banner
Traceback (most recent call last):
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 2271, in _check_banner
buf = self.packetizer.readline(timeout)
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/packet.py", line 380, in readline
buf += self._read_timeout(timeout)
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/packet.py", line 622, in _read_timeout
raise socket.timeout()
socket.timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 2094, in run
self._check_banner()
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 2275, in _check_banner
raise SSHException(
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
Traceback (most recent call last):
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 2271, in _check_banner
buf = self.packetizer.readline(timeout)
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/packet.py", line 380, in readline
buf += self._read_timeout(timeout)
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/packet.py", line 622, in _read_timeout
raise socket.timeout()
socket.timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "ssh4.py", line 11, in <module>
ssh.connect(host, port, username, password, allow_agent=False, look_for_keys=False, sock=proxy, banner_timeout=60)
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/client.py", line 406, in connect
t.start_client(timeout=timeout)
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 699, in start_client
raise e
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 2094, in run
self._check_banner()
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 2275, in _check_banner
raise SSHException(
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
So, any help would be appreciated. Also documentation for Pomerium can be found here:
https://github.com/pomerium/cli
Thank you in advance.

Paramiko "TypeError: 'NoneType' object is not callable" at the end of a script that execute command on the server

I'm writing a script to execute commands in an EC2 instance, and come back. For that, I'm using Paramiko. My script is like this:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('<hostname>', username='<username>', password='<password>')
print('connected')
stdin, stdout, stderr = ssh.exec_command("ls -lah")
ssh.close()
print('done')
The trace I'm getting is:
connected
done
Exception ignored in: <function BufferedFile.__del__ at 0x7fa5de814950>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/file.py", line 66, in __del__
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/channel.py", line 1392, in close
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/channel.py", line 991, in shutdown_write
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/channel.py", line 963, in shutdown
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/channel.py", line 1246, in _send_eof
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paramiko/message.py", line 232, in add_int
TypeError: 'NoneType' object is not callable
The command which I use to login is:
ssh -o PubkeyAuthentication=no -l <username> <hostname>
Can someone tell me what I'm doing wrong here?
You are not waiting for the command to complete before you close the SSH connection.
When Python garbage-collects the stdin, stdout and stderr, Paramiko crashes, as it does not expect the connection to be closed.
For a correct code, see Wait to finish command executed with Python Paramiko.
if ssh is not None:
ssh.close()
del client, stdin, stdout, stderr

paramiko not executing basic command on machine

I am trying to execute a basic command on a device using paramiko ("show clock", which displays the time):
#!/usr/bin/python
import paramiko
import time
import re
hostname = 'HIDDEN1'
port = '22'
username = 'admin'
password = 'HIDDEN2'
if __name__ == "__main__":
s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname, port, username, password, timeout=3)
command = 'show clock'
print("Starting...")
stdin, stdout, stderr = s.exec_command(command)
s.close()
It doesn't run the command; I'm sure it connects though, since if I purposely make the password incorrect it hangs instead of returning an error. I ensured I can connect manually to the device and run the "show clock" command, but the paramiko snippet doesn't work. This is the error it returns:
Starting...
Traceback (most recent call last):
File "./para2.py", line 21, in <module>
stdin, stdout, stderr = s.exec_command('show clock')
File "/usr/lib/python2.6/site-packages/paramiko/client.py", line 350, in exec_command
chan.exec_command(command)
File "/usr/lib/python2.6/site-packages/paramiko/channel.py", line 213, in exec_command
self._wait_for_event()
File "/usr/lib/python2.6/site-packages/paramiko/channel.py", line 1084, in _wait_for_event
raise e
EOFError
The server might not be allowing exec_command()
Try using the interactive shell
ssh_client = paramiko.SSHClient()
shh_client.connect(#creds)
shell = ssh_client.invoke_shell()
You can now use shell.send() and shell.recv() to execute commands and get back their outputs
http://docs.paramiko.org/en/2.7/api/channel.html#paramiko.channel.Channel.send
http://docs.paramiko.org/en/2.7/api/channel.html#paramiko.channel.Channel.recv
Example: https://www.semicolonworld.com/question/56794/implement-an-interactive-shell-over-ssh-in-python-using-paramiko

Proxycommand in paramiko

I'm trying to do a simple proxycommand using paramiko in python.
Basically I'm trying to replicate the behaviour of this ssh command:
ssh -i ~/.ssh/destination_key user#destination.test.internal -o 'ProxyCommand ssh -i ~/.ssh/jumpbox_key -W %h:%p user#jumpbox.test.internal'
The above works as expected amd connects to destination.test.internal.
I'm trying to do the same thing in python with the following on the same box:
#!/usr/bin/python3
import paramiko
import argparse
addresses = ["destination.test.internal"];
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
proxy = paramiko.ProxyCommand("ssh -i ~/.ssh/jumpbox_key -W %h:%p user#jumpbox.test.internal")
for address in addresses:
#Connect over ssh to each server
try:
ssh.connect(address , username='user', key_filename="~/.ssh/destination_key", sock = proxy )
except paramiko.AuthenticationException:
print ("Authentication Failed")
except paramiko.SSHException:
print ("Connection Failed")
stdin,stdout,stderr = ssh.exec_command('ls -l')
print (stdout.readlines())
ssh.close()
Needless to say this isn't working. It's failing with:
Traceback (most recent call last):
Exception: Error reading SSH protocol banner
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 1893, in _check_banner
buf = self.packetizer.readline(timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 331, in readline
buf += self._read_timeout(timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 501, in _read_timeout
raise socket.timeout()
socket.timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 1749, in run
self._check_banner()
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 1897, in _check_banner
raise SSHException('Error reading SSH protocol banner' + str(e))
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
Connection Failed
Traceback (most recent call last):
File "./log_file_fix.py", line 31, in <module>
stdin,stdout,stderr = ssh.exec_command('ls -l')
File "/usr/local/lib/python3.5/dist-packages/paramiko/client.py", line 436, in exec_command
chan = self._transport.open_session(timeout=timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 716, in open_session
timeout=timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 800, in open_channel
raise SSHException('SSH session not active')
paramiko.ssh_exception.SSHException: SSH session not active
However I'm not sure where I'm going wrong.
Instead of %h:%p, Specify host and port inside paramiko.ProxyCommand()
proxy = paramiko.ProxyCommand("ssh -i ~/.ssh/jumpbox_key -W DESTINATION_HOST_ADDRESS:22 user#jumpbox.test.internal")
You have to change your code like below:
#!/usr/bin/python3
import paramiko
import argparse
addresses = ["destination.test.internal"];
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for address in addresses:
proxy_jump_command='ssh -i ~/.ssh/jumpbox_key -W {HOST}:{PORT} user#jumpbox.test.internal'.format(HOST=address, PORT=22)
proxy = paramiko.ProxyCommand(proxy_jump_command)
#Connect over ssh to each server
try:
ssh.connect(address , username='user', key_filename="~/.ssh/destination_key", sock = proxy )
except paramiko.AuthenticationException:
print ("Authentication Failed")
except paramiko.SSHException:
print ("Connection Failed")
stdin,stdout,stderr = ssh.exec_command('ls -l')
print (stdout.readlines())
ssh.close()

Running Multiple SSH Commands Using Paramiko in Python 3.4.3

I'm trying to run multiple commands on a box, but ONLY the first command succeeds. It seems something related to fork, but I cannot figure out how to resolve this. Your help or guidance will be deeply appreciated.
MY SCRIPT
import paramiko
#Made in python 3.4.3
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('xxx.xx.xx.xx', port=22, username='domain\myusername', password='mypassword')
stdin, stdout, stderr = ssh.exec_command('vol status -f') #this being the first command works fine
output = stdout.readlines()
print ("\n".join(output))
stdin, stdout, stderr = ssh.exec_command('disk show -n')
output1 = stdout.readlines()
print ("\n".join(output1))
#stdin, stdout, stderr = ssh.exec_command('vol status -s')
#stdin, stdout, stderr = ssh.exec_command('df -Ag')
ssh.close()
input("Press <Enter> to exit ")
OUTPUT:
Traceback (most recent call last):
File "C:\Users\myusername\Desktop\folder_copy\test.py", line 13, in <module>
stdin, stdout, stderr = ssh.exec_command('disk show -n')
File "C:\Python34\lib\site-packages\paramiko-1.16.0-py3.4.egg\paramiko\client.py", line 401, in exec_command
chan = self._transport.open_session(timeout=timeout)
File "C:\Python34\lib\site-packages\paramiko-1.16.0-py3.4.egg\paramiko\transport.py", line 702, in open_session
timeout=timeout)
File "C:\Python34\lib\site-packages\paramiko-1.16.0-py3.4.egg\paramiko\transport.py", line 823, in open_channel
raise e
File "C:\Python34\lib\site-packages\paramiko-1.16.0-py3.4.egg\paramiko\transport.py", line 1726, in run
ptype, m = self.packetizer.read_message()
File "C:\Python34\lib\site-packages\paramiko-1.16.0-py3.4.egg\paramiko\packet.py", line 386, in read_message
header = self.read_all(self.__block_size_in, check_rekey=True)
File "C:\Python34\lib\site-packages\paramiko-1.16.0-py3.4.egg\paramiko\packet.py", line 251, in read_all
raise EOFError()
EOFError
If you believe fork is the issue, try the atfork() method of the Transport class.

Categories