-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnmp.py
41 lines (35 loc) · 1.1 KB
/
snmp.py
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
import logging
from pysnmp.hlapi import *
def snmp_get(host, oid):
for errorIndication, errorStatus, errorIndex, varBinds in getCmd(
SnmpEngine(),
CommunityData("public", mpModel=0),
UdpTransportTarget((host, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid)),
lookupMib=False,
lexicographicMode=False,
):
if errorIndication:
logging.error(errorIndication)
break
elif errorStatus:
logging.error(
"%s at %s"
% (
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
)
)
break
else:
for varBind in varBinds:
return varBind
# Get SNMP info from router
def snmp_get_hostname(ip):
snmp_host_name = "1.3.6.1.2.1.1.5.0"
data = snmp_get(ip, snmp_host_name)
if data is not None:
return data[1].prettyPrint()
logging.error("Could not get SNMP Hostname")
raise ValueError("Could not get SNMP Hostname")