All -

I use OpenBSD 4.1 and I use Net-Snmp version : 5.1.3

pgurumur-vm-openbsd (OpenBSD): [~/working/prog/python/snmp]
10.200.0.46: [384]$ net-snmp-config --version
5.1.3

I am trying to write a python module which will do tftp/snmp based backup of 
Cisco routers (this currently works with bourne shell script and I want to 
change that to python).

When I run this script, I get the following error:

pgurumur-vm-openbsd (OpenBSD): [~/working/prog/python/snmp]
10.200.0.46: [385]$ ./snmp.py -s 10.200.0.105 -v 1 -c "SECRET"
error: set: no type found for objectNone
Exception exceptions.TypeError: 'expected string or Unicode object, NoneType 
found' in 'garbage collection' ignored
Fatal Python error: unexpected exception during garbage collection
Abort trap

I know that from test.py python script that I need to set the type to be either 
INTEGER, OCTETSTR, GUAGE, etc depending on the OID. But I dont know how to set 
the type. I even tried types module under python and it still does not help.

Can anybody give me pointers?

Thanks a lot
Prabhu
-

Here is the python script:
#!/usr/bin/env python2.4
# $Id: snmp.py,v 1.11 2007/06/20 00:51:57 pgurumur Exp $

import random, getopt, re, os, struct, sys, netsnmp, time, types

(dirname, program) = os.path.split(sys.argv[0])
argc = len(sys.argv)

class SNMPError(Exception):
    pass

class Snmp:
    def __init__(self, HostName, Version, Community):
       self._snmpError = SNMPError()
       self._host = ""
       self._version = ""
       self._community = ""
       self._snmpSession = 0
       self._count = 0

       if HostName:
          self._host = HostName
       else:
          raise SNMPError, "no hostname provided"

       if Version:
          self._version = Version
       else:
          raise SNMPError, "no version provided"

       if Community:
          self._community = Community
       else:
          raise SNMPError, "no community provided"

       try:
          self._snmpSession = netsnmp.Session(
                Version=int(self._version),
                DestHost=self._host,
                Community=self._community)
       except TypeError, message:
          raise SNMPError, message
       else:
          self._snmpSession.UseEnums = 1
          self._snmpSession.UseLongNames = 1

    def Get(self, Oid, VarId = "0"):
       response = ""
       if Oid:
          vars = netsnmp.VarList(netsnmp.Varbind(Oid, VarId))
          if self._snmpSession:
             retVal = self._snmpSession.get(vars)
             response = retVal[0]

       return response

    def Set(self, Var):
       if Var:
          try:
             retVal = self._snmpSession.set(Var)
          except TypeError, message:
             raise SNMPError, message
          else:
             print retVal

def doBackup(Server, Version, Community):
    if Server and Version and Community:
       try:
          snmp = Snmp(Server, Version, Community)
       except SNMPError, message:
          error("%s: %s" %(program, message))
       else:
          randNum = str(random.randint(0, 65535))

          # var = netsnmp.Varbind(".1.3.6.1.4.1.9.9.96.1.1.1.1.14", randNum, 6)
          varList = netsnmp.VarList(
                netsnmp.Varbind(".1.3.6.1.4.1.9.9.96.1.1.1.1.14", randNum, 6))
                # netsnmp.Varbind(".1.3.6.1.4.1.9.9.96.1.1.1.1.2", randNum, 1),
                # netsnmp.Varbind(".1.3.6.1.4.1.9.9.96.1.1.1.1.5", randNum,
                #    ipAddress),
                # netsnmp.Varbind(".1.3.6.1.4.1.9.9.96.1.1.1.1.6", randNum,
                #    backupFile),
                # netsnmp.Varbind(".1.3.6.1.4.1.9.9.96.1.1.1.1.3", randNum, 4),
                # netsnmp.Varbind(".1.3.6.1.4.1.9.9.96.1.1.1.1.4", randNum, 1),
                # netsnmp.Varbind(".1.3.6.1.4.1.9.9.96.1.1.1.1.14", randNum, 1))

          try:
             snmp.Set(varList)
          except SNMPError, message:
             print "%s: %s" %(program, message)
             sys.exit(1)

def parseCommandLine(Args):
    if Args:
       try:
          opts, args = getopt.getopt(Args, "s:v:c:h",
                ["server=", "version=", "community=", "help"])
       except getopt.GetoptError, message:
          print "%s: %s" %(program, message)
          usage()
       else:

          for optind, optarg in opts:
             if optind in ("--server", "-s"):
                Server = optarg
             elif optind in ("--version", "-v"):
                Version = optarg
             elif optind in ("--community", "-c"):
                Community = optarg
             elif optind in ("--help", "-h"):
                usage()
             else:
                usage()

          doBackup(Server, Version, Community)

def usage():
    global program
    print "%s: options" %program
    print "options: "
    print "        --server    | -s [ name of the host ]"
    print "        --version   | -v [ snmp version to use (1, 2c only) ]"
    print "        --community | -c [ snmp community ]"
    print "        --help      | -h [ prints this help ]"
    sys.exit(1)

def error(Message):
    if Message:
       global program
       print "%s: %s" %(program, Message)
       sys.exit(1)

if __name__ == "__main__":
    if (argc <= 1):
       usage()
    else:
       parseCommandLine(sys.argv[1:])


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to