Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import argparse
  4. from netmiko import ConnectHandler
  5. from getpass import getpass
  6. import re
  7. from pprint import pprint
  8.  
  9.  
  10. def main():
  11.  
  12. parser = argparse.ArgumentParser(
  13. description="Find mac-address in mac-address table"
  14. )
  15. parser.add_argument(
  16. "--i",
  17. dest="filename",
  18. required=True,
  19. help="input filename, file should contain ip-address or hostname of the devices that the script should log into",
  20. )
  21. parser.add_argument(
  22. "--status",
  23. choices=('connected', 'notconnect', 'err-disable'),
  24. help="connected, notconnect or err-disable",
  25. )
  26.  
  27. args = parser.parse_args()
  28. username = input("Username: ")
  29. password = getpass()
  30.  
  31. with open(args.filename) as devices:
  32. # Fetch devices from input file
  33. for line in devices:
  34. deviceaddr = line
  35.  
  36. if args.status:
  37. sh_int_status = run_command(
  38. args, deviceaddr, username, password, "show int status"
  39. )
  40. parsed = parse_ports(sh_int_status)
  41. if args.status == "notconnect":
  42. notconnect(parsed)
  43. if args.status == "connected":
  44. connected(parsed)
  45. if args.status == "err-disable":
  46. err-disable(parsed)
  47.  
  48.  
  49.  
  50.  
  51. def run_command(args, deviceaddr, username, password, command):
  52.  
  53. device = {
  54. "device_type": "cisco_ios",
  55. "host": deviceaddr,
  56. "username": username,
  57. "password": password,
  58. }
  59.  
  60. net_connect = ConnectHandler(**device)
  61. hostname = net_connect.find_prompt()
  62. print(hostname)
  63. cmd = net_connect.send_command(command)
  64. return cmd
  65.  
  66.  
  67. def parse_ports(cmd_output):
  68.  
  69. # Split output at newline
  70. # Interfaces start at index [2]
  71. s_status_split = cmd_output.split("\n")
  72. port_rows = s_status_split[2:]
  73.  
  74. list_ports = {}
  75. for p in port_rows:
  76. # Loop is executed on all rows in port_rows
  77. # Split line on whitespaces
  78. # 1. Fetch array index for interface names and split on whitespaces
  79. # 2. Fetch array starting index for port status and split on whitespaces
  80. # 3. Create a dict with port_name as key
  81. port_name = p.split()[0]
  82. port_status = p[29:].split()
  83.  
  84. list_ports[port_name] = {
  85. "status": port_status[0],
  86. "vlan": port_status[1],
  87. "duplex": port_status[2],
  88. "speed": port_status[3],
  89. "type": port_status[4],
  90. }
  91. return list_ports
  92.  
  93.  
  94. def connected(parsed):
  95.  
  96. connected = { k:v for (k,v) in parsed.items() if v["status"] == "connected" }
  97. for key, value in connected.items():
  98. print("{}: {}".format(key, value["status"]))
  99. print("Port with status connected: {}".format(len(connected)))
  100.  
  101. def notconnect(parsed):
  102.  
  103. notconnect = { k:v for k,v in parsed.items() if v["status"] == "notconnect" }
  104. for key, value in notconnect.items():
  105. print("{}: {}".format(key, value["status"]))
  106. print("Ports with status notconnect: {}".format(len(notconnect)))
  107.  
  108.  
  109. def errdisable(parsed):
  110.  
  111. notconnect = { k:v for k,v in parsed.items() if v["status"] == "err-disable" }
  112. for key, value in notconnect.items():
  113. print("{}: {}".format(key, value["status"]))
  114. print("Ports with status err-disable: {}".format(len(notconnect)))
  115.  
  116. if __name__ == "__main__":
  117. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement