Skip to content

Instantly share code, notes, and snippets.

@csghone
Created May 20, 2020 06:55
Show Gist options
  • Save csghone/fda28bc5cc85046cad188cda87c87883 to your computer and use it in GitHub Desktop.
Save csghone/fda28bc5cc85046cad188cda87c87883 to your computer and use it in GitHub Desktop.
UDP listener using socat
#!/bin/bash
# listen_feed.sh 224.0.31.130 14382 ens6d1 out_file
# Runs: socat udp-datagram:224.0.31.130:14382,bind=:14382,ip-add-membership=224.0.31.130:ens6d1 /dev/null > out_file
UDP_ADDRESS="224.0.31.130"
UDP_PORT="14382"
INTERFACE="ens6d1"
if [ $# -ne 3 ] && [ $# -ne 4 ]; then
echo "Example usage: $0 224.0.31.130 14382 ens6d1 <out_file>"
echo "<out_file> is optional. Default is to pipe to console using hexdump"
exit 255
fi
UDP_ADDRESS="$1"
UDP_PORT="$2"
INTERFACE="$3"
OUTFILE="$4"
## Alternate command, blocks other apps: socat udp-datagram:$UDP_ADDRESS:$UDP_PORT,bind=:$UDP_PORT,ip-add-membership=$UDP_ADDRESS:$INTERFACE - |
socat udp-recvfrom:$UDP_PORT,ip-add-membership=$UDP_ADDRESS:$INTERFACE,fork,reuseaddr - |
if [ "$OUTFILE" == "" ]; then
tee /dev/null | hexdump -C
else
tee /dev/null > $OUTFILE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment