Sniff packets and return a list of packets. Args: count: number of packets to capture. 0 means infinity. store: whether to store sniffed packets or discard them prn: function to apply to each packet. If something is returned, it is displayed.
| 1085 | |
| 1086 | |
| 1087 | class AsyncSniffer(object): |
| 1088 | """ |
| 1089 | Sniff packets and return a list of packets. |
| 1090 | |
| 1091 | Args: |
| 1092 | count: number of packets to capture. 0 means infinity. |
| 1093 | store: whether to store sniffed packets or discard them |
| 1094 | prn: function to apply to each packet. If something is returned, it |
| 1095 | is displayed. |
| 1096 | --Ex: prn = lambda x: x.summary() |
| 1097 | session: a session = a flow decoder used to handle stream of packets. |
| 1098 | --Ex: session=TCPSession |
| 1099 | See below for more details. |
| 1100 | filter: BPF filter to apply. |
| 1101 | lfilter: Python function applied to each packet to determine if |
| 1102 | further action may be done. |
| 1103 | --Ex: lfilter = lambda x: x.haslayer(Padding) |
| 1104 | offline: PCAP file (or list of PCAP files) to read packets from, |
| 1105 | instead of sniffing them |
| 1106 | quiet: when set to True, the process stderr is discarded |
| 1107 | (default: False). |
| 1108 | timeout: stop sniffing after a given time (default: None). |
| 1109 | L2socket: use the provided L2socket (default: use conf.L2listen). |
| 1110 | opened_socket: provide an object (or a list of objects) ready to use |
| 1111 | .recv() on. |
| 1112 | stop_filter: Python function applied to each packet to determine if |
| 1113 | we have to stop the capture after this packet. |
| 1114 | --Ex: stop_filter = lambda x: x.haslayer(TCP) |
| 1115 | iface: interface or list of interfaces (default: None for sniffing |
| 1116 | on the default interface). |
| 1117 | monitor: use monitor mode. May not be available on all OS |
| 1118 | started_callback: called as soon as the sniffer starts sniffing |
| 1119 | (default: None). |
| 1120 | |
| 1121 | The iface, offline and opened_socket parameters can be either an |
| 1122 | element, a list of elements, or a dict object mapping an element to a |
| 1123 | label (see examples below). |
| 1124 | |
| 1125 | For more information about the session argument, see |
| 1126 | https://scapy.rtfd.io/en/latest/usage.html#advanced-sniffing-sniffing-sessions |
| 1127 | |
| 1128 | Examples: synchronous |
| 1129 | >>> sniff(filter="arp") |
| 1130 | >>> sniff(filter="tcp", |
| 1131 | ... session=IPSession, # defragment on-the-flow |
| 1132 | ... prn=lambda x: x.summary()) |
| 1133 | >>> sniff(lfilter=lambda pkt: ARP in pkt) |
| 1134 | >>> sniff(iface="eth0", prn=Packet.summary) |
| 1135 | >>> sniff(iface=["eth0", "mon0"], |
| 1136 | ... prn=lambda pkt: "%s: %s" % (pkt.sniffed_on, |
| 1137 | ... pkt.summary())) |
| 1138 | >>> sniff(iface={"eth0": "Ethernet", "mon0": "Wifi"}, |
| 1139 | ... prn=lambda pkt: "%s: %s" % (pkt.sniffed_on, |
| 1140 | ... pkt.summary())) |
| 1141 | |
| 1142 | Examples: asynchronous |
| 1143 | >>> t = AsyncSniffer(iface="enp0s3") |
| 1144 | >>> t.start() |
no outgoing calls
no test coverage detected
searching dependent graphs…