Skip to content

Instantly share code, notes, and snippets.

@stefanlasiewski
Last active August 7, 2021 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanlasiewski/920783a8e825c70259e37c4cfa5efd6e to your computer and use it in GitHub Desktop.
Save stefanlasiewski/920783a8e825c70259e37c4cfa5efd6e to your computer and use it in GitHub Desktop.
tcpdump on all interfaces at one
#!/bin/bash
#===================================================================================
#
# FILE: dump.sh
# USAGE: dump.sh [-i interface] [tcpdump-parameters]
# DESCRIPTION: tcpdump on any interface and add the prefix [Interace:xy] in front of the dump data.
# OPTIONS: same as tcpdump
# REQUIREMENTS: tcpdump, sed, ifconfig, kill, awk, grep, posix regex matching
# BUGS: ---
# FIXED: - In 1.0 The parameter -w would not work without -i parameter as multiple tcpdumps are started.
# - In 1.1 VLAN's would not be shown if a single interface was dumped.
# NOTES: ---
# - 1.2 git initial
# AUTHOR: Sebastian Haas
# COMPANY: pharma mall
# VERSION: 1.2
# CREATED: 16.09.2014
# REVISION: 22.09.2014
# REVISION: 06.09.2021 - Stefan Lasiewski - Fixed to support Calico interfaces for Kubernetes/Docker/Containers. Normalize the length of the interface names.
#
#===================================================================================
# When this exits, exit all background processes:
trap 'kill $(jobs -p) &> /dev/null && sleep 0.2 && echo ' EXIT
# Create one tcpdump output per interface and add an identifier to the beginning of each line:
if [[ $@ =~ -i[[:space:]]?[^[:space:]]+ ]]; then
tcpdump -l $@ | sed 's/^/[Interface:'"${BASH_REMATCH[0]:2}"'] /' &
else
for interface in $(ip link list | grep '^[a-z0-9]' | awk '{print $2}' | sed 's/@...:$//' | sed 's/:$//')
do
tcpdump -l -i $interface -nn $@ port 5201 | sed 's/^/[Interface:'"$(echo $interface" " |cut -c1-10)"'] /' &
done
fi
# wait .. until CTRL+C
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment