Created
March 17, 2015 09:38
-
-
Save tzermias/90d4295f9d1f92a93499 to your computer and use it in GitHub Desktop.
A simple script that parses data from the show "mac address-table" command of Dell Poweredge 5548 switch, and updates a specific network switch object in Racktables.
This file contains 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
#! /usr/bin/env python | |
""" racktables.py | |
Simple script that parses data from 'show mac address-table' command of Dell | |
Poweredge 5548 switch, and updates the Racktables DB accordingly | |
[email protected] | |
""" | |
import MySQLdb | |
# Replace DB credentials with your own. | |
host='localhost' | |
user='racktables_user' | |
passwd='racktables_password' | |
port=3306 | |
DB='racktables' | |
# Replace these as well | |
filename = 'test-table' | |
remote_object_id = 214 | |
def insert_link(c,mac_addr,object_id,port_name): | |
""" insert_link | |
Insert a link between the object containing a MAC address specified in | |
mac_addr with the ID of the remote object in object_id (must previously know the | |
object_id of the object.) | |
Port name is the name of the port (that will be created) in the remote | |
object. | |
""" | |
#Strip semicolons from the mac_addr | |
mac_addr=mac_addr.replace(':','') | |
lines = c.execute('select id from Port where l2address=%s', (mac_addr,)) | |
if lines == 0: | |
print "No object with the MAC address %s has been found!" % mac_addr | |
return 1 | |
local_obj = c.fetchone()[0] | |
#Check if a name with port_name exists on the remote object, or insert it. | |
remote_port_id=0 | |
if c.execute(""" select id from Port where object_id=%s and name=%s """, | |
(object_id,port_name)) == 0: | |
#Insert a new port with port_name and get the port_name id | |
c.execute(""" insert into Port (object_id,name,iif_id,type) values | |
(%s,%s,1,24) """, (object_id, port_name) ) | |
c.execute(""" select id from Port where object_id=%s and name=%s """, | |
(object_id,port_name)) | |
remote_port_id = c.fetchone()[0] | |
else: | |
remote_port_id = c.fetchone()[0] | |
#Make the connection between the two IDs | |
print "Connecting port %s" % port_name | |
c.execute(""" insert ignore into Link (porta,portb,cable) values (%s,%s,'') | |
""", (local_obj,remote_port_id)) | |
def main(): | |
#filename = 'mac-address-table' | |
# Connect to DB | |
db=MySQLdb.connect(host=host,user=user,passwd=passwd,port=port,db=DB) | |
c=db.cursor() | |
#TEST | |
#insert_link(c,'90:1B:0E:15:4B:E9',211,'gi1/0/39') | |
with open(filename,'r') as f: | |
for line in f.readlines(): | |
# Works only if input contains 2 columns with the first containing | |
# the MAC address and the second contains the port name. | |
#FIXME: Use re instead | |
line = line.replace('\n','') | |
columns = line.split(' ') | |
# print columns | |
insert_link(c, columns[0], remote_object_id, columns[1]) | |
db.commit() | |
c.close() | |
db.close() | |
if __name__ == '__main__': | |
main() | |
# vi: ts=4 sts=4 et sw=4 tw=80 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment