• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# This file is part of pyasn1-modules software.
4#
5# Copyright (c) 2005-2017, Ilya Etingof <etingof@gmail.com>
6# License: http://pyasn1.sf.net/license.html
7#
8# Generate SNMPGET request, parse response
9#
10import socket
11import sys
12
13from pyasn1.codec.ber import decoder
14from pyasn1.codec.ber import encoder
15
16from pyasn1_modules import rfc1157
17
18if len(sys.argv) != 4:
19    print("""Usage:
20$ %s <community> <host> <OID>""" % sys.argv[0])
21    sys.exit(-1)
22
23msg = rfc1157.Message()
24msg.setComponentByPosition(0)
25msg.setComponentByPosition(1, sys.argv[1])
26# pdu
27pdus = msg.setComponentByPosition(2).getComponentByPosition(2)
28pdu = pdus.setComponentByPosition(0).getComponentByPosition(0)
29pdu.setComponentByPosition(0, 123)
30pdu.setComponentByPosition(1, 0)
31pdu.setComponentByPosition(2, 0)
32vbl = pdu.setComponentByPosition(3).getComponentByPosition(3)
33vb = vbl.setComponentByPosition(0).getComponentByPosition(0)
34vb.setComponentByPosition(0, sys.argv[3])
35v = vb.setComponentByPosition(1).getComponentByPosition(1).setComponentByPosition(0).getComponentByPosition(0).setComponentByPosition(3).getComponentByPosition(3)
36
37print('sending: %s' % msg.prettyPrint())
38
39sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
40sock.sendto(encoder.encode(msg), (sys.argv[2], 161))
41
42substrate, _ = sock.recvfrom(2048)
43
44# noinspection PyRedeclaration
45rMsg, _ = decoder.decode(substrate, asn1Spec=msg)
46
47print('received: %s' % rMsg.prettyPrint())
48