Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

python - netmiko can't execute 'sh run | i host'

I notice that my netmiko code can't run sh run | i host which is a legitimate Cisco command.

When I replace sh run with other command such as sh clo, or show ip interface brief, it works perfectly.

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)

Output

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

^
% Invalid input detected at '^' marker.

user@linux:~$ 

Desired Output

hostname Router1
hostname Router2

Any idea why this code behave this way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

sh run is short for show running-config; this is a privileged-mode command (requires enable first) and you're trying to issue it in unprivileged mode.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...