Hi

i am having a code to check the input and output rates of ports on few cisco mds switches.
i will be saving yesterday and today values to two separate csv files.
I am trying to write a python code where it will check the difference in values and will give me updated sheet with the difference

my csv files look like:

yest.csv
10.5.137.231,port-channel14,0,0
10.5.137.231,port-channel21,100,2
10.5.137.232,port-channel14,0,0
10.5.137.232,port-channel21,102,2

today.csv
10.5.137.231,port-channel14,0,0
10.5.137.231,port-channel21,151,2
10.5.137.232,port-channel14,0,0
10.5.137.232,port-channel21,153,2

Please help me with the python coding on how to get difference.csv file

Recommended Answers

All 3 Replies

sorry here is my code to get my port channel utilization reports:
import paramiko
import csv,os
import time, re

ip = ['10.5.137.231','10.5.137.232']
port = []
rate = []
ratein = dict()
rateout = dict()
port1 = dict()
errorips_list = {}
my_dict = dict()

def ssh_to_switch(ip):
command = "show port-ch su"
username = "admin"
password = ""

connecting to a switch

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip, username=username, password=password)
print("Successful Connected to switch: %s" %(ip))

passing commands to switch after successful connection

stdin, stdout, stderr = ssh_client.exec_command(command)

get the port channel number from the commands output and updating in dictionary

stdout1 = stdout.readlines()
for line in stdout1:
if line.startswith('port'):
port.append(line)
for p in port:
a = p.split()
port1[a[0]] = a[1]

passing the seconf command to switch and get the switch counter

command2 = "show inter port-ch %s counters br " % (a[1])
stdin, stdout, stderr = ssh_client.exec_command(command2)
stdout2 = stdout.readlines()
for line in stdout2:
if line.startswith('port'):
out = line.split()
ratein[out[0]] = out[1]
rateout[out[0]] = out[3]
print("I/p Rate MB/s: ",ratein,'\n',"O/p Rate MB/s: ",rateout)
time.sleep(3)
ssh_client.close()

def main():
with open('C:/Users/kasar/PycharmProjects/Cisco_Fabric/out.csv', 'r') as fp, open('C:/Users/kasar/PycharmProjects/Cisco_Fabric/hello.csv', 'w',newline='') as out:
reader = csv.reader(fp)
writer = csv.writer(out)
for row in reader:
if row:
writer.writerow(row)
with open('C:/Users/kasar/PycharmProjects/Cisco_Fabric/out.csv', 'w',newline='') as csvfile:
field = ['SwitchName','Portchannel', 'Input-Rate(MB/s)', 'Outout-Rate(MB/s)']
writer = csv.DictWriter(csvfile, fieldnames=field)
writer.writeheader()
for hostip in ip:
ssh = ssh_to_switch(hostip)
print(20 * "#--#")
for key, values in ratein.items() and rateout.items():
writer.writerow({'SwitchName':hostip,'Portchannel': key, 'Input-Rate(MB/s)': ratein[key], 'Outout-Rate(MB/s)': rateout[key]})
ratein.clear()
rateout.clear()
port.clear()
rate.clear()
compare()

main()

I have to write a funciton "Compare()". i checked multiple sites and i see that we can easily do that using pandas, but i am not able to install pandas module due to restriction in my work place.
trying to figure out the logic on how do i get to compare and get difference between two columns in two sheets.

Since you have workplace restrictions, deal with that first. That is, if that insist you work while handcuffed, why do this? Just do it like you have been and get paid more. Yes, I think companies that impede developers deserve to be called out.

Long ago a workplace did this so I zero'd out the IT budget from our projects. In the next year after a lot of complaints from IT and many meetings it was settled. If IT doesn't deliver, we don't pay them. Later we brought in our own PCs and moved farther away from the company IT. Enough on that story.

You may want to practice using the DW editor to put code in proper formatting.

-> Back to Compare(). I'd be guessing what the function does. Tell exactly what it does to avoid GIGO.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.