Created
March 20, 2021 21:10
-
-
Save tars01/f815fcda9d082ad1f6015744c241743d to your computer and use it in GitHub Desktop.
Dataframe
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
#Library definitions | |
from netmiko import ConnectHandler | |
from getpass import getpass | |
import multiprocessing | |
from functools import partial | |
from netmiko.ssh_exception import NetMikoTimeoutException | |
from netmiko.ssh_exception import NetMikoAuthenticationException | |
import sys | |
import pandas as pd | |
import logging #for netmiko debugging | |
#debugging loging file for netmiko, once in production comment these out as passwords are displayed in logs | |
logging.basicConfig(filename='debug-thread', level=logging.DEBUG) | |
logger = logging.getLogger("netmiko") | |
#Function which is sending IOS commands | |
def job(username, password, ipaddr): | |
ips = open('iplist.txt', 'r') | |
allips = ips.read().splitlines() | |
rtr = { | |
'device_type': 'cisco_ios_ssh', | |
'ip': ipaddr, | |
'username': username, | |
'password': password, | |
} | |
try: | |
device = ConnectHandler(**rtr) | |
for ip in allips: | |
print ('------------', ip) | |
if ip == ipaddr: | |
print ('Current router and ping dest same, skipping...') | |
latency = 0 | |
df.at[ipaddr, ip] = latency | |
else: | |
print ('In loop for ', ipaddr , ' and pinging', ip) | |
ping = device.send_command('ping ' + str(ip)) | |
# print(ping) | |
for line in ping.splitlines(): | |
if 'avg' in line: | |
split_line = line.split('/') | |
# print 'now printing line', split_line | |
latency = split_line[-2] | |
print('Latency from ' + ipaddr + ' to ' + ip + ' is: ', latency) | |
df.at[ipaddr, ip] = latency | |
continue | |
if 'Success rate is 0 percent' in line: | |
latency = 'timeout' | |
print('Latency from ' + ipaddr + ' to ' + ip + ' is: ', latency) | |
df.at[ipaddr, ip] = latency | |
device.disconnect() | |
except (NetMikoTimeoutException): | |
print("Device timed out " + ipaddr + "\n") | |
except (NetMikoAuthenticationException): | |
print("Authetication failed for device " + ipaddr + "\n") | |
#Multitasking to improve script runtime | |
def parallel_runs(ipaddr, inside_ipaddr): | |
pool = multiprocessing.Pool(processes=1) | |
newjob=partial(job, username, password) | |
result = pool.map(newjob, ipaddr) | |
#Main Loop | |
if __name__ == '__main__': | |
# username = input('Username: ') | |
# password = getpass() | |
username = 'admin' | |
password = 'admin' | |
iplist = open('iplist.txt','r') | |
ipaddr = iplist.read().splitlines() | |
inside_ipaddr = ipaddr | |
df = pd.DataFrame(columns=ipaddr, index=ipaddr) | |
# print (df) | |
parallel_runs(ipaddr, inside_ipaddr) | |
iplist.close() | |
print(df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment