1#!/usr/bin/python 2# 3# Generate SNMPGET request, parse response 4# 5from pyasn1.codec.ber import encoder, decoder 6from pyasn1_modules import rfc1157 7import sys, socket 8 9if len(sys.argv) != 4: 10 print("""Usage: 11$ %s <community> <host> <OID>""" % sys.argv[0]) 12 sys.exit(-1) 13 14msg = rfc1157.Message() 15msg.setComponentByPosition(0) 16msg.setComponentByPosition(1, sys.argv[1]) 17# pdu 18pdus = msg.setComponentByPosition(2).getComponentByPosition(2) 19pdu = pdus.setComponentByPosition(0).getComponentByPosition(0) 20pdu.setComponentByPosition(0, 123) 21pdu.setComponentByPosition(1, 0) 22pdu.setComponentByPosition(2, 0) 23vbl = pdu.setComponentByPosition(3).getComponentByPosition(3) 24vb = vbl.setComponentByPosition(0).getComponentByPosition(0) 25vb.setComponentByPosition(0, sys.argv[3]) 26v = vb.setComponentByPosition(1).getComponentByPosition(1).setComponentByPosition(0).getComponentByPosition(0).setComponentByPosition(3).getComponentByPosition(3) 27 28print('sending: %s' % msg.prettyPrint()) 29 30sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 31sock.sendto(encoder.encode(msg), (sys.argv[2], 161)) 32 33substrate, _ = sock.recvfrom(2048) 34 35rMsg, _ = decoder.decode(substrate, asn1Spec=msg) 36 37print('received: %s' % rMsg.prettyPrint()) 38