Skip to content

Instantly share code, notes, and snippets.

@ndunks
Created May 5, 2018 04:24
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 ndunks/229a5ab78051de9041391e379f0e6467 to your computer and use it in GitHub Desktop.
Save ndunks/229a5ab78051de9041391e379f0e6467 to your computer and use it in GitHub Desktop.
Start dnsmaq when connected to wifi
#!/bin/bash
# Write file /etc/network/if-up.d/ and link to /etc/network/if-post-down.d/
# by default, scripts on that directories will be triggered by NetworkManager
PID="/var/run/dnsmasq/wlan.pid"
WLAN="wlan0"
DNSMASQCONF="/etc/dnsmasq.conf"
IP=$(ifconfig $WLAN | sed -En 's/.*inet ([0-9\\.]+) .+/\1/p')
ME="$IFACE-dnsmasq:"
#echo "$ME Triggered by $IFACE" > /dev/kmsg
[ "$IFACE" = "$WLAN" ] || exit 0
echo "$ME $MODE $IP" > /dev/kmsg
#Remove interface & listen address and replace loopback
CONFIG=`sed "s/^interface=.*//; s/^listen-address=.*//; s/127.0.0.1/$IP/" /etc/dnsmasq.conf`
if [ "$MODE" = "start" ]; then
if [ -f "$PID" ]; then
# Already running
echo "$ME Canceled, pid exists $PID" > /dev/kmsg
exit 1
fi
echo "$CONFIG" | dnsmasq -C - \
--bind-interfaces \
--except-interface=lo \
--interface=$WLAN \
--listen-address=$IP \
--pid-file=$PID &> /dev/kmsg
elif [ "$MODE" = "stop" ]; then
if [ -f "$PID" ]; then
# Already running
kill `cat $PID` > /dev/kmsg
unlink $PID
else
echo "$ME No Pid found" > /dev/kmsg
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment