cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
187
Views
10
Helpful
5
Replies
Highlighted
Beginner

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Rising star
Rising star

Re: netmiko can't execute 'sh run | i host'

hmm ok thanks for confirming .. 

I am thinking it could be that .. sh clock and show int bri can be run without privilege mode. and for show runn you need to be in enable mode. does the user have priv 15? can you run any of the enable mode commands? 

you can try - connect.enable()

r1 = {
        'device_type': 'cisco_ios',
        'host':'x.x.x.x',
        'username':'xxx',
        'password': 'xxx',
        'secret': 'xxx'}
...
connect.enable()
connect.send_command('sh run | i host')

 

 

 

View solution in original post

5 REPLIES 5
Highlighted
Rising star
Rising star

Re: netmiko can't execute 'sh run | i host'

the issue is not that netmiko can't execute .. netmiko can send the command and get output 

try to run the command "sh run | i host" on the device first... do you get the desired output?

Screenshot 2020-04-15 at 07.48.26.png

Highlighted
Beginner

Re: netmiko can't execute 'sh run | i host'

No issue with the Router, I already tested the command manually

Highlighted
Rising star
Rising star

Re: netmiko can't execute 'sh run | i host'

hmm ok thanks for confirming .. 

I am thinking it could be that .. sh clock and show int bri can be run without privilege mode. and for show runn you need to be in enable mode. does the user have priv 15? can you run any of the enable mode commands? 

you can try - connect.enable()

r1 = {
        'device_type': 'cisco_ios',
        'host':'x.x.x.x',
        'username':'xxx',
        'password': 'xxx',
        'secret': 'xxx'}
...
connect.enable()
connect.send_command('sh run | i host')

 

 

 

View solution in original post

Highlighted
Rising star

Re: netmiko can't execute 'sh run | i host'

Hi,

Have you tried sending only "show hostname" command? 

 

Regards,

Sergiu

Highlighted
Cisco Employee

Re: netmiko can't execute 'sh run | i host'

The reason is probably because you are not getting into enable mode. The other commands run even without being in enable mode. But show run first needs to be enable mode. Try connect.enable() first to enter enable mode. You can always check the prompt with find_prompt function.