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

Python Netmiko to get hostname

I was able to get device hostname with the following netmiko code.

>>> print(net_connect.find_prompt())
Cisco#
>>>

>>> print(net_connect.send_command('show running-config | include hostname'))
hostname Cisco
>>>

Would it be possible to remove `#` and `hostname` from the output?

Desired Output

>>> print(net_connect.find_prompt()) <= need to do something here
Cisco
>>>

>>> print(net_connect.send_command('sh run | i host')) <= need to do something here
Cisco
>>>
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
VIP Advisor

Re: Python Netmiko to get hostname

Hi there,

Try the following:

 

>>> print(net_connect.find_prompt()[:-1])

>>> print(net_connect.send_command('sh run | i host').split()[1])

cheers,

Seb.

View solution in original post

3 REPLIES 3
Highlighted
VIP Advisor

Re: Python Netmiko to get hostname

Hi there,

Try the following:

 

>>> print(net_connect.find_prompt()[:-1])

>>> print(net_connect.send_command('sh run | i host').split()[1])

cheers,

Seb.

View solution in original post

Highlighted

Re: Python Netmiko to get hostname

Hi,

Instead of parsing the running-config (which means additional execution time for your script until the running-config is collected), why not getting the hostname directly using 'show hostname' command? 

print(net_connect.send_command('show hostname'))

Cheers,

Sergiu

Highlighted
Beginner

Re: Python Netmiko to get hostname

There is no "show hostname" on 3725

R1#show hostname
            ^
% Invalid input detected at '^' marker.

R1#
R1#show host?
hosts  

R1#