Chuyển đến nội dung

Bài 29 · Statistics and Automation

tshark: Wireshark on the Command Line

Use tshark, Wireshark's command-line tool: capture to files, read pcaps, apply filters, extract fields to CSV, and build quick statistics for scripts and servers.

tshark is the command-line sibling of Wireshark. It uses the same dissectors and the same filter syntax, but it prints text, which makes it perfect for servers without a screen, for scripts, and for processing large files quickly. If you know Wireshark, you already know most of tshark.

tshark cannot run in the browser, so every command below is read-only with example output. Where a real capture is not needed, the Python blocks mimic what a command does so you can see the logic. Capture only on networks you are authorized to monitor, and note that capturing normally needs elevated privileges.

Basic commands

Terminal
tshark -D                      # list capture interfaces
tshark -i eth0                 # capture live from an interface, print packets
tshark -i eth0 -c 100          # stop after 100 packets
tshark -r capture.pcapng       # read a file
tshark -i eth0 -w out.pcapng   # write to a file instead of printing

Two kinds of filters, just like in the GUI:

Option Filter type Example
-f Capture filter (BPF), applied while capturing -f "host 10.0.0.5 and port 443"
-Y Display filter, applied while reading -Y "tcp.analysis.retransmission"

Mixing them up is the most common beginner error: -Y only works on packets already captured (or in a read file), and -f uses the simpler BPF syntax.

Terminal
tshark -r capture.pcapng -Y "http.request" 
TEXT
   12   0.412   10.0.0.20 -> 93.184.216.34 HTTP 421 GET /index.html HTTP/1.1
   47   1.903   10.0.0.20 -> 93.184.216.34 HTTP 388 GET /style.css HTTP/1.1

Choosing output fields

Use -T fields with -e for each field you want. Add -E header=y -E separator=, for CSV.

Terminal
tshark -r capture.pcapng -Y "dns.flags.response == 1" \
  -T fields -e frame.time_relative -e dns.qry.name -e dns.flags.rcode \
  -E header=y -E separator=,
TEXT
frame.time_relative,dns.qry.name,dns.flags.rcode
0.014,cdn.example.com,0
0.020,typo.exampel.com,3

Find field names in Wireshark by selecting a field in the packet details: the status bar shows its name. tshark -G fields lists everything (a very long list; pipe it to grep).

Python Simulate -T fields -e on sample packets

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Statistics from the shell

The -q option suppresses packet output, and -z asks for a statistic:

Terminal
tshark -r capture.pcapng -q -z io,phs          # protocol hierarchy
tshark -r capture.pcapng -q -z conv,tcp        # TCP conversations
tshark -r capture.pcapng -q -z endpoints,ip    # IP endpoints
tshark -r capture.pcapng -q -z io,stat,1       # packets and bytes per second
tshark -r capture.pcapng -q -z http,tree       # HTTP request/response summary
tshark -r capture.pcapng -q -z expert          # expert information
TEXT
| Interval |  Frames |  Bytes |
| 0  <> 1  |     412 | 391204 |
| 1  <> 2  |     388 | 366019 |

These correspond to the windows in Statistics: Conversations, Endpoints and I/O Graphs.

Pipelines with other tools

Because tshark prints plain text, regular Unix tools can post-process it:

Terminal
# Top 5 DNS names queried
tshark -r capture.pcapng -Y "dns.flags.response == 0" -T fields -e dns.qry.name \
  | sort | uniq -c | sort -rn | head -5

# Distinct TLS server names
tshark -r capture.pcapng -Y "tls.handshake.type == 1" -T fields \
  -e tls.handshake.extensions_server_name | sort -u
Python The same pipeline in Python (count, sort, top 3)

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Output formats for tools

  • -T json and -T jsonl print the full dissection as JSON, which you can parse in any language (it is large).
  • -T ek prints Elasticsearch bulk format.
  • -T pdml prints XML.
  • -V prints the full packet details in text, like the middle pane of the GUI.
  • -x adds a hex and ASCII dump.

Long-running captures

Terminal
# Keep 10 files of 100 MB each, overwriting the oldest (ring buffer)
tshark -i eth0 -b filesize:102400 -b files:10 -w ring.pcapng

# Stop after 10 minutes
tshark -i eth0 -a duration:600 -w tenmin.pcapng

-a sets an autostop condition, and -b sets ring buffer rules. See Large Captures and Ring Buffers for more.

Practical tips

  • Use -n to turn off name resolution, which makes output faster and avoids sending your own DNS lookups.
  • Use -t ad for absolute timestamps with dates.
  • If a protocol uses a non-standard port, tell tshark how to decode it: -d tcp.port==8081,http.
  • Do not run tshark with root longer than needed. On Linux, set up the capture permissions group so a normal user can capture, and never keep a root capture running forever on a shared system.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which option gives tshark a display filter?

    -Y is the display filter used when reading packets; -f is the capture filter in BPF syntax.
  2. 2. How do you extract only the DNS query name and response code as CSV?

    -T fields with -e options chooses columns, and -E controls the separator.
  3. 3. What does -q combined with -z conv,tcp do?

    -q is quiet mode, so only the requested statistics are printed.
  4. 4. What does '-b filesize:102400 -b files:10' create?

    Ring buffer options switch to a new file after 100 MB and delete the oldest once ten exist.

Summary

  • tshark shares Wireshark's dissectors and display filter syntax.
  • -f is a capture filter, -Y a display filter, -T fields -e selects columns, -z prints statistics.
  • Plain-text output chains neatly with sort, uniq, and scripts.
  • Ring buffers and autostop options make long captures safe.

Next: Parsing PCAP Files with Python

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.