Skip to content

Instantly share code, notes, and snippets.

@mcastelino
Forked from lonelymtn/mirror-port.sh
Created October 27, 2017 00:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcastelino/6e270a3264c0959d882e5fde04303995 to your computer and use it in GitHub Desktop.
Save mcastelino/6e270a3264c0959d882e5fde04303995 to your computer and use it in GitHub Desktop.
Mirror traffic between two interfaces using Linux's traffic controll subsystem (tc)
#!/usr/bin/env bash
# Time-stamp: <2014-07-31 13:31:43 (ryanc)>
#
# Description: Mirror traffic between two interfaces using Linux's
# traffic control subsystem (tc)
trap cleanup EXIT
CLEANUP=1
SRC_IFACE=$1
DST_IFACE=$2
function cleanup() {
if [ $CLEANUP -eq 1 ]; then
tc qdisc del dev $SRC_IFACE ingress
tc qdisc del dev $SRC_IFACE root
fi
echo
}
if [ $# -lt 2 ]; then
echo "Usage: ${0/*\//} <src interface> <dst interface>"
CLEANUP=0
exit 1
fi
echo
echo "Mirroring traffic from $SRC_IFACE to $DST_IFACE"
# ingress
tc qdisc add dev $SRC_IFACE ingress
tc filter add dev $SRC_IFACE parent ffff: \
protocol all \
u32 match u8 0 0 \
action mirred egress mirror dev $DST_IFACE
# egress
tc qdisc add dev $SRC_IFACE handle 1: root prio
tc filter add dev $SRC_IFACE parent 1: \
protocol all \
u32 match u8 0 0 \
action mirred egress mirror dev $DST_IFACE
echo "Hit Ctrl-C or kill this session to end port mirroring"
sleep infinity
trap - EXIT
cleanup
exit 0
# End of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment