使用 OCNOS 无法从路由器获得所需的响应。在Python中

问题描述 投票:0回答:1

正如响应中提到的,命令已在路由器上成功执行。但响应不完整或不正确。我可能没有正确阅读回复。我怎样才能读到正确的答案?

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。

python shell ssh router paramiko
1个回答
0
投票

命令后缺少 Enter

即取决于远程系统,

\n
\r
(甚至
\r\n
)。


一般来说,您不应该使用

invoke_shell
,但我了解您的设备可能不支持正确的
exec_command

在设备上使用 Paramiko exec_command 执行命令不起作用

© www.soinside.com 2019 - 2024. All rights reserved.