Advertisement
core_st

Cisco configs parser

Mar 13th, 2014
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. """
  2.     Switches configs parser
  3. """
  4.  
  5. import glob
  6. import errno
  7. import os
  8.  
  9. path =   os.getcwd()
  10. files = glob.glob(path+"\sw\*")  
  11.  
  12. allFiles = dict() # read ports status
  13. switchNumbers = ["17","18","20","21","22","23","24","25","27","28","29","31","32"]
  14. switches = dict.fromkeys(switchNumbers,[]) # dict -> key: switch name, value: list with tuples (port, status)
  15.  
  16. for name in files: # 'file' is a builtin type, 'name' is a less-ambiguous variable name.
  17.     try:
  18.         with open(name) as f: # No need to specify 'r': this is the default.
  19.             allFiles[name[name.find("sw")+3:name.find(".txt")]] = f.readlines()
  20.     except IOError as exc:
  21.         if exc.errno != errno.EISDIR: # Do not fail if a directory is found, just ignore it.
  22.             raise # Propagate other kinds of IOError.
  23.  
  24.  
  25. for fileName,content in allFiles.items():
  26.     #print(fileName)
  27.     switches[fileName[0:3]] = [(fileName[0], line[:7].strip(), "connected" in line)
  28.                                 for line in content if line[0] in ["F","G","T"]]
  29.  
  30.  
  31. for switch in switchNumbers:
  32.     print("\n\nSwitch: " + switch + "\n" + "---------------------------")
  33.     for row in range(0,len(switches["1"+switch])):
  34.         print("Port: {0} | status: {1} | no changes: {2}".format(switches["1"+switch][row][1],
  35.             switches["1"+switch][row][2],
  36.             all(switches["1"+switch][row][2] == switches[str(i)+switch][row][2] for i in range(2,6))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement