chetan6893

plotter

Mar 1st, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. from jsonrpclib import Server
  2. import networkx as nx#
  3. #
  4. #hostnames= {"10.85.128.101":"mt701","10.85.128.102":"mt702","10.85.128.103":"mt703","10.85.128.104":"mt704","10.85.128.105":"mt705","10.85.128.106":"mt706","10.85.128.107":"mt707","10.85.128.108":"mt708","10.85.128.109":"mt709","10.85.128.110":"mt710"}
  5. hostnames= {"10.85.128.101":"mt701","10.85.128.102":"mt702"}
  6. switches=hostnames.keys()
  7. username = ""
  8. password = ""
  9. #
  10. G=nx.MultiGraph()
  11. #
  12. def getlldpinfo(switch_ip):
  13.     urlString = "https://{}:{}@{}/command-api".format(username, password, switch_ip)
  14.     switchReq = Server( urlString )
  15.     response = switchReq.runCmds( 1, ["show lldp neighbors"] )
  16.     neighbors=response[0]["lldpNeighbors"]
  17.     return neighbors
  18.  
  19. def getspeed(interface_name,switch_ip):
  20.     urlString = "https://{}:{}@{}/command-api".format(username, password, switch_ip)
  21.     switchReq = Server( urlString )
  22.     response = switchReq.runCmds( 1, ["show interfaces status"] )
  23.     return response[0]["interfaceStatuses"][interface_name]["bandwidth"]
  24.  
  25. for switch in switches:
  26.     print "Obtaining lldp info for %s" %(switch)
  27.     lldpinfo=getlldpinfo(switch)
  28.     for neighbor in lldpinfo:
  29.         print "Scanning details for neighbor %s" %(neighbor["neighborDevice"])
  30.         localport=neighbor["port"]
  31.         remoteport=neighbor["neighborPort"]
  32.         speedint=getspeed(localport,switch)
  33.         check_key=neighbor["neighborDevice"]+"_"+hostnames[switch]+".blr.aristanetworks.com"+"_"+remoteport+localport
  34.         print "check_key is "+check_key
  35.         if G.has_edge(neighbor["neighborDevice"],hostnames[switch]+".blr.aristanetworks.com",key=check_key)==False:
  36.             add_key=check_key=hostnames[switch]+".blr.aristanetworks.com"+"_"+neighbor["neighborDevice"]+"_"+localport+remoteport
  37.             print "\tAdding edge %s <-----------------------> %s with key %s\n" %(localport,remoteport,add_key)
  38.             G.add_edge(hostnames[switch]+".blr.aristanetworks.com",neighbor["neighborDevice"],port=localport,neighborPort=remoteport,speed=speedint,key=add_key)
  39.        
  40. print G.edges()
  41. nx.write_graphml(G,'network.graphml')
Add Comment
Please, Sign In to add comment