0

I'm trying to do a SNMP walk with a script based on the examples given by the PySNMP developers.

My code looks like this

from pysnmp.entity.rfc3413.oneliner import cmdgen
from os.path import exists
import sys
import os

# Turn on debugging
#debug.setLogger(debug.Debug('msgproc', 'secmod'))

# Enter parameters
target_IP           = raw_input('Target IP (192.168.13.100): ') or '192.168.13.100'
target_port         = raw_input('Target port (161): ') or 161
target_file_name    = raw_input('Target filename (.txt is added): ') + '.txt' 

# Check for already existing file
path = 'walks/'
if not os.path.exists(path):
    os.makedirs(path)
if exists(path+target_file_name):
    sys.exit("The file '%s' already exists. Try again." % target_file_name)
else: 
    target_file = open(path+target_file_name, 'w+')

# Initialize counter to zero
counter = 0

# Create command generator
cmdGen = cmdgen.CommandGenerator()

# Get data

errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
    cmdgen.CommunityData('public'),
    cmdgen.UdpTransportTarget((target_IP, target_port)),
    '1.3',  # <----------------- doesn't seem perfect either
    lexicographicMode=True, 
    maxRows=5000, # <-------------------- can't leave it out
    ignoreNonIncreasingOid=True,
    lookupNames=True, 
    lookupValues=True
)

# Print errors and values to file
if errorIndication:
    print(errorIndication)
else:
    # Print error messages

    if errorStatus:
        print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
            )
        )
    else:
        # Print values
        for varBindTableRow in varBindTable:
            for name, val in varBindTableRow:
                counter += 1
                target_file.write("(%s)\t start_OID: %s\tvalue =\t%s\n" % (counter, name.prettyPrint(), val.prettyPrint()))

        # Finish the operation                
        target_file.close()
        print('Writing to %s successful. %d lines have been written' % (target_file_name, counter))
        sys.exit(0)

It works quite well now, the only problem is I can't leave out the maxRows paramater. But how do i make it walk "till the end" if I always have to enter a maximum number of rows?

vicco
  • 1,049
  • 2
  • 14
  • 33
  • What happens if you leave out maxRows? I believe pysnmp would walk till the end then. – Ilya Etingof Mar 30 '15 at 10:06
  • It says `No SNMP response received before timeout` and writes nothing to the file. I already tried setting a higher timeout manually, but then I still get the same result. – vicco Mar 30 '15 at 10:43
  • The maxRows flag can't by itself cause timeouts. But letting your script walk till the end may cause it reaching a non-responsive OID what will ultimately lead to timeout. So a workaround would be to increase timeout/retries settings. Or you could enable pysnmp debugging to see where exactly walk fails. – Ilya Etingof Mar 30 '15 at 16:47
  • Thank you, increasing the timeout (a lot) solved the problem. – vicco Mar 31 '15 at 12:40

0 Answers0