Skip to content

Commit

Permalink
mxsnoop: Add prototype
Browse files Browse the repository at this point in the history
Use to monitor network traffic on site.
  • Loading branch information
thomas committed Aug 6, 2021
1 parent dac25ac commit 7572632
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions mxsnoop/mxsnoop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/bin/bash

#
# +--------------+ (!) +----------------------+ +---****
# | | | br01 | |
# | THE SUSPECT O]===X===[O SCHNUEFFELSTUECK O]======[O WIS-NET
# | | |INLET OUTLET| |
# +--------------+ +----------------------+ +---****
# (MX_NETDEV)

# Basics: https://wiki.archlinux.org/title/Network_bridge

# GATEWAY=10.0.3.1
# BROADCAST=10.0.3.255
# NETMASK=255.255.255.0
# NIDL=24

GATEWAY=141.14.16.128
BROADCAST=141.14.31.255
NETMASK=255.255.240.0
NIDL=20

INLET=${INLET:-}
OUTLET=${OUTLET:-}
BRIDGE=br01

TRANSPARENT=${TRANSPARENT:-}
STP=${STP:-}

function die() { echo $1; exit 1; }

type ip > /dev/null || \
die 'Error: Tools not available, check for ip command.'

MXHOSTCONF=${MXHOSTCONF:-/etc/local/mxhost.conf}
source $MXHOSTCONF || die "# Error: No 'mxhost.conf' ?"
test -n "$MX_IPADDR" || die '# Error: MX_IPADDR missing.'

if [ -z "$OUTLET" ]; then
test -n "$MX_NETDEV" || die '# Error: MX_NETDEV missing?'
test -e /sys/class/net/$MX_NETDEV || die "# Error: $MX_NETDEV missing."
OUTLET=$MX_NETDEV
fi


# inlet was present at boot time -> netXY, or plugged later -> Search for ethX
if [ -z "$INLET" ]; then # find the (sole) inlet
for D in net{00..09} eth{0..9}; do
test -e /sys/class/net/$D || continue
test $D = $OUTLET && continue
test -n "$INLET" && \
die 'Error: Too many network devices (netXY) present, use INLET=dev (OUTLET=dev ?).'
INLET=$D
done
fi

test -z "$INLET" && die '# Error: no second network device found.'

echo "# Note: about to setup/destroy $BRIDGE between $INLET (INLET) and $OUTLET (OUTLET), IP: $MX_IPADDR"

cmd="$1"

case "$cmd" in

start)

if [ -z "$STP" ]; then
ip link add dev $BRIDGE type bridge
else
ip link add dev $BRIDGE type bridge stp_state 1
fi

ip address flush dev $INLET
ip address flush dev $OUTLET

ip link set $INLET master $BRIDGE
ip link set $OUTLET master $BRIDGE

if [ -z "$TRANSPARENT" ] ; then
ip addr add $MX_IPADDR/$NIDL broadcast $BROADCAST dev $BRIDGE
ip link set up dev $BRIDGE
ip route add default via $GATEWAY dev $BRIDGE
fi

ip link set dev $INLET up
ip link set dev $OUTLET up

;;

stop)

ip link set $INLET nomaster
ip link set $OUTLET nomaster
ip link delete $BRIDGE type bridge

ip addr add $MX_IPADDR/$NIDL broadcast $BROADCAST dev $OUTLET
ip link set dev $OUTLET up
ip route add default via $GATEWAY

;;

restart)

$0 stop
sleep 1
$0 start
;;

*)

echo "# usage: [INLET=dev] [OUTLET=dev] $0 [start|stop|restart]"
echo "# options: TRANSPARENT=1 doesn't bind IP on the bridge"
echo "# STP=1 enables Spanning Tree"
;;

esac

0 comments on commit 7572632

Please sign in to comment.