Files
CursoPythonDavidBombal/m04_networking/pyshark_example.py
Antonio Torres Míguez 8456ae9c2a ultimos cambios
2021-01-11 11:19:10 +01:00

40 lines
1.5 KiB
Python

import pyshark
# CAPTURE EVERYTHING AND PRINT PACKET SUMMARIES
print("\n----- Packet summaries --------------------")
capture = pyshark.LiveCapture(interface='enp0s3', only_summaries=True)
capture.sniff(packet_count=10)
for packet in capture:
print(f" {packet}")
# CAPTURE DNS AND PRINT PACKETS
print("\n----- DNS packet summaries (collect for 50 packets) --------------------")
capture = pyshark.LiveCapture(interface='enp0s3', only_summaries=True, display_filter='dns')
capture.sniff(packet_count=50)
for packet in capture:
print(f" {packet}")
# CAPTURE ONLY DNS AND PRINT COMPLETE PACKETS
print("\n\n----- DNS packets, complete (collect for 40 total packets) ---------------------")
capture = pyshark.LiveCapture(interface='enp0s3', display_filter='dns')
capture.sniff(packet_count=40)
for packet in capture:
print(packet)
# CAPTURE AND HANDLE PACKETS AS THEY ARRIVE
print("\n\n----- Print packets as they are detected ---------------------")
capture = pyshark.LiveCapture(interface='enp0s3', only_summaries=True, bpf_filter='tcp port https')
def print_packet(pkt):
print(" ", pkt)
capture.apply_on_packets(print_packet, packet_count=20)
# CAPTURE AND HANDLE PACKETS AS THEY ARRIVE USING LAMBDA
print("\n\n----- Print packets as they are detected (lambda version) ---------------------")
capture = pyshark.LiveCapture(interface='enp0s3', only_summaries=True, bpf_filter='tcp port https')
capture.apply_on_packets(lambda pkt: print("lambda ", pkt), packet_count=20)