Tshark
Table of Contents
Tshark
Tshark is a powerful command-line network protocol analyzer. It is the terminal-based counterpart of Wireshark and provides a way to capture and analyze network traffic without a graphical user interface. Tshark is highly scriptable and useful for automation, remote analysis, and situations where a GUI is not available or desired.
Core Features
- Live Capture: Captures network traffic in real-time from specified interfaces.
- PCAP Analysis: Reads and analyzes network traffic data stored in PCAP files (and other formats supported by Wireshark).
- Filtering: Supports the same powerful display filters as Wireshark, allowing for precise selection of traffic to view or process.
- Output Formatting: Offers various output formats, including plain text, CSV, JSON, and more, making it suitable for scripting and integration with other tools.
- Protocol Dissection: Leverages the same extensive protocol dissectors as Wireshark, providing detailed decoding of numerous network protocols.
- Statistical Analysis: Can generate various network statistics, such as protocol hierarchies, endpoint lists, and conversation statistics.
- Saving Captured/Filtered Data: Allows saving captured or filtered traffic to new PCAP files.
- Scriptability: Well-suited for use in shell scripts and other automation workflows.
- Remote Capture: Can capture traffic on remote machines using tools like SSH.
Use Cases
- Automated Network Analysis: Scripting traffic capture and analysis for routine tasks or monitoring.
- Remote Network Troubleshooting: Analyzing network issues on servers or headless systems without a GUI.
- Batch Processing of PCAP Files: Analyzing multiple PCAP files programmatically.
- Quick Traffic Inspection: Performing basic traffic analysis directly from the command line.
- Integration with Security Tools: Feeding network traffic data into other security analysis platforms.
- Generating Network Statistics: Quickly obtaining summaries of network activity.
- Testing Network Devices and Protocols: Observing the raw traffic exchanged during testing.
Basic Syntax
tshark [options] [capture_filter] [read_file]
- options: Various command-line flags to control Tshark's behavior (e.g., interface selection, capture duration, output format).
- capturefilter: A Berkeley Packet Filter expression to specify which packets to capture (only used during live capture).
- readfile: The path to a PCAP file to analyze.
Common Options
-i <interface>: Specify the network interface to capture from (e.g., eth0, wlan0).-r <file>: Read packet data from the specified PCAP file.-w <file>: Write captured or filtered packets to a new PCAP file.-T <format>: Set the output format (e.g., pdml, psml, text, fields, json).-q: Be quiet (suppress non-essential output).-z <analysis>: Perform various types of statistical analysis (e.g., -z io,phs).-Y <display_filter>: Apply a Wireshark display filter to show only specific packets during analysis.-c <count>: Capture only the specified number of packets.-n: Disable network object name resolution (faster).-t ad: Display absolute date and time for timestamps.-e <field>: Specify a field to print in -T fields output.
Basic Examples
- Capture live traffic on eth0 and display in default format: `tshark -i eth0
- Read a PCAP file and display packets with HTTP protocol:
tshark -r capture.pcap -Y http - Capture 100 TCP packets on port 80 and save to a file:
sudo tshark -i eth0 -c 100 -f "tcp port 80" -w output.pcap - Read a PCAP file and output only the source and destination IP addresses in plain text:
tshark -r capture.pcap -T fields -e ip.src -e ip.dst - Read a PCAP file and output HTTP request URIs in plain text:
tshark -r capture.pcap -T fields -e http.request.uri -Y http.request - Read a PCAP file and generate protocol hierarchy statistics:
tshark -r capture.pcap -qz io,phs
Key ifferences from Wireshark
- Interface: Tshark is command-line only, while Wireshark has a graphical user interface.
- Interactivity: Wireshark is more interactive for exploration, while Tshark excels in automation and scripting.
- Resource Usage: Tshark can be more lightweight than Wireshark, especially for remote analysis on resource-constrained systems.