简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM