Last active
March 18, 2021 21:08
-
-
Save tars01/eaf9bb99b8002de7d4fd5037ee9e5c8e to your computer and use it in GitHub Desktop.
Ping routers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#==================================================================================== | |
#Ping function | |
def ping_func(lan_list, site, site_name_dict, net_connect, results, mgmt_ip): | |
ping_results_list = [] | |
for ip in lan_list: | |
ip_addr = ip | |
remote_site = site_name_dict[ip] | |
if site == remote_site: | |
print ('Remote site: ' + site +' is same as local site :' + remote_site + ',' + ' skipping...') | |
latency = 0 | |
ping_results_list.append(str(latency)) | |
continue | |
print ('Pinging: ' + remote_site + ' on ip:', ip_addr) | |
ping_result = net_connect.send_command('ping ' + ip_addr) | |
for line in ping_result.splitlines(): | |
if 'avg' in line: | |
split_line = line.split('/') | |
latency = split_line[-2] | |
print ('Latency from ' + site + ' to ' + remote_site+ ' is: ', latency) | |
continue | |
if 'Success rate is 0 percent' in line: | |
latency = 'timeout' | |
print ('Latency from ' + site + ' to ' + remote_site+ ' is: ', latency) | |
ping_results_list.append(latency) | |
print (ping_results_list) | |
csv_row = ','.join(ping_results_list) | |
results.write(site + ',' + csv_row + '\n') | |
return | |
#======================================================================= | |
#open device_file to retrieve a list of routers to run discovery on | |
with open ('router_list') as devices: | |
devices_list = devices.read().splitlines() | |
#Create a dict of all of the site IPs and sites | |
lan_list = [] | |
site_list = [] | |
site_name_dict = {} | |
for line in devices_list: | |
device = line.split(',') | |
lan_list.append(device[1]) | |
site_list.append(device[0]) | |
site_name_dict[device[1]] = device[0] | |
#Take site list and turn it into a string to be used as top row of CSV | |
top_row = ', '.join(site_list) | |
results.write(',' + top_row + '\n') #Write the sites to top row of CSV | |
for line in devices_list: | |
device = line.split(',') | |
site = device[0] | |
#lan_ip = device[1] | |
mgmt_ip = device[1] | |
print ('Site is: ', site) | |
#print ('LAN ip is: ', lan_ip) | |
print ('Management IP is: ', mgmt_ip) | |
print ('-----------------------------------') | |
ios_device = { | |
'device_type': 'cisco_ios', | |
'ip': mgmt_ip, | |
'username': username, | |
'password': password, | |
'global_delay_factor': 4, | |
'session_log': 'session_output_ssh' | |
} | |
try: | |
net_connect = ConnectHandler(**ios_device) | |
except (AuthenticationException): | |
**remove for brevity*** | |
#Call ping function, pass in current router details, plus site name | |
ping_func(lan_list, site, site_name_dict, net_connect, results, mgmt_ip) | |
#close results file | |
results.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment