【发布时间】:2014-07-30 15:54:36
【问题描述】:
我是 Python 新手,在使用 Scapy 进行 ICMP ping 扫描时遇到了 lambda 函数的使用。我已经阅读了一些教程,但我很难理解这个概念以正确利用我的代码。
我试图弄清楚 lambda 参数如何准确引用 Scapy 给出的输出;主要是 's' 和 'r' 的功能以及它们如何使用 '%IP.src%' 直接从 IP 字段中提取数据?
def icmp_ping(target_ip):
"Function for a classic ICMP Ping sweep."
ans,unans=sr(IP(dst=target_ip)/ICMP(), timeout=3)
ans.summary(lambda (s,r): r.sprintf("%IP.src% is alive") )
ans.summary(lambda (s,r): r.sprintf("%IP.ttl% is the TTL value") )
print(ans)
它给出了输出:
Received 3 packets, got 1 answers, remaining 10 packets
192.168.0.100 is alive
64 is the TTL value
[(<IP frag=0 proto=icmp dst=192.168.0.100 |<ICMP |>>, <IP version=4L ihl=5L tos=0x0 len=28 id=30394 flags= frag=0L ttl=64 proto=icmp chksum=0x820d src=192.168.0.100 dst=192.168.0.101 options=[] |<ICMP type=echo-reply code=0 chksum=0xffff id=0x0 seq=0x0 |<Padding load='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |>>>)]
希望这个问题有意义!
【问题讨论】:
-
似乎我已经设法弄清楚了一些。 lambda 功能等同于
codefor s,r in ans:code其中 s = 发送和 r= 接收。然后使用 sprintf 拉出需要的数据。
标签: python lambda network-programming scapy