Question

Sory for my bad english. Perhaps issue has been resolved, but unfortunately I have not found a solution for my question. In general, there was a problem with the task. Can someone help me? Using scapy and netinfo, I need create functionality which sends ping requests to "8.8.8.8" host out of the default network interface in the system (something like 'ethX', where X is number) and verifies that the requests have been sent by capturing outgoing packets.

with this step, I partly understood:

#!/usr/bin/python

import sys 
from scapy.all import *
import netinfo
class test: 
    host = "8.8.8.8" 

    def pingh(self): 
        self.host 
        pkt = Ether()/IP(dst=self.host,ttl=(1,3))/ICMP() 
        ans,unans = srp(pkt,iface="eth0",timeout=2) 
        ans.summary(lambda (s,r): r.sprintf("%Ether.src% %IP.src%") ) 

r = test()
print "request from ping " 
r.pingh() 

but at the next step I was stuck:

Do the same for 'lo' and 'ethX' interfaces simultaneously (use standard 'threading' module). Captured results should be collected into dictionary which has the following structure: {'iface1': list_of_captured_packets, 'iface2': list_of_captured_packets, ...}. Modification of this dictionary should be thread-safe. Modify the test class by adding a test which checks that resulting dictionary contains both - 'lo' and 'ethX' interfaces as keys. P. S. Don't let me die a fool :)

Was it helpful?

Solution

The following uses the threading module to do two parallel ping tests, one on each of two interfaces. For future work, use the multiprocessing module with Pool() and imap_unordered() -- it's a lot easier.

# INSTALL:
#   sudo apt-get install python-scapy
# RUN:
#   sudo /usr/bin/python ./pping.py

import sys, Queue, threading
from scapy import all as S

IFACE_LIST = 'wlan0','lo'


# pylint:disable=E1101
def run_ping(iface, out_q):
    host = '8.8.8.8'
    pkt = S.Ether()/S.IP(dst=host, ttl=(1,3))/S.ICMP() 
    ans,_unans = S.srp(pkt, iface=iface, timeout=2) 
    out_q.put( (iface,ans) )


result_q = Queue.Queue()
for iface in IFACE_LIST:
    threading.Thread(
        target=run_ping, args=(iface, result_q)
    ).start()

for t in threading.enumerate():
    if t != threading.current_thread():
        t.join()

print 'result:', dict( [
    result_q.get()
    for _ in range(result_q.qsize())
    ] )

Output:

Begin emission:
Begin emission:
..Finished to send 3 packets.
*Finished to send 3 packets.
...**
Received 5 packets, got 3 answers, remaining 0 packets
....................
Received 23 packets, got 0 answers, remaining 3 packets
result: {'lo': <Results: TCP:0 UDP:0 ICMP:0 Other:0>, 'wlan0': <Results: TCP:0 UDP:0 ICMP:3 Other:0>}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top