【问题标题】:netmiko can't execute 'sh run | i host'netmiko 无法执行 'sh run |我主持'
【发布时间】:2020-04-15 06:38:53
【问题描述】:

我注意到我的 netmiko 代码无法运行 sh run | i host 这是一个合法的 Cisco 命令。

当我将sh run 替换为sh closhow ip interface brief 等其他命令时,它可以完美运行。

from netmiko import ConnectHandler

R1 = {
    'device_type': 'cisco_ios',
    'ip': 'Router1',
    'username': 'u',
    'password': 'p'
}

R2 = {
    'device_type': 'cisco_ios',
    'ip': 'Router2',
    'username': 'u',
    'password': 'p'
}

all_devices = [R1, R2]

for device in all_devices:
    connect = ConnectHandler(**device)
    output = connect.send_command('sh run | i host')
    print(output)

输出

user@linux:~$ python3 script.py 
^
% Invalid input detected at '^' marker.

^
% Invalid input detected at '^' marker.

user@linux:~$ 

期望的输出

hostname Router1
hostname Router2

知道为什么这段代码会这样吗?

【问题讨论】:

    标签: python python-3.x netmiko


    【解决方案1】:

    sh runshow running-config 的缩写;这是一个特权模式命令(首先需要enable),您正尝试在非特权模式下发出它。

    【讨论】:

    • 要访问 Netmiko 中的特权模式,您需要执行 connect.enable()。只要路由器定义了enable secretenable password 命令,就需要特权模式。