I'm working on a small script that'll act as an snmp pass_persist handler. I want it to read a file in (called 'numbers', for now in the same dir) which just contains some integers and return these as an oid tree.
I've been stuck on this for a few days now, and I realise now it's due to a fundamental misunderstanding of how snmpd works. I'm using the snmpd.conf man page which makes no mention of any difference in how 'get' and 'getnext' requests are to be handled, but I assume there is one. I can't for the life of me get snmpwalk to work with this script.
Could someone who knows a little more about snmp look this code over ? I've seen several other versions of pass scripts, including a few in python but I've not been able to see from looking at the code how they handle the protocol differently to my code. I saw one implementation that handled a blank command ( '' ), but others that apparently didn't.
Basically, I'm pretty confused at this point ! - It's also proving pretty hard to debug snmpd as it's the one calling my script, not me. I'm logging what i can, and running snmpd in the foreground, but other than that it's all a bit "black-box".
Can anyone shed some light ?
i.e: numbers file:
101 102 103 I want returned as: .1.3.6.1.4.1..[snip]..1 = 101 .1.3.6.1.4.1..[snip]..2 = 102 .1.3.6.1.4.1..[snip]..3 = 103
My script (I'm not worried about returning anything other than integers, and i know the file close will never be reached, but it makes me feel better):
#!/bin/python -u
import os,sys, syslog
def getLine():
return sys.stdin.readline().strip()
def getFileLine(sub_oid, lines):
sub_oid = int(sub_oid)
if sub_oid >= len(lines):
return 'NONE'
else:
return lines[sub_oid]
def printOutput(oid, var_type, varbind_value):
if varbind_value == 'NONE':
print 'NONE'
else:
print oid
print var_type
print varbind_value
######################################################
sub_oid = 0
FH = open('numbers','r')
lines = FH.readlines()
while True:
command = getLine()
syslog.syslog("command: %s" % command)
if command == 'PING':
syslog.syslog('got a ping')
print 'PONG'
elif command == 'get':
given_oid = getLine()
sub_oid = int(given_oid.split('.')[-1])
varbind_value = getFileLine(sub_oid, lines)
printOutput(given_oid, 'integer', varbind_value.strip())
elif command == 'getnext':
given_oid = getLine()
syslog.syslog("got a requested oid of: %s" % given_oid)
sub_oid = int(given_oid.split('.')[-1])
varbind_value = getFileLine(sub_oid, lines)
printOutput(given_oid, 'integer', varbind_value.strip())
else:
syslog.syslog("Unknown command: %s" % command)
FH.close()