tshark (1/2)
TShark is the terminal version of Wireshark: it captures live or reads a file and decodes every packet with the full Wireshark dissectors. That makes it ideal for servers without a GUI and for scripts (JSON, CSV). It ships with Wireshark + Npcap on Windows, as the tshark package on Linux, and with Wireshark.app or brew on macOS. Lab: client 192.168.144.10, server 192.168.144.20 (DNS + HTTP for shop.lab).
124.1 Why is it used?
- Wireshark-level decoding (3001 protocols) on a headless server.
- Display filters on any protocol field (http.request, dns.qry.name), not just host/port.
- Machine-readable output for scripts and pipelines: fields/CSV, JSON, EK (Elasticsearch), PDML/PSML.
- Safe, bounded captures with autostop and ring buffers.
124.2 How it works
- Step 1
1.Client 192.168.144.10 sends a DNS query through the bridge
- Step 2
2.tshark on eth0 gets a copy of every packet
- Step 3
3.1. Capture filter (-f, BPF) decides whether to keep the copy
- Step 4
4.2. Dissectors decode every layer: Ethernet, IP, UDP, DNS
- Step 5
5.3. Display filter (-Y) picks the packets to show
- Step 6
6.4. Output: summary, -V tree, fields, JSON, or -w file
124.3 Capture interface and settings
- -D lists interfaces with numbers (also extcap ones like sshdump, ciscodump, udpdump).
- -L lists link types; -i picks the interface (repeat -i for several; -i - for stdin).
- -f sets a BPF capture filter; -c 6 stops after six packets.
- -s snap length, -B buffer (MiB), -p no promiscuous, -I Wi-Fi monitor mode.
tshark -Dtshark -i eth0 -Ltshark -i eth0 -c 6 -f "host 192.168.144.20" &sleep 2dig +short @192.168.144.20 shop.labcurl -s -H "Host: shop.lab" http://192.168.144.20/wait$ tshark -D1. eth02. any3. lo (Loopback)4. nflog5. nfqueue6. ciscodump (Cisco remote capture)9. sshdump (SSH remote capture)10. udpdump (UDP Listener remote capture)11. wifidump (Wi-Fi remote capture)$ tshark -i eth0 -c 6 -f "host 192.168.144.20"Capturing on 'eth0' 1 0.000000000 a2:b0:80:b3:82:4a → Broadcast ARP 42 Who has ... 2 0.000015710 a2:b0:80:b3:82:4a → Broadcast ARP 42 Who has ... 3 0.000043728 c2:e2:33:2f:a4:26 → a2:b0:80:b3:82:4a ARP 42 ... 4 0.000047230 192.168.144.10 → 192.168.144.20 DNS 91 Standard query 0x871c A shop.lab OPT 5 0.000618496 192.168.144.20 → 192.168.144.10 DNS 84 Standard query response ... 6 0.053178224 192.168.144.10 → 192.168.144.20 TCP 74 35514 → 80 [SYN] ...124.4 Autostop and ring buffer
- -a duration:4 stops after 4 seconds; -a filesize:2 stops at 2 kB (unit is kB).
- -a files:N and -a packets:N are the other stop conditions.
- -q hides the live counter and prints only the total.
- -b packets:10 -b files:3: new file every 10 packets, keep only the last 3 files.
- -b duration / interval (on the clock) / filesize; nametimenum sets number or time first in the name.
tshark -i eth0 -q -a duration:4 -w /tmp/dur.pcapngcapinfos -c -u /tmp/dur.pcapng | tail -2tshark -i eth0 -q -a filesize:2 -w /tmp/size.pcapngcapinfos -c -s /tmp/size.pcapng | tail -2$ tshark -i eth0 -q -a duration:4 -w /tmp/dur.pcapngCapturing on 'eth0'12 packets captured$ capinfos -c -u /tmp/dur.pcapng | tail -2Number of packets: 12Capture duration: 0.824376471 seconds$ tshark -i eth0 -q -a filesize:2 -w /tmp/size.pcapngCapturing on 'eth0'14 packets captured$ capinfos -c -s /tmp/size.pcapng | tail -2Number of packets: 14File size: 2232 bytesmkdir -p /tmp/ringping -q -c 40 -i 0.1 192.168.144.20 > /dev/null &tshark -i eth0 -q -b packets:10 -b files:3 \ -a duration:5 -w /tmp/ring/web.pcapngls /tmp/ringCapturing on 'eth0'60 packets captured$ ls /tmp/ringweb_00005_20261109031559.pcapngweb_00006_20261109031600.pcapngweb_00007_20261109031600.pcapng- Step 1
1.60 packets captured in 5 seconds
- Step 2
2.Every 10 packets a new file starts: web_00001, web_00002 ...
- Step 3
3.When file 4 opens, file 1 is deleted
- Step 4
4.At the end only web_00005, web_00006, web_00007 remain
- Step 5
5.Disk use never grows beyond 3 files
124.5 Input and output files
- -w writes a file; default format is pcapng. -r reads one (gz/zstd also ok).
- -P prints the summary even while writing with -w.
- -F pcap converts to classic pcap; -F alone lists formats.
- --export-objects http,DIR pulls files sent over HTTP/SMB out of the capture.
tshark -i eth0 -q -a duration:6 \ -f "host 192.168.144.20" -w /tmp/web.pcapng &tshark -r /tmp/web.pcapng -c 6tshark -i eth0 -c 4 -P -w /tmp/p.pcapng -f icmptshark -r /tmp/web.pcapng -F pcap -w /tmp/web.pcapcapinfos -t /tmp/web.pcap | tail -128 packets captured$ tshark -r /tmp/web.pcapng -c 6 1 0.000000000 192.168.144.10 → 192.168.144.20 DNS 91 Standard query ... 2 0.001329987 192.168.144.20 → 192.168.144.10 DNS 84 Standard query response ... 3 0.053274376 192.168.144.10 → 192.168.144.20 TCP 74 52338 → 80 [SYN] ... 4 0.053399907 192.168.144.20 → 192.168.144.10 TCP 74 80 → 52338 [SYN, ACK] ... 5 0.053531644 192.168.144.10 → 192.168.144.20 TCP 66 52338 → 80 [ACK] ... 6 0.053775399 192.168.144.10 → 192.168.144.20 HTTP 137 GET / HTTP/1.1$ tshark -r /tmp/web.pcapng -F pcap -w /tmp/web.pcap$ capinfos -t /tmp/web.pcap | tail -1File type: Wireshark/tcpdump/... - pcap124.6 Display filters and two-pass analysis
- -Y uses Wireshark display filter syntax; with -w only the matching packets are saved.
- In one pass dns.response_in of the query is empty, because the reply has not been read yet.
- -2 runs a second pass, so frame 1 shows the response is in frame 2; -R is the first-pass filter.
- A trailing filter after -r is treated as a display filter, so BPF syntax ('udp port 53') errors out.
tshark -r /tmp/web.pcapng -Y "http.request"tshark -r /tmp/web.pcapng -Y "dns" -T fields \ -e frame.number -e dns.response_intshark -r /tmp/web.pcapng -2 -R "dns" -T fields \ -e frame.number -e dns.response_intshark -r /tmp/web.pcapng udp port 53tshark -r /tmp/web.pcapng "udp.port==53"$ tshark -r /tmp/web.pcapng -Y "http.request" 6 0.053775399 192.168.144.10 → 192.168.144.20 HTTP 137 GET / HTTP/1.1$ tshark -r ... -Y "dns" -T fields -e frame.number -e dns.response_in1 2 $ tshark -r ... -2 -R "dns" -T fields -e frame.number -e dns.response_in1 22 $ tshark -r /tmp/web.pcapng udp port 53tshark: "port" was unexpected in this context. Note: That read filter code looks like a valid capture filter; maybe you mixed them up?$ tshark -r /tmp/web.pcapng "udp.port==53" 1 0.000000000 192.168.144.10 → 192.168.144.20 DNS 91 Standard query ... 2 0.001329987 192.168.144.20 → 192.168.144.10 DNS 84 Standard query response ...124.7 Output detail, formats and fields
- -V prints the full detail tree; -O dns expands only DNS and keeps other layers on one line.
- -x adds a hex + ASCII dump (GET / and Host: shop.lab are readable).
- -T json / ek / pdml / psml / fields / tabs; -j or -J limit which protocols appear in JSON.
- -T fields -e x -e y gives one line per packet with tab-separated values.
- -E header=y -E separator=, -E quote=d turns it into CSV that opens in a spreadsheet.
tshark -r /tmp/web.pcapng -Y "dns.flags.response==1" -O dnstshark -r /tmp/web.pcapng -Y "http.request" -xFrame 2: 84 bytes on wire (672 bits), 84 bytes captured ...Ethernet II, Src: c2:e2:33:2f:a4:26 (c2:e2:33:2f:a4:26), Dst: ...Internet Protocol Version 4, Src: 192.168.144.20, Dst: 192.168.144.10User Datagram Protocol, Src Port: 53, Dst Port: 52993Domain Name System (response) Transaction ID: 0x4aff Flags: 0x8180 Standard query response, No error Answers shop.lab: type A, class IN, addr 192.168.144.200000 c2 e2 33 2f a4 26 a2 b0 80 b3 82 4a 08 00 45 00 ..3/.&.....J..E.0040 b8 ce 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 ..GET / HTTP/1.10050 0d 0a 48 6f 73 74 3a 20 73 68 6f 70 2e 6c 61 62 ..Host: shop.labtshark -r /tmp/web.pcapng -Y "dns" -T json -j "dns"tshark -r /tmp/web.pcapng -Y "dns.flags.response==0" -T ektshark -r /tmp/web.pcapng -Y "dns.flags.response==0" -T pdmltshark -r /tmp/web.pcapng -Y "http" -T psml[ { "_index": "packets-2026-11-09", "_source": { "layers": { ... "dns": { "dns.id": "0x4aff", "dns.flags": "0x0120", "dns.count.queries": "1",{"index":{"_index":"packets-2026-11-09","_type":"doc"}}{"timestamp":"1794194167169","layers":{"udp":{"udp_udp_time_delta":...<pdml version="0" creator="wireshark/4.2.4" ... <proto name="dns" showname="Domain Name System (query)" size="49" pos="42"> <field name="dns.qry.name" showname="Name: shop.lab" size="10" ...<psml version="0" creator="wireshark/4.2.4"><packet><section>6</section><section>0.053775399</section>tshark -r /tmp/web.pcapng -Y "dns or http.request" -T fields \ -e frame.number -e ip.src -e ip.dst -e _ws.col.Protocoltshark -r /tmp/web.pcapng -Y "dns or http.request" -T fields \ -E header=y -E separator=, -E quote=d \ -e frame.number -e dns.qry.name -e http.host -e http.request.uri1 192.168.144.10 192.168.144.20 DNS2 192.168.144.20 192.168.144.10 DNS6 192.168.144.10 192.168.144.20 HTTPframe.number,dns.qry.name,http.host,http.request.uri"1","shop.lab",,"2","shop.lab",,"6",,"shop.lab","/"124.8 Timestamp display (-t, -u)
| Option | Shows | Lab value (frame 3) |
|---|---|---|
| -t r (default) | Seconds since the first packet | 0.053274376 |
| -t ad | Local date + time | 2026-11-09 03:16:07.222628959 |
| -t d | Delta from previous captured packet | 0.051944389 |
| -t dd | Delta from previous displayed packet | (not run) |
| -t e | Epoch seconds | 1794194167.222628959 |
| -t u / ud | UTC time / UTC date + time | (compare servers across time zones) |
| -t ad.3 | Precision cut to 3 decimals | 2026-11-09 03:16:07.222 |
| -u hms | Show in h/m/s units | 0.053274376s |
124.9 Name resolution and Decode As
- -n turns all name resolution off; -N t maps ports to service names (http(80)).
- -N dn uses names learned from captured DNS replies, so the destination shows shop.lab.
- -d tcp.port==7000,http decodes non-standard-port traffic as HTTP (Decode As).
- -o tls.keylog_file:keys.log decrypts TLS; --disable-protocol speeds up big files.
tshark -r /tmp/web.pcapng -Y "tcp.flags.syn==1" -N ttshark -r /tmp/web.pcapng -Y "http.request" -N dntshark -r /tmp/web.pcapng \ -Y "tcp.port==7000 and tcp.len>0" \ -d tcp.port==7000,http$ tshark -r ... -Y "tcp.flags.syn==1" -N t 4 0.053399907 192.168.144.20 → 192.168.144.10 TCP 74 http(80) → 52338 [SYN, ACK] ... 16 0.074720048 192.168.144.20 → 192.168.144.10 TCP 74 afs3-fileserver(7000) → 34250 [SYN, ACK] ...$ tshark -r ... -Y "http.request" -N dn 6 0.053775399 192.168.144.10 → shop.lab HTTP 137 GET / HTTP/1.1$ tshark -r ... -Y "tcp.port==7000 and tcp.len>0" (without -d) 18 0.074946184 192.168.144.10 → 192.168.144.20 TCP 148 34250 → 7000 [PSH, ACK] Seq=1 Ack=1 Win=64512 Len=82 ...$ tshark -r ... -d tcp.port==7000,http 18 0.074946184 192.168.144.10 → 192.168.144.20 HTTP 148 GET / HTTP/1.1 20 0.081745878 192.168.144.20 → 192.168.144.10 TCP 251 HTTP/1.0 200 OK [TCP segment of a reassembled PDU] 22 0.082079426 192.168.144.20 → 192.168.144.10 HTTP 87 HTTP/1.0 200 OK (text/html)124.10 Glossaries (-G)
- -G prints an internal list and exits: protocols (3001), fields (251524), decodes, folders, plugins, manuf, services.
- -G fields dns. narrows to the 334 DNS fields; these are the names for -Y and -e.
tshark -G protocols | wc -ltshark -G fields | wc -ltshark -G fields dns. | wc -ltshark -G fields | grep -wE "dns\.qry\.name|dns\.response_in"$ tshark -G protocols | wc -l3001$ tshark -G fields | wc -l251524$ tshark -G fields dns. | wc -l334$ tshark -G fields | grep -wE ...F Name dns.qry.name FT_STRING dns 0x0 Query NameF Name Length dns.qry.name.len FT_UINT16 dns BASE_DEC 0x0 Query Name LenF Response In dns.response_in FT_FRAMENUM dns 0x0 The response to this DNS query is in this frame124.11 Comparison: -f vs -Y vs -R/-2
| Point | -f capture filter | -Y display filter | -2 -R two-pass |
|---|---|---|---|
| Syntax | BPF (host, port) | Wireshark fields (dns, http.request) | Wireshark fields |
| When | Before saving/dissecting | After dissection | First pass, then second pass |
| Reduces live load | Yes | No, only the output | No |
| Can use any field | No | Yes | Yes, plus future fields like dns.response_in |
| Works on -r files | No | Yes | Yes |
124.12 Common mistakes
- Thinking -Y makes a live capture lighter: every packet is still captured and dissected; use -f.
- Putting BPF syntax after -r or in -Y.
- Open-ended production captures: always add -c, -a or a -b ring.
- Expecting dns.response_in in a single pass: use -2.
124.13 Interview tips
- -f = BPF, before saving, fast; -Y = Wireshark syntax, after dissection, any field.
- -2 enables future references such as response_in.
- DNS query names to CSV: -r web.pcapng -Y "dns.flags.response==0" -T fields -e dns.qry.name -E header=y -E separator=,
- Use -T ek/json to feed captures into Elasticsearch or other tools.