cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
8561
Views
11
Helpful
4
Replies

Python Netmiko to get hostname

write_erase
Level 1
Level 1

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

Seb Rupik
VIP Alumni
VIP Alumni

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

4 Replies 4

Seb Rupik
VIP Alumni
VIP Alumni

Hi there,

Try the following:

 

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

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

cheers,

Seb.

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

There is no "show hostname" on 3725

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

R1#
R1#show host?
hosts  

R1#

I run 'show version | in uptime' which is way more faster than parsing running config.