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
Related
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
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
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.
Environment:
Python: 2.7.6
Paramiko: 1.13.0
Target SSH Server: Cisco MDS NX-OS 6.2(3)
Code:
import paramiko
import StringIO
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("switchname", username="username", password="password")
stdin, stdout, stderr = ssh.exec_command("term length 0")
stdin, stdout, stderr = ssh.exec_command("show zoneset active")
t = stdout.read()
ssh.close()
for line in StringIO.StringIO(t):
line = line.replace("\n","")
print line
It is only producing few lines of output instead of the entire output. If I modify above code and add one more ssh.exec_command like shown below.
Code:
import paramiko
import StringIO
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("switchname", username="username", password="password")
stdin, stdout, stderr = ssh.exec_command("term length 0")
stdin, stdout, stderr = ssh.exec_command("show zoneset active")
stdin2, stdout2, stderr2 = ssh.exec_command("show version ")
t = stdout.read()
ssh.close()
for line in StringIO.StringIO(t):
line = line.replace("\n","")
print line
It produce following errors:
Traceback (most recent call last):
File "paramiko_issue.py", line 15, in <module>
stdin, stdout, stderr = ssh.exec_command("show zoneset active")
File "build/bdist.macosx-10.9-intel/egg/paramiko/client.py", line 307, in exec_command
File "build/bdist.macosx-10.9-intel/egg/paramiko/transport.py", line 543, in open_session
File "build/bdist.macosx-10.9-intel/egg/paramiko/transport.py", line 607, in open_channel
paramiko.ssh_exception.SSHException: SSH session not active
I would really appreciate any help in resolving this issue. Thank you in advance.
I'm trying to write a simple script to connect Cisco C2960 switcher.I just can't figure out how to re-use the ssh session to execute more than two commands.
There's a discussion on SO,
Persistent ssh session to Cisco router
but none of the answers provided there can solve my problem.
Here's my code:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('xxx',username='xxx',password='xxx',allow_agent=False)
stdin, stdout, stderr = ssh.exec_command('show version')
stdin, stdout, stderr = ssh.exec_command('sh mac brief')
Results in:
Traceback (most recent call last):
File "./test.py", line 10, in <module>
stdin, stdout, stderr = ssh.exec_command('sh mac brief')
File "/usr/lib/python2.6/site-packages/paramiko/client.py", line 363, in exec_command
chan = self._transport.open_session()
File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 658, in open_session
return self.open_channel('session')
File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 746, in open_channel
raise e
EOFError
invoke_shell() is best when interacting with Cisco IOS, i tried other fucntions in paramiko but all of them throw buggy EOF file errors
I answered this on the referenced SO question, but did you try using invoke_shell()?
I've seen many reports that some Cisco devices only allow one command execution before closing the connection (this may be configurable somewhere in the device though). In this case, you need to start a shell, and work interactively (or pseudo-interactively like with pexpect), or create a script to send as a single command.