From e5ab25f3617f0603a26a2b44bc161495e354b50c Mon Sep 17 00:00:00 2001 From: Clinton Thomas <1033162+KernelClint@users.noreply.github.com> Date: Tue, 1 Sep 2026 13:29:01 -0400 Subject: [PATCH] snmp: keep printing results after an ICMP answer AI-Assisted: yes (GPT-5.6-Cyber) --- scapy/layers/snmp.py | 2 +- test/scapy/layers/snmp.uts | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/scapy/layers/snmp.py b/scapy/layers/snmp.py index 713a65a46b8..8952bd345b8 100644 --- a/scapy/layers/snmp.py +++ b/scapy/layers/snmp.py @@ -308,7 +308,7 @@ def snmpget(dst, oid="1.0.8802.1.1.1.1.1.2.1.2.29", community="public"): for r in ans: if ICMP in r.answer: print(repr(r.answer)) - return + continue print("[%-10s] %-40s: %r" % ( r.query.dst, r.answer[SNMPvarbind].oid.val, diff --git a/test/scapy/layers/snmp.uts b/test/scapy/layers/snmp.uts index b281a4dd5b3..85c53f03c67 100644 --- a/test/scapy/layers/snmp.uts +++ b/test/scapy/layers/snmp.uts @@ -38,6 +38,34 @@ assert SNMP in z x = UDP()/SNMP() assert x.sport == x.dport == 161 += snmpget continues after an ICMP result +~ SNMP ASN1 + +from types import SimpleNamespace +from scapy.layers import snmp as snmp_module + +query_one = IP(dst="192.0.2.1")/UDP()/SNMP() +query_two = IP(dst="192.0.2.2")/UDP()/SNMP() +icmp_answer = IP(src="192.0.2.1")/ICMP(type=3, code=3) +snmp_answer = IP(src="192.0.2.2")/UDP()/SNMP( + PDU=SNMPresponse( + varbindlist=[SNMPvarbind(oid=ASN1_OID("1.4"), value=ASN1_STRING(b"two"))], + ), +) +answers = [ + SimpleNamespace(query=query_one, answer=icmp_answer), + SimpleNamespace(query=query_two, answer=snmp_answer), +] +saved_sr = snmp_module.sr +snmp_module.sr = lambda *args, **kwargs: (answers, []) +try: + with ContextManagerCaptureOutput() as output: + snmp_module.snmpget(["192.0.2.1", "192.0.2.2"]) +finally: + snmp_module.sr = saved_sr + +assert "192.0.2.2" in output.get_output() + = Basic SNMPvarbind build ~ SNMP ASN1 x = SNMPvarbind(oid=ASN1_OID("1.3.6.1.2.1.1.4.0"), value=RandBin())