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
>>>
Python 3 Cleaner:
#print hotname without #
hostname = net_connect.find_prompt()[:-1]
print(hostname)
# print output without the word "hostname"
output = net_connect.send_command('sh run | i host')
for line in output:
if "hostname" in line:
print(line.strip("hostname"))
print(line)
find_prompt by default should return you either hostnem# (privileged) or hostname> - it's the whole idea behind it. As this is just a string, you can find a way around it like:
output = net_connect.find_prompt()
output = output.replace('#','')
print(output )
or
output = net_connect.send_command('sh run | i host'))
output = output.split()
hostname = output[1]
print(hostname)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.