SkillPilotAI

tcpflow, tcptrack, iptstate, arpwatch

Four small Linux monitoring tools: tcpflow saves each direction of a TCP connection into its own file (or prints it), tcptrack shows live per-connection speed, iptstate shows the kernel's conntrack table, and arpwatch tracks IP-MAC pairs and reports changes. All install with apt and need root for live use; on macOS only tcpflow (brew); on Windows use WSL. Lab: mon 192.168.149.10, web 192.168.149.20, new 192.168.149.30.

129.1 Why is it used?

  • tcpflow: read the actual application data (HTTP requests, bodies) instead of packets.
  • tcptrack: find which connection is eating bandwidth, live.
  • iptstate: see what the firewall/NAT is tracking (states, timeouts).
  • arpwatch: detect new devices, MAC changes and ARP spoofing on a LAN.

129.2 How it works

tcpflow and tcptrack on eth0
  1. 1.mon sends GET /hello.txt through the Docker bridge to web

  2. 2.web returns 200 OK with the body

  3. 3.tcpflow on eth0 reassembles each direction and writes it to its own file

  4. 4.tcptrack on the same eth0 shows state and speed of every connection live

iptstate and arpwatch: no packet capture
  1. 1.Kernel conntrack keeps one entry per tracked connection

  2. 2.Each entry has a state (ESTABLISHED, TIME_WAIT) and a TTL until it expires

  3. 3.iptstate reads this table; it does not sniff packets

  4. 4.arpwatch listens only to ARP and stores IP-MAC pairs in arp.dat

  5. 5.A new pair -> 'new station'; a changed MAC -> 'changed ethernet address' / 'flip flop'

129.3 tcpflow: live capture and console output

  • -i eth0 captures live; a pcap filter (port 80) limits what is reassembled; -r reads a pcap.
  • -c prints content with a src-dst header; -C prints content only; -D hex dump; -g colours client/server.
  • Flow names: source IP.port-destination IP.port, zero-padded (192.168.149.010.38996).
