使用Netmiko遍历文件,获取交换机ARP表项



我正在尝试使用Netmiko输出交换机的ARP表。我想使用包含IP地址的文件,然后让Python/Netmiko运行"show arp"然后从我提供的文件中添加IP地址。我希望它通过IP地址文件循环显示文件中IP地址的所有ARP项,然后输出到包含IP和MAC地址的文件。以下是我的一个地址,如有任何帮助,将不胜感激:

#!/usr/bin/env python3
#CF extract ARP table and send output as text file
from netmiko import ConnectHandler
from datetime import datetime
import time
import sys
##initializing device
device = {
'device_type': 'hp_comware',
'ip': '10.1.10.10',
'username': 'xxxx',
'password': 'xxxx',
}
start_time = datetime.now()
print (start_time)
net_connect = ConnectHandler(**device)
output = net_connect.send_command("dis arp 172.16.100.100")
time.sleep(2)
filename="test-arp.txt"
saveconfig=open(filename, 'w+')
saveconfig.write(output)
saveconfig.close()
time.sleep(2)
net_connect.disconnect()
end_time = datetime.now()
print (end_time)

使用下面的代码,您可以根据设备的提示,同时在100个设备上非常快速地执行操作(您可以根据需要增加)。

from netmiko import Netmiko
from multiprocessing.dummy import Pool as ThreadPool
import time
f_2 = open("multiple_device_list_cisco.txt","r") # You should open a notepad with this name and add all the IPs one under the other.
multiple_device_list = f_2.readlines()
file1 = open("Result.txt", "a") # this will be your automatic output when the code is finished
def _ssh_(nodeip):
try:
hp = {
'device_type': 'hp_comware', 'ip': nodeip, 'username':
xxxx, 'password': xxxx, 'secret':xxxx, "conn_timeout": 20}
hp_connect = Netmiko(**hp)
print(nodeip.strip() + "  " + "is reachable")
except Exception as e:
print (e)
f_3.write(nodeip.strip() + "n")
return
prompt_hp_fnk = hp_connect.find_prompt()
hostname_fnk = prompt_hp_fnk.strip("#") # Here you should put whatever the prompt of your HP device is
print(hostname_fnk)
output = hp_connect.send_command_timing("dis arp "+ nodeip)
file1.write(nodeip +" "+ output+ "n")
hp_connect.disconnect()
myPool = ThreadPool(100) # you can increase or decrease this value
result = myPool.map(_ssh_,multiple_device_list)

我对上面的代码做了必要的修改。我希望这将是有用的

最新更新