2

I have been given a task to generate SNMP data for printers in a network. I have been able to generate data using snmpwalk in pyscripter.

However i wish to know how to implement the same and display on the web using Django.

This is similar to NMS systems.

The code i am using to generate SNMP data is

from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
    cmdgen.CommunityData('public'),
    cmdgen.UdpTransportTarget(('192.168.1.101', 161)),
    '1.3.6.1.2.1.2.2.1',
)

if errorIndication:
    print(errorIndication)
else:
    if errorStatus:
        print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
        ))
    else:
        for varBindTableRow in varBindTable:
            for name, val in varBindTableRow:
                print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Your question is a bit opaque. I guess you should start implementing a Django view which would call pysnmp (your script above would work) to gather data from printers. And then render collected var-binds (varBindTable) in your Django template. – Ilya Etingof Oct 04 '13 at 11:32
  • I have implemented the above code in views.py and i call it through the urls.py, i searched about implementing/displaying same in the template. Do i need to have forms.py or models.py to display the above output. I am bit confused about the same. I am new to Django and Python, relatively i can understand. – SameerKatti Oct 04 '13 at 12:05
  • Thanks Ilya, i could successfully manage to run the code. But i keep getting error as "The view didn't return Httpresponse". I wish you could help me with the template. Do i have to use Django Tags. – SameerKatti Oct 05 '13 at 06:01
  • I don't think you need models at least for SNMP part of the system. According to Django API, a view should return django.http.HttpResponse instance. Refer to Django documentation for more information. – Ilya Etingof Oct 07 '13 at 08:17
  • I had not used return in the beginning of HttpResponse. It is working now, however i am not able to get all the data generated from SNMP only first data is displayed. I am using **return HttpResponse** in place of **print** and variables. – SameerKatti Oct 07 '13 at 08:24

0 Answers0