使用 Python 从 Scapy 获取变量 [英] Getting variables from Scapy using Python
问题描述
我需要帮助尝试访问 scapy 返回的元组中的元素.代码如下.
Hi I need help trying to access elements in what seems to be a tuple that scapy is returning. The code is below.
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 地址存储为变量?我想要实现以下目标.
If I enter Scapy and type both of these commands manually it will return a single IP address. However I am running it from inside a Python script and need to store that IP address as a variable. This may be a stupid question and I am just learning Scapy, but how would I store this IP address as a variable using scapy? I am wanting to achieve something like the below.
#!/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 方法.所以只需将事情更改为:
If you ever need the value of a layer use the getlayer method. So just change things to:
ans = sr1(IP(dst="www.google.com",ttl=5)/ICMP(),verbose=0)
address = ans.getlayer(IP).src
还有另一种方法来判断一个层是否存在返回真或假.这是一个例子.
There is also another method to if a layer exists that returns true or false. Here is an example of that one.
if ans.haslayer(IP):
print "Packet has the layer IP"
这篇关于使用 Python 从 Scapy 获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!