Terminal
timeout 6 tcpflow -i eth0 -c port 80 &curl -s -o /dev/null http://192.168.149.20/hello.txttimeout 6 tcpdump -i eth0 -w /tmp/web.pcap tcp port 80 &for f in hello.txt hello.txt missing.txt; do  curl -s -o /dev/null -w "%{http_code} $f\n" \    http://192.168.149.20/$f; done
Output
tcpflow: listening on eth0192.168.149.010.38996-192.168.149.020.00080:  GET /hello.txt HTTP/1.1Host: 192.168.149.20192.168.149.020.00080-192.168.149.010.38996:  HTTP/1.1 200 OKContent-Length: 20192.168.149.020.00080-192.168.149.010.38996:  hello from n129 lab200 hello.txt200 hello.txt404 missing.txt34 packets captured
Terminal
cd /tmptcpflow -r web.pcap -C 'dst port 80'tcpflow -r web.pcap -c -D 'dst port 80'
Output
$ tcpflow -r web.pcap -C 'dst port 80'GET /hello.txt HTTP/1.1Host: 192.168.149.20User-Agent: curl/7.88.1Accept: */*$ tcpflow -r web.pcap -c -D 'dst port 80'192.168.149.010.38998-192.168.149.020.00080:0000: 4745 5420 2f68 656c 6c6f 2e74 7874 2048  5454 502f 312e 310d 0a48 6f73 743a 2031  GET /hello.txt HTTP/1.1..Host: 1

129.4 tcpflow: flow storage and limits

  • -o out writes flow files into a directory; 3 connections = 6 files (two directions each) + report.xml (DFXML).
  • -b 40 keeps only the first 40 bytes of each flow, saving disk on big captures.
  • -f max open files, -P keep closed flows in memory, -K separate pcap per flow, -X report name.
Terminal
tcpflow -r web.pcap -o outls outtcpflow -r web.pcap -o outb -b 40wc -c outb/*
Output
reportfilename: out/report.xml192.168.149.010.38998-192.168.149.020.00080192.168.149.010.39010-192.168.149.020.00080192.168.149.010.39022-192.168.149.020.00080192.168.149.020.00080-192.168.149.010.38998192.168.149.020.00080-192.168.149.010.39010192.168.149.020.00080-192.168.149.010.39022report.xml$ wc -c outb/*  40 outb/192.168.149.010.38998-192.168.149.020.00080  40 outb/192.168.149.010.39010-192.168.149.020.00080  ...5504 outb/report.xml

129.5 tcpflow: file naming (-F, -T)

OptionEffect
-Ft / -FTUnix / ISO-8601 timestamp in front of the name
-FcConnection counter (only when a name repeats)
-Fk / -Fm / -FgSub-directories for 1000 / 1M / 1G files
-FX / -FMNo flow files, only DFXML / MD5 of each flow in DFXML
-T '%B.%b/%A.%a.flow'Custom path: %A %a src IP/port, %B %b dst IP/port, / makes folders
%T %t %# %C %c %V %v %N %KTime, connection count, VLAN, number chunks for nested folders
Terminal
tcpflow -r web.pcap -o outT -FT; ls outT | head -1tcpflow -r web.pcap -o tpl -T '%B.%b/%A.%a.flow'find tpl -type f | sorttcpflow -r web.pcap -o tpl2 -T '%t-%a-%#.flow'ls tpl2
Output
2026-11-09T03:39:39Z192.168.149.010.38998-192.168.149.020.00080$ find tpl -type f | sorttpl/192.168.149.010.38998/192.168.149.020.00080.flowtpl/192.168.149.010.39010/192.168.149.020.00080.flowtpl/192.168.149.010.39022/192.168.149.020.00080.flowtpl/192.168.149.020.00080/192.168.149.010.38998.flowtpl/192.168.149.020.00080/192.168.149.010.39010.flowtpl/192.168.149.020.00080/192.168.149.010.39022.flowtpl/report.xml$ ls tpl21794195579-00080-0.flow1794195579-00080-1.flowc11794195579-00080-2.flowc21794195579-38998-0.flow1794195579-39010-0.flow1794195579-39022-0.flowreport.xml

129.6 tcpflow: scanners

  • -e http extracts each response body into its own file (hello text and the 404 HTML page).
  • -e md5 hashes every flow into the DFXML report (-X); -a / -e all runs every scanner.
  • -E name runs only one scanner, -x name disables one, -e python passes payloads to Python.
  • Verbosity: -d 0 silent, -v verbose; -h usage (55 lines), -hh with plug-in parameters (110 lines).
Terminal
tcpflow -r web.pcap -o http -e httpls http | grep HTTPBODYcat http/*HTTPBODY-001.txthead -2 http/*HTTPBODY-001.htmltcpflow -r web.pcap -o md5 -e md5 -X md5.xmlgrep -c hashdigest md5.xml
Output
$ ls http | grep HTTPBODY192.168.149.020.00080-192.168.149.010.38998-  HTTPBODY-001.txt192.168.149.020.00080-192.168.149.010.39022-  HTTPBODY-001.html$ cat http/*HTTPBODY-001.txthello from n129 lab$ head -2 http/*HTTPBODY-001.html<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY><H1>404 Not Found</H1>$ grep -c hashdigest md5.xml4

129.7 tcptrack: live connection speed

  • A TUI (top for TCP connections); here run inside tmux and captured as text.
  • -r 10 keeps closed connections on screen for 10 s; a pcap filter shortens the list; -d only new connections; -f faster average.
  • Keys: s = sort (unsorted, rate, total bytes), p = pause display (tracking continues), q = quit.
Terminal
tmux new-session -d -s tt -x 80 -y 10 \  'tcptrack -i eth0 -r 10'(exec 3<>/dev/tcp/192.168.149.20/9000  timeout 25 cat <&3 >/dev/null) &(exec 3<>/dev/tcp/192.168.149.20/9001  timeout 25 cat <&3 >/dev/null) &curl -s -o /dev/null http://192.168.149.20/hello.txttmux capture-pane -p -t tttmux send-keys -t tt s
Output
 Client                Server                State        Idle A Speed 192.168.149.10:50738  192.168.149.20:9001   ESTABLISHED  0s     7 KB/s 192.168.149.10:34424  192.168.149.20:80     CLOSED       6s     0 B/s 192.168.149.10:37020  192.168.149.20:9000   ESTABLISHED  0s     29 KB/s  TOTAL                                                           36 KB/s Connections 1-3 of 3                         Unpaused  Unsorted$ tmux send-keys -t tt s Client                Server                State        Idle A Speed 192.168.149.10:37020  192.168.149.20:9000   ESTABLISHED  0s     26 KB/s 192.168.149.10:50738  192.168.149.20:9001   ESTABLISHED  1s     7 KB/s  TOTAL                                                           33 KB/s Connections 1-2 of 2                         Unpaused  Sorted by rate

129.8 iptstate: the conntrack table

  • -1 prints once and exits: HTTP requests in TIME_WAIT, the port 9000 stream ESTABLISHED.
  • -t shows totals; -C needs conntrack accounting (real error: counters not enabled in the kernel).
  • Filters (IPs only): -s, -d (IP[/mask]), -S, -D (ports), -i invert.
  • Sort with -b: S, d, D, p, s, t (TTL), b / P (need -C); -r reverses.
  • Top-mode keys: b/B change sort, x deletes the highlighted state (careful on a production firewall), h help, q quit.
Terminal
iptstate -1iptstate -1 -t -C -b b -riptstate -1 -D 80 -b t
Output
$ iptstate -1IP Tables State Top -- Sort by: SrcIPSource                Destination           Prt State       TTL192.168.149.10:34424  192.168.149.20:80     tcp TIME_WAIT     0:01:15192.168.149.10:39010  192.168.149.20:80     tcp TIME_WAIT     0:00:33192.168.149.10:49690  192.168.149.20:80     tcp TIME_WAIT     0:00:22192.168.149.10:48484  192.168.149.20:9000   tcp ESTABLISHED 119:59:59$ iptstate -1 -t -C -b b -rCounters requested, but not enabled in the kernel!IP Tables State Top -- Sort by: Bytes reverseTotal States: 8 -- TCP: 8 UDP: 0 ICMP: 0 Other: 0 (Filtered: 0)$ iptstate -1 -D 80 -b tIP Tables State Top -- Sort by: TTLFilters: dport: 80Source                Destination           Prt State       TTL192.168.149.10:49690  192.168.149.20:80     tcp TIME_WAIT     0:00:14192.168.149.10:38996  192.168.149.20:80     tcp TIME_WAIT     0:00:17192.168.149.10:39010  192.168.149.20:80     tcp TIME_WAIT     0:00:25
Output
$ iptstate -s 192.168.149.10 -D 9000                         IPTState - IPTables State TopVersion: 2.2.7        Sort: SrcIP           b: change sorting   h: helpFilters: src: 192.168.149.10, dport: 9000Source                      Destination                Prt State       TTL192.168.149.10:48484        192.168.149.20:9000        tcp CLOSE         0:00:00

129.9 arpwatch: new stations and MAC changes

  • Create an empty arp.dat first; -d keeps arpwatch in the foreground with reports on stderr.
  • -i interface, -f database; Debian adds -z (ignore a DHCP range), -m (mail), -Q (no mail), -u (drop root).
  • A new container's first ARP triggered 'new station'.
Terminal
touch /tmp/arp.datarpwatch -d -i eth0 -f /tmp/arp.dat \  > /tmp/aw.log 2>&1 &# host: a new container joins n129-netdocker run -d --name n129-new \  --mac-address 02:42:c0:a8:95:1e \  --ip 192.168.149.30 ... alpine:3.20# on n129-new:ping -c 1 -W 2 192.168.149.10 | tail -1cat /tmp/aw.log
Output
[mon]$ cat /tmp/aw.logFrom: arpwatch (Arpwatch mon)Subject: new station (n129-new.n129-net) eth0            hostname: n129-new.n129-net          ip address: 192.168.149.30           interface: eth0    ethernet address: 02:42:c0:a8:95:1e           timestamp: Monday, November 9, 2026 3:41:49 +0000
arpwatch: new device and MAC changes
  1. 1.arpwatch listens for ARP on mon's eth0

  2. 2.New container sends its first ARP broadcast: 'new station'

  3. 3.Container changes its MAC: 'changed ethernet address'

  4. 4.Old MAC comes back: 'flip flop' (IP conflict or ARP spoofing)

  5. 5.Normal mode: messages go to syslog; arp.dat keeps both MACs

Output
[new]$ ip link set eth0 down; ip link set eth0 address 02:42:c0:a8:95:99; ...Subject: changed ethernet address (n129-new.n129-net) eth0          ip address: 192.168.149.30    ethernet address: 02:42:c0:a8:95:99old ethernet address: 02:42:c0:a8:95:1e               delta: 5 seconds[new]$ ip link set eth0 down; ip link set eth0 address 02:42:c0:a8:95:1e; ...Subject: flip flop (n129-new.n129-net) eth0          ip address: 192.168.149.30    ethernet address: 02:42:c0:a8:95:1eold ethernet address: 02:42:c0:a8:95:99               delta: 4 seconds[mon]$ cat /tmp/arp.dat02:42:c0:a8:95:1e  192.168.149.30  1794195721  n129-new  eth002:42:c0:a8:95:99  192.168.149.30  1794195717  n129-new  eth0
arpwatch reportMeaning
new stationMAC never seen before
new activityKnown pair seen again after 6+ months
changed ethernet addressIP now uses a new MAC
flip flopIP went back to the previous MAC
reused old ethernet addressIP uses an older MAC again
bogonIP not in the local subnet
ethernet mismatchFrame MAC differs from the MAC inside the ARP

129.10 Comparison: which tool when

ToolSource of dataBest for
tcpflowPackets (live or pcap), reassembledReading application data, extracting HTTP bodies
tcptrackLive packets on an interfaceLive bandwidth per TCP connection
iptstateKernel conntrack tableFirewall/NAT states and timeouts
arpwatchARP packets + arp.datNew devices, MAC changes, ARP spoofing alerts

129.11 Common mistakes

  • Thinking iptstate captures packets: it only shows what conntrack tracks.
  • Expecting -C counters without conntrack accounting enabled.
  • Pressing x in iptstate on a production firewall: it deletes the state.
  • Starting arpwatch without an arp.dat file.

129.12 Interview tips

  • Who is eating the bandwidth: tcptrack -i eth0, then key s to sort by rate.
  • Detect ARP spoofing: arpwatch keeps IP-MAC pairs; 'changed ethernet address' / 'flip flop' alerts go to syslog; prevent with dynamic ARP inspection on the switch.
  • TIME_WAIT entries in conntrack are normal after a client closes a connection.
  • tcpflow -e http pulls files out of HTTP traffic for forensics.