1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#!/usr/local/bin/python -O
"""
Perform SNMP GET request for user specified MIB variables against
multiple SNNP agents at once.
Since MIB parser is not yet implemented in Python, this script takes and
reports Object IDs in dotted numeric representation only.
Written by Ilya Etingof <ilya@glas.net>, 2000-2002.
"""
import sys
import string
import getopt
# Import PySNMP modules
from pysnmp import asn1, v1, v2c
from pysnmp import bulkrole
# Initialize help messages
options = 'Options:\n'
options = options + ' -p <port> port to communicate with at the agent. Default is 161.\n'
options = options + ' -r <retries> number of retries to be used in requests. Default is 5.\n'
options = options + ' -t <timeout> timeout between retries. Default is 1.\n'
options = options + ' -v <version> SNMP version to use [1, 2c]. Default is 1 (version one).\n'
options = options + ' -i <filename> file to read additional args from. Use \'stdin\' for stdin.'
usage = 'Usage: %s [options] <snmp-agent> <community> <obj-id [[obj-id] ... ]\n' % sys.argv[0]
usage = usage + options
# Initialize defaults
port = 161
retries = 5
timeout = 1
version = '1'
input = None
# Parse possible options
try:
(opts, args) = getopt.getopt(sys.argv[1:], 'hp:r:t:v:i:',\
['help', 'port=', 'retries=', \
'timeout=', 'version=', 'input='])
except getopt.error, why:
print 'getopt error: %s\n%s' % (why, usage)
sys.exit(-1)
try:
for opt in opts:
if opt[0] == '-h' or opt[0] == '--help':
print usage
sys.exit(0)
if opt[0] == '-p' or opt[0] == '--port':
port = int(opt[1])
if opt[0] == '-r' or opt[0] == '--retries':
retries = int(opt[1])
if opt[0] == '-t' or opt[0] == '--timeout':
timeout = int(opt[1])
if opt[0] == '-v' or opt[0] == '--version':
version = opt[1]
if opt[0] == '-i' or opt[0] == '--input':
input = opt[1]
except ValueError, why:
print 'Bad parameter \'%s\' for option %s: %s\n%s' \
% (opt[1], opt[0], why, usage)
sys.exit(-1)
if args and len(args) < 3:
print 'Insufficient number of arguments supplied\n%s' % usage
sys.exit(-1)
if not args and not input:
print 'Neither command line nor file arguments supplied\n%s' % usage
sys.exit(-1)
# Create bulk SNMP manager object
client = bulkrole.manager()
# Pass it a few options
client.timeout = timeout
client.retries = retries
# Reset global source of request IDs
serial = 0
# Build SNMP request and submit it to bulk transport from command line args
if args:
try:
req = eval('v'+version).GETREQUEST(request_id=serial,\
community=args[1],\
encoded_oids=\
map(asn1.OBJECTID().encode,\
args[2:]))
except (NameError, AttributeError):
print 'Unsupported SNMP protocol version: %s\n%s' % (version, usage)
sys.exit(-1)
serial = serial + 1
client.append(((args[0], port), req))
# Read additional args from file
if input:
# Can also read from stdin
if input == 'stdin':
f = sys.stdin
else:
try:
f = open(input)
except IOError, why:
print 'open() failed: %s\n%s' % (why, usage)
sys.exit(-1)
while 1:
line = f.readline()
if not line:
break
args = string.split(line)
if len(args) < 3:
print 'Insufficient number of arguments at line: \'%s\'\n%s'\
% (line, usage)
sys.exit(-1)
try:
req = eval('v'+version).GETREQUEST(request_id=serial,\
community=args[1],\
encoded_oids=\
map(asn1.OBJECTID().encode,\
args[2:]))
except (NameError, AttributeError):
print 'Unsupported SNMP protocol version: %s\n%s' %\
(version, usage)
sys.exit(-1)
serial = serial + 1
client.append(((args[0], port), req))
# Run the I/O
client.dispatch()
# Walk over the list of replies
for (src, rsp) in client:
# Handle failed responses
print 'Response from: ' + str(src)
if rsp is None:
print 'Timed out...'
continue
# Decode BER encoded Object IDs.
oids = map(lambda x: x[0], map(asn1.OBJECTID().decode, \
rsp['encoded_oids']))
# Decode BER encoded values associated with Object IDs.
vals = map(lambda x: x[0](), map(asn1.decode, rsp['encoded_vals']))
# Check for remote SNMP agent failure
if rsp['error_status']:
print 'SNMP error #' + str(rsp['error_status']) + ' for OID #' \
+ str(rsp['error_index'])
continue
# Print out results
for (oid, val) in map(None, oids, vals):
print oid + ' ---> ' + str(val)
|