Skip to content

Instantly share code, notes, and snippets.

@ndunks ndunks/dnsmasq-wlan
Created May 5, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.