Skip to content

Commit 175275b

Browse files
committed
snmp: add snmpget function
AI-Assisted: no
1 parent 5a869ef commit 175275b

1 file changed

Lines changed: 43 additions & 2 deletions

File tree

scapy/layers/snmp.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from scapy.asn1.asn1 import ASN1_Class_UNIVERSAL, ASN1_Codecs, ASN1_NULL, \
1616
ASN1_SEQUENCE
1717
from scapy.asn1.ber import BERcodec_SEQUENCE
18-
from scapy.sendrecv import sr1
18+
from scapy.sendrecv import sr, sr1
1919
from scapy.volatile import RandShort, IntAutoTime
2020
from scapy.layers.inet import UDP, IP, ICMP
2121

@@ -287,10 +287,51 @@ def answers(self, other):
287287
bind_layers(UDP, SNMP, sport=161, dport=161)
288288

289289

290+
def snmpget(dst, oid="1.0.8802.1.1.1.1.1.2.1.2.29", community="public"):
291+
"""
292+
SNMP get.
293+
294+
This can be used to perform a SNMP scan::
295+
296+
>>> snmpget("192.168.0.0/16", community="public")
297+
"""
298+
ans, _ = sr(
299+
IP(dst=dst) / UDP(sport=RandShort()) / SNMP(
300+
community=community,
301+
PDU=SNMPnext(varbindlist=[SNMPvarbind(oid=oid)]),
302+
),
303+
timeout=2,
304+
chainCC=1,
305+
verbose=0,
306+
retry=2,
307+
)
308+
for r in ans:
309+
if ICMP in r.answer:
310+
print(repr(r.answer))
311+
return
312+
print("[%-10s] %-40s: %r" % (
313+
r.query.dst,
314+
r.answer[SNMPvarbind].oid.val,
315+
r.answer[SNMPvarbind].value,
316+
))
317+
318+
290319
def snmpwalk(dst, oid="1", community="public"):
320+
"""
321+
SNMP walk
322+
"""
291323
try:
292324
while True:
293-
r = sr1(IP(dst=dst) / UDP(sport=RandShort()) / SNMP(community=community, PDU=SNMPnext(varbindlist=[SNMPvarbind(oid=oid)])), timeout=2, chainCC=1, verbose=0, retry=2) # noqa: E501
325+
r = sr1(
326+
IP(dst=dst) / UDP(sport=RandShort()) / SNMP(
327+
community=community,
328+
PDU=SNMPnext(varbindlist=[SNMPvarbind(oid=oid)]),
329+
),
330+
timeout=2,
331+
chainCC=1,
332+
verbose=0,
333+
retry=2,
334+
)
294335
if r is None:
295336
print("No answers")
296337
break

0 commit comments

Comments
 (0)