正如响应中提到的,命令已在路由器上成功执行。但响应不完整或不正确。我可能没有正确阅读回复。我怎样才能读到正确的答案?
import paramiko
import logging
# Enable debug-level logging
logging.basicConfig(level=logging.DEBUG)
hostname = '10.1.1.86'
port = 22
username = '***'
password = '***'
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# Enable logging to a file
paramiko.util.log_to_file("paramiko.log")
ssh_client.connect(hostname=hostname, port=port, username=username, password=password)
shell = ssh_client.invoke_shell()
#shell.send("show version\n")
#output = shell.recv(65535).decode()
commands = [
'enable',
'show version',
'show ip route',
]
for command in commands:
shell.send(command)
output = shell.recv(65535).decode()
print("Command Output:")
print(output)
print("--- COMMAND EXECUTION FINISHED ---")
except paramiko.AuthenticationException:
print("Authentication failed, please verify your credentials.")
except paramiko.SSHException as e:
print("SSH connection failed:", str(e))
except Exception as e:
print("An error occurred:", str(e))
finally:
ssh_client.close()
这是路由器的响应...
Command Output:
Linux CSGW-N1 4.19.91-g1790faa27 #1 SMP Mon Mar 7 15:51:25 UTC 2022 x86_64
Last login: Tue Oct 8 11:15:28 2019 from 10.1.1.88
--- COMMAND EXECUTION FINISHED ---
Command Output:
enable
--- COMMAND EXECUTION FINISHED ---
Command Output:
show version
--- COMMAND EXECUTION FINISHED ---
DEBUG:paramiko.transport:EOF in transport thread
大多数帮助适用于 CISCO 设备,但这使用的是 OCNOS。
命令后缺少 Enter。
即取决于远程系统,
\n
或\r
(甚至\r\n
)。
一般来说,您不应该使用
invoke_shell
,但我了解您的设备可能不支持正确的exec_command
。