paramiko 没有在机器上执行基本命令

paramiko not executing basic command on machine

我正在尝试使用 paramiko("show clock",显示时间)在设备上执行基本命令:

#!/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()

它不是 运行 命令;我确定它可以连接,因为如果我故意使密码不正确,它会挂起而不是返回错误。我确保我可以手动连接到设备和 运行 "show clock" 命令,但 paramiko 代码段不起作用。这是错误吧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

服务器可能不允许 exec_command()

尝试使用交互式 shell

ssh_client = paramiko.SSHClient()
shh_client.connect(#creds)
shell = ssh_client.invoke_shell()

您现在可以使用 shell.send() 和 shell.recv() 来执行命令并取回它们的输出

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

示例:https://www.semicolonworld.com/question/56794/implement-an-interactive-shell-over-ssh-in-python-using-paramiko