In [1]:
import easysnmp
In [15]:
session = easysnmp.Session(hostname='localhost', community='public', version=2,
timeout=1, retries=1, use_sprint_value=True)
# IMPORTANT: use_sprint_value=True for proper formatting of values
In [16]:
location = session.get('sysLocation.0')
location.oid, location.oid_index, location.snmp_type, location.value
Out[16]:
In [17]:
iftable = session.walk('IF-MIB::ifTable')
In [18]:
for item in iftable:
print(item.oid, item.oid_index, item.snmp_type, item.value, type(item.value))
The type of .value is always a Python string but the string returned in .snmp_type can be used to convert to the correct Python type.
In [19]:
macaddress = session.get('IF-MIB::ifPhysAddress.2')
macaddress
Out[19]:
In [20]:
ifindex = session.get('IF-MIB::ifIndex.2')
ifindex
Out[20]:
In [22]:
import easysnmptable
In [23]:
session = easysnmptable.Session(hostname='localhost', community='public', version=2,
timeout=1, retries=1, use_sprint_value=True)
# IMPORTANT: use_sprint_value=True for proper formatting of values)
In [24]:
iftable = session.gettable('IF-MIB::ifTable')
In [25]:
iftable.indices
Out[25]:
In [26]:
iftable.cols
Out[26]:
In [27]:
iftable
Out[27]:
In [28]:
import pprint
for index,row in iftable.rows.items():
pprint.pprint(index)
pprint.pprint(row)
A short-coming of easysnmptable is that it does not return the snmp_type for columns. A possible work-around is to use easysnmp to fetch a single column for a random index. This should probably be memoized or cached.
In [72]:
random_index = iftable.indices.pop()
iftable.indices.add(random_index)
random_index
Out[72]:
In [76]:
column2type = {column: session.get('{}.{}'.format(column, random_index)).snmp_type for column in iftable.cols}
column2type
Out[76]:
Such a table could be build by "walking" a device.
In [82]:
column2type = {item.oid: item.snmp_type for item in session.walk('IF-MIB::ifTable')}
column2type
Out[82]:
In [ ]: