簡體   English   中英

Python Netmiko 獲取主機名

[英]Python Netmiko to get hostname

我能夠使用以下 netmiko 代碼獲取設備主機名。

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

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

是否可以從 output 中刪除#hostname

所需 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 清潔劑:

#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 默認應該返回 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM