22# @Author: JanKinCai
33# @Date: 2019-09-03 09:50:27
44# @Last Modified by: jankincai
5- # @Last Modified time: 2024-09-26 11:50:36
5+ # @Last Modified time: 2025-01-08 11:12:08
66import argparse
77
88from pylibpcap .base import Sniff
@@ -47,6 +47,7 @@ def pylibpcap_sniff():
4747 parser .add_argument ("-i" , "--iface" , type = str , help = "Iface" , required = True )
4848 parser .add_argument ("-c" , "--count" , type = int , default = - 1 , help = "Capture packet num" )
4949 parser .add_argument ("-m" , "--promisc" , type = int , default = 0 , help = "Promiscuous mode" )
50+ parser .add_argument ("-t" , "--timeout" , type = int , default = 0 , help = "Set pcap handler timeout." )
5051 parser .add_argument ("filter" , nargs = "*" , type = str , help = "BPF filter rules" )
5152 parser .add_argument ("-o" , "--output" , type = str , help = "Output pcap file" )
5253 parser .add_argument ("-v" , "--view" , action = "store_true" , help = "Show Packet Info" )
@@ -59,11 +60,12 @@ def pylibpcap_sniff():
5960
6061 try :
6162 sniffobj = Sniff (iface = args .iface , count = args .count , promisc = args .promisc ,
62- filters = " " .join (args .filter ), timeout = 1000 , out_file = args .output )
63+ filters = " " .join (args .filter ), timeout = args . timeout , out_file = args .output )
6364
6465 for plen , t , buf in sniffobj .capture ():
65-
66- if args .view and plen :
66+ if plen == 0 :
67+ raise Exception (f"Capture timeout ({ args .timeout } )" )
68+ elif args .view :
6769 num += 1
6870 print (num , Packet (buf , plen ).to_string (args .view_payload ))
6971 except KeyboardInterrupt :
0 commit comments