Getting variables from Scapy using Python

a 夏天 提交于 2021-01-28 19:14:36

问题


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%") )

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%") )

回答1:


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"


来源:https://stackoverflow.com/questions/10371310/getting-variables-from-scapy-using-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!