[scripts] add live capture for sniffer.py on simulation traffic (#5327)

This commit enhances sniffer.py so that we can use it as a standalone
sniffer for non Virtual Time simulations.
This commit is contained in:
Simon Lin
2020-08-03 09:25:14 -07:00
committed by GitHub
parent be288331ac
commit 5c6c798576
2 changed files with 42 additions and 11 deletions
+1
View File
@@ -80,6 +80,7 @@ class PcapCodec(object):
pkt = self.encode_frame(frame, *timestamp)
self._pcap_file.write(pkt)
self._pcap_file.flush()
return pkt
def __del__(self):
self._pcap_file.close()
+41 -11
View File
@@ -31,6 +31,9 @@ import collections
import io
import logging
import os
import sys
import time
import pcap
import threading
import traceback
@@ -62,6 +65,9 @@ class Sniffer:
self._message_factory = message_factory
self._pcap = pcap.PcapCodec(os.getenv('TEST_NAME', 'current'))
if __name__ == '__main__':
sys.stdout.buffer.write(self._pcap.encode_header())
sys.stdout.buffer.flush()
# Create transport
transport_factory = sniffer_transport.SnifferTransportFactory()
@@ -81,19 +87,27 @@ class Sniffer:
while self._thread_alive.is_set():
data, nodeid = self._transport.recv(self.RECV_BUFFER_SIZE)
self._pcap.append(data)
pkt = self._pcap.append(data)
if __name__ == '__main__':
try:
sys.stdout.buffer.write(pkt)
sys.stdout.flush()
except BrokenPipeError:
self._thread_alive.clear()
break
# Ignore any exceptions
try:
messages = self._message_factory.create(io.BytesIO(data))
self.logger.debug("Received messages: {}".format(messages))
for msg in messages:
self._buckets[nodeid].put(msg)
if self._message_factory is not None:
try:
messages = self._message_factory.create(io.BytesIO(data))
self.logger.debug("Received messages: {}".format(messages))
for msg in messages:
self._buckets[nodeid].put(msg)
except Exception as e:
# Just print the exception to the console
print("EXCEPTION: %s" % e)
traceback.print_exc()
except Exception as e:
# Just print the exception to the console
self.logger.error("EXCEPTION: %s" % e)
traceback.print_exc()
self.logger.debug("Sniffer stopped.")
@@ -115,7 +129,7 @@ class Sniffer:
self._transport.close()
self._thread.join()
self._thread.join(timeout=1)
self._thread = None
def set_lowpan_context(self, cid, prefix):
@@ -140,3 +154,19 @@ class Sniffer:
messages.append(bucket.get_nowait())
return message.MessagesSet(messages)
def run_sniffer():
sniffer = Sniffer(None)
sniffer.start()
while sniffer._thread_alive.is_set():
try:
time.sleep(1)
except KeyboardInterrupt:
break
sniffer.stop()
if __name__ == '__main__':
run_sniffer()