Created
December 5, 2012 19:00
-
-
Save DanielThomas/4218502 to your computer and use it in GitHub Desktop.
init script to automatically start vpnc on TomatoUSB 1.28 Shibby K26RT-N build 101
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/bin/bash | |
# | |
# requires: ipkg install bash procps | |
SERVICE_NAME=autostart-vpnc.sh | |
LOGFILE=/opt/var/log/$SERVICE_NAME.log | |
PACKET_LOGFILE=/var/log/messages | |
EXTERNAL_IF=vlan2 | |
POLLING_INTERVAL=30 | |
function set_service_pid { | |
PID=$(pgrep -o -f $SERVICE_NAME) | |
} | |
function set_vpnc_pid { | |
VPNC_PID=$(pgrep -o -f ^vpnc) | |
} | |
function traffic_monitor { | |
while true; do | |
set_vpnc_pid | |
if [ -z "$VPNC_PID" ]; then | |
logger "$SERVICE_NAME [$$] starting vpnc" | |
date >> $LOGFILE | |
vpnc >> $LOGFILE | |
TUNNEL_DEVICE=$(ifconfig | grep tun | awk '{print $1}') | |
TUNNEL_NAT=$(iptables --table nat --list POSTROUTING --verbose | grep $TUNNEL_DEVICE) | |
if [ -z "$TUNNEL_NAT" ]; then | |
# routing table will be configured by vpnc, add masquerade rule only | |
iptables --table nat --append POSTROUTING --out-interface $TUNNEL_DEVICE --jump MASQUERADE | |
fi | |
else | |
while [ -n "$VPNC_PID" ]; do | |
# wait for disconnect | |
sleep $POLLING_INTERVAL | |
set_vpnc_pid | |
done | |
logger "$SERVICE_NAME [$$] vpnc has stopped" | |
fi | |
done | |
} | |
function start { | |
set_service_pid | |
if [ "$PID" == "$$" ]; then | |
if [ ! -d ${LOGFILE%/*} ]; then | |
mkdir -p ${LOGFILE%/*} | |
fi | |
traffic_monitor & | |
else | |
echo "$SERVICE_NAME already running at $PID" | |
fi | |
} | |
function stop { | |
set_service_pid | |
while [ "$PID" != "$$" ]; do | |
kill $PID | |
set_service_pid | |
done | |
set_vpnc_pid | |
if [ -n "$VPNC_PID" ]; then | |
vpnc-disconnect | |
fi | |
} | |
function status { | |
set_service_pid | |
if [ "$PID" != "$$" ]; then | |
set_service_pid | |
echo "$SERVICE_NAME is running at pid $PID" | |
else | |
echo "$SERVICE_NAME is not running" | |
fi | |
set_vpnc_pid | |
if [ -n "$VPNC_PID" ]; then | |
echo "vpnc is running at pid $VPNC_PID" | |
else | |
echo "vpnc is not running" | |
fi | |
} | |
case $1 in | |
start) start ;; | |
stop) stop ;; | |
restart) $0 stop; $0 start ;; | |
status) status ;; | |
*) echo "Usage: $0 {start|stop|status}" | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment