forked from mehmet-y/my_snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpysnmp_list_used_and_down_interfaces
105 lines (88 loc) · 4.39 KB
/
pysnmp_list_used_and_down_interfaces
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/home/repos/public/Python/bin/python3.6
#
import re
from datetime import timedelta
from pysnmp.hlapi import *
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def construct_snmp_iterator(device, snmp_oid):
iterator = f"nextCmd(SnmpEngine(), UsmUserData('{snmp_username}', '{snmp_password}'), UdpTransportTarget(('{device}', 161)), ContextData(), ObjectType(ObjectIdentity('{snmp_oid}')), loo
kupMib=False, lexicographicMode=False)"
return eval(iterator)
def snmpwalk(device, iterator):
snmpwalk_response_list = []
for (errorIndication, errorStatus, errorIndex, varBinds) in iterator:
if errorIndication:
print(device, "ERROR: ", errorIndication)
break
elif errorStatus:
print('ERROR STATUS: %s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
break
else:
for varBind in varBinds:
snmp_response = ' = '.join([x.prettyPrint() for x in varBind])
snmpwalk_response_list.append([device, snmp_response])
return snmpwalk_response_list
def create_devices_key_dict(devices):
devices_key_dict = {}
for device in devices:
devices_key_dict[device]=[]
return devices_key_dict
def discover_used_ports_per_description(devices):
print("Discovering all used interfaces based on description and name!")
print()
ifxAlias = "1.3.6.1.2.1.31.1.1.1.18"
ifDescr = "1.3.6.1.2.1.2.2.1.2"
matched_description_ifindexes = create_devices_key_dict(devices)
for device in devices:
iterator1 = construct_snmp_iterator(device, ifxAlias)
iterator2 = construct_snmp_iterator(device, ifDescr)
device_interface_description_list = snmpwalk(device, iterator1)
device_interface_name_list = snmpwalk(device, iterator2)
for desc, name in zip(device_interface_description_list,device_interface_name_list):
description = desc[-1].split("=")[-1].strip()
name = name[-1].split("=")[-1].strip()
ifindex = desc[-1].split("=")[0].split(".")[-1]
if ("UNUSED" not in description) and (description != "") and (("Ether" in name) or ("Port" in name) or ("Tunnel" in name)):
matched_description_ifindexes[device].append(ifindex)
return matched_description_ifindexes
def list_down_interfaces(devices):
ifxAlias = "1.3.6.1.2.1.31.1.1.1.18"
ifDescr = "1.3.6.1.2.1.2.2.1.2"
ifxName = "1.3.6.1.2.1.31.1.1.1.1"
ifOperStatus = "1.3.6.1.2.1.2.2.1.8" # 1 is UP
ifAdminStatus = "1.3.6.1.2.1.2.2.1.7" # 1 is UP
device_and_ifindexes = discover_used_ports_per_description(devices)
#print(device_and_ifindexes)
print("Listing DOWN Interfaces!")
print("If only:")
print(" It is not marked as 'UNUSED' or 'empty' description")
print(" Its name contains 'Ether' or 'Port' or 'Tunnel'")
print("----------------------------------------------------------------------")
for device in devices:
for ifindex in device_and_ifindexes[device]:
operstatus_getCmd_iterator = construct_getCmd_snmp_iterator(device, ifOperStatus + "." + ifindex)
adminstatus_getCmd_iterator = construct_getCmd_snmp_iterator(device, ifAdminStatus + "." + ifindex)
xname_getCmd_iterator = construct_getCmd_snmp_iterator(device, ifxName + "." + ifindex)
interface_operstatus_list = snmpwalk(device, operstatus_getCmd_iterator)
interface_adminstatus_list = snmpwalk(device, adminstatus_getCmd_iterator)
interface_xname_list = snmpwalk(device, xname_getCmd_iterator)
operstatus = interface_operstatus_list[0][-1].split("=")[-1].strip()
adminstatus = interface_adminstatus_list[0][-1].split("=")[-1].strip()
interface = interface_xname_list[0][-1].split("=")[-1].strip()
if adminstatus == "2":
print(f"{device : <35} {interface : <20} is {bcolors.FAIL}Admin DOWN!{bcolors.ENDC}")
elif (adminstatus == "1") and (operstatus != "1"):
print(f"{device : <35} {interface : <20} is {bcolors.FAIL}DOWN!{bcolors.ENDC}")
snmp_username = "<snmp_username>"
snmp_password = "<snmp_password>"
devices = ["device1", "device2"]
list_down_interfaces(devices)