Getting variables from Scapy using Python
Question: 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%") )
    


Answer: 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"
    

