The tcpdump One-Liner I Actually Remember

A small Python wrapper around the only tcpdump invocation I can recall under pressure, plus a parser that turns its line-buffered output into JSON so I can pipe it to jq.

Python
Compiler
3 snippets
debugging
networking
code-template
utility
elisehuang

By @elisehuang

March 17, 2026

·

Updated May 18, 2026

382 views

3

4.4 (9)

"""tcpdump_cmd.py: the wrapper I keep so I never retype the flags."""
import shutil


def tcpdump_cmd(port, iface='any', count=100):
    cmd = [
        'tcpdump', '-i', iface,
        '-A',       # ASCII payload, so HTTP is readable
        '-s', '0',  # full snaplen, do not truncate at 96 bytes
        '-l',       # line-buffered, so piping to grep flushes
        '-nn',      # skip DNS and port-name lookups
    ]
    if count is not None:
        cmd += ['-c', str(count)]
    cmd += [f'tcp port {port}']
    return cmd


if __name__ == '__main__':
    cmd = tcpdump_cmd(port=8080, count=50)
    print(f'# tcpdump on PATH: {shutil.which("tcpdump") is not None}')
    head, filt = cmd[:-1], cmd[-1]
    print(' '.join(head) + f" '{filt}'")

Every time my service stops talking to a downstream and metrics are unhelpful, I reach for tcpdump. The flags I always need are -A (ASCII payload so HTTP headers are readable), -s 0 (full snaplen, otherwise headers cut at 96 bytes), -l (line-buffered so piping to grep actually flushes), and -nn (skip DNS, which can take seconds on a slow resolver). The function exists so I stop typing -s 96 and wondering why my Authorization header is truncated. On macOS BSD tcpdump, replace -i any with an explicit interface like -i en0.