PyShark is a Python wrapper for TShark, the command-line network protocol analyzer that comes with Wireshark. It allows for Pythonic packet parsing and analysis by leveraging Wireshark's powerful dissection engine. The library is currently at version 0.6 and sees active development with several minor and patch releases per year, addressing compatibility and adding features.
pip install pysharkVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to perform a live packet capture using `pyshark.LiveCapture`. It sniffs 5 packets on a specified network interface (defaulting to 'eth0' or an environment variable) and prints basic information about each packet. It also includes error handling for the common `TShark not found` issue and ensures the capture process is properly closed. Remember to replace 'eth0' with your actual network interface name or set the `PYSHARK_INTERFACE` environment variable.
Upgrade to Python 3.7 or newer. Python 3.7+ is officially supported.
Install Wireshark (which includes TShark) for your operating system and ensure `tshark` is added to your system's PATH environment variable. Verify installation by running `tshark --version` in your terminal.
Migrate your parsing logic to use the EK (Elasticsearch-compatible JSON) mode for improved performance and future compatibility. Enable it by passing `use_ek=True` to your capture object.
Identify the correct NPF interface name for your adapter. You can often find this by running `pyshark.LiveCapture.interfaces()` or checking TShark's output directly. Example: `capture = pyshark.LiveCapture(interface=r'\Device\NPF_{YOUR-ADAPTER-GUID}')`.Run `xcode-select --install` and `pip install libxml` (or `brew install libxml2` if using Homebrew) to resolve potential compilation issues.
If experiencing issues with missing or malformed fields in EK mode, try disabling `include_raw=True` if raw packet data is not strictly required for that specific operation. Check GitHub issues for potential workarounds or updates.
Rename or remove the 'email.py' file in your working directory to avoid the naming conflict.
Avoid pickling PyShark packet objects or passing them through multiprocessing queues; instead, extract necessary data from packets before such operations.
Before accessing 'tcp.analysis_ack_rtt', check if the attribute exists using 'hasattr(packet.tcp, 'analysis_ack_rtt')' to prevent the error.
Ensure that all asynchronous tasks are awaited properly and that the event loop remains open until all tasks are completed; consider using 'asyncio.run()' to manage the event loop.
Install Wireshark (which includes TShark) and ensure that the TShark executable is added to your system's PATH environment variable.