Skip to content

Instantly share code, notes, and snippets.

@allex
Last active October 9, 2016 09:49
Show Gist options
  • Select an option

  • Save allex/3f2177cacc46a9b9bd170f580ed507ab to your computer and use it in GitHub Desktop.

Select an option

Save allex/3f2177cacc46a9b9bd170f580ed507ab to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# pptp Automatically Detect and Restart Linux PPTP Client
#
# chkconfig: 2345 55 25
#
# author: Allex Wang ([email protected])
#
# */1 * * * * /etc/init.d/pptp start test >> /var/log/pptp_pinger.log 2>&1
#
# GistID: 3f2177cacc46a9b9bd170f580ed507ab
# GistURL: https://gist.github.com/3f2177cacc46a9b9bd170f580ed507ab
set -e
[ -f /etc/profile ] && . /etc/profile
lock_file=/tmp/vpn_check.lock
[ ! -f "$lock_file" ] || exit 1;
echo "$$" > "$lock_file"
exit_code()
{
local ec=$1
[ -n "$ec" ] || ec=0
rm -f "$lock_file"
exit $ec
}
trap 'exit_code' 0 1 2 3 9 13 15
get_gatway() {
local c tunnel=$1
case "$tunnel" in
testing )
c="192.168.0.0/24 192.168.0.10"
;;
staging )
c="192.168.200.0/24 192.168.200.1"
;;
* )
echo >&2 "Tunnel not found"
exit_code 255
;;
esac
printf "$c"
}
start()
{
local tunnel="$1"
pppd call $tunnel updetach && \
{
local cfg=( `get_gatway "$tunnel"` )
route add -net ${cfg[0]} gw ${cfg[1]}
}
}
check() {
local tunnel=$1
local cfg=( `get_gatway "$tunnel"` )
local ip=${cfg[1]}
local PINGRES=`ping -c 2 $ip`
local PLOSS=`echo $PINGRES : | grep -oP '\d+(?=% packet loss)'`
if [ "100" -eq "$PLOSS" ]
then
echo "Loss result: $PLOSS"
return 1
fi
}
case "$1" in
start)
echo `date`
if ! check "$2"; then
start "$2" || exit_code 1
else
echo running...
fi
;;
stop)
echo "not implements"
;;
*)
echo >&1 "Internal error"
;;
esac
exit_code $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment