0

我能够使用以下 netmiko 代码获取设备主机名。

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

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

是否可以从输出中删除#hostname

期望的输出

>>> 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
>>> 
4

2 回答 2

1

Python 3 清洁器:

#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)
于 2021-04-06T22:42:12.930 回答
0

find_prompt 默认应该返回 hostnem#(特权)或 hostname> - 这是它背后的全部想法。由于这只是一个字符串,您可以找到一种解决方法,例如:

output = net_connect.find_prompt()
output = output.replace('#','')
print(output )

或者

output = net_connect.send_command('sh run | i host')) 
output = output.split()
hostname = output[1]
print(hostname)
于 2020-05-26T11:22:19.007 回答