python - 使用 Python 从 Scapy 获取变量

标签 python scapy

您好,我需要帮助尝试访问 scapy 返回的元组中的元素。代码如下。

ans,unans=sr(IP(dst="www.google.com",ttl=5)/ICMP(),verbose=0)
ans.summary(lambda (s,r): r.sprintf("%IP.src%") )

如果我输入 Scapy 并手动键入这两个命令,它将返回一个 IP 地址。但是,我从 Python 脚本内部运行它,并且需要将该 IP 地址存储为变量。这可能是一个愚蠢的问题,我刚刚学习 Scapy,但是如何使用 scapy 将此 IP 地址存储为变量?我想要实现如下所示的目标。

#!/usr/bin/python
from scapy.all import *
ans,unans=sr(IP(dst="www.google.com",ttl=5)/ICMP(),verbose=0)
SourceIP = ans.summary(lambda (s,r): r.sprintf("%IP.src%") )

最佳答案

如果您需要图层的值,请使用 getlayer 方法。所以只需将其更改为:

ans = sr1(IP(dst="www.google.com",ttl=5)/ICMP(),verbose=0)
address = ans.getlayer(IP).src

还有另一种方法可以判断图层是否存在,返回 true 或 false。这是一个例子。

if ans.haslayer(IP):
    print "Packet has the layer IP"

关于python - 使用 Python 从 Scapy 获取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10371310/

相关文章:

python - 工程数据结构/信息检索与存储

python - 生成带有替换的随机整数列表的最佳方法

python - 一点点编程数学

python - 从 MATLAB 调用 Python 函数

python - Scapy - 发送 IPv6 路由器广告失败,生命周期 > 0

python - 如何访问字典列表的索引?

ip - 使用 Scapy 转发具有特定 IP 地址的数据包

python-3.x - 使用 scapy 使用 TCP 进行 DNS 请求

python - 'haslayer' 函数在 scapy-python 中不起作用

python - Scapy:原始 ICMP 数据包没有回复