From 135074e8e5bc04823514460ff54cf3eff62ce2fe Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Sat, 3 Nov 2018 12:01:25 +0100
Subject: [PATCH] added basic snmp walk function

---
 config.py | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/config.py b/config.py
index 7c4cf4b0..21d5ddca 100644
--- a/config.py
+++ b/config.py
@@ -4,9 +4,41 @@ import re
 
 import click
 import mysql.connector
+from pysnmp.hlapi import nextCmd, SnmpEngine, CommunityData, \
+        UdpTransportTarget, ContextData, ObjectType, ObjectIdentity
 
 
 
+def walk(agent_hostname, community, base_oid):
+    """
+    https://stackoverflow.com/a/45001921
+    http://snmplabs.com/pysnmp/docs/hlapi/asyncore/sync/manager/cmdgen/nextcmd.html
+
+    http://snmplabs.com/pysnmp/faq/pass-custom-mib-to-manager.html
+
+    :param agent_hostname:
+    :param community:
+    :param base_oid:
+    :return:
+    """
+    print("walking %s: %s" % (agent_hostname, base_oid))
+    for (engineErrorIndication,
+         pduErrorIndication,
+         errorIndex,
+         varBinds) in nextCmd(
+            SnmpEngine(),
+            CommunityData(community),
+            UdpTransportTarget((agent_hostname, 161)),
+            ContextData(),
+            ObjectType(ObjectIdentity(base_oid)),
+            lexicographicMode=False):
+        assert not engineErrorIndication
+        assert not pduErrorIndication
+        assert errorIndex == 0
+        for oid, val in varBinds:
+            print("\toid: '%s', val: '%s'" % (
+                oid.prettyPrint(), val.prettyPrint()))
+
 
 def _validate_config(ctx, param, value):
     """
@@ -98,7 +130,7 @@ def cli(config):
         with open("routers_community.conf") as f:
             for r in load_routers(f):
                 _db_test(c, r)
-
+                walk(r["hostname"], r["community"], ".1.3.6.1.2.1.4.20.1.1")
 
 if __name__ == "__main__":
     cli()
-- 
GitLab