VPN Check Per Minute With Automatic Route (PPTP CLIENT)

script automatic – ( script check vpn dan tambahkan route static ) disini gua pake centos 6

first create directory running below command.

# mkdir -p /var/log/vpn/

then save below script

# save this /root/cron/vpn.sh
#!/bin/bash

PPP_MPPE=`modinfo ppp_mppe &> /dev/null`
ISLOADMODULEPPP_MPPE=`echo $?`

if [[ $ISLOADMODULEPPP_MPPE == 0 ]] ; then
	echo "MODULE PPP_MPPE IS LOADED" &> /dev/null
else
	modprobe ppp_mppe &> /dev/null
	echo "FIRST MODULE WAS STARTED" &> /dev/null
fi

NF_CONNTRACK_PPTP=`modinfo nf_conntrack_pptp &> /dev/null`
ISLOADMODULENF_CONNTRACK_PPTP=`echo $?`

if [[ $ISLOADMODULENF_CONNTRACK_PPTP == 0 ]] ; then
	echo 'MODULE NF_CONNTRACK_PPTP IS LOADED' &> /dev/null
else
	modprobe nf_conntrack_pptp &> /dev/null
	echo "SECOND MODULE WAS STARTED" &> /dev/null
fi

#LOCALVPNIP=`ip addr | grep -i 'inet ' | grep -i 'ppp0' | awk '{print $2}'`
LOCALVPNIP=10.10.11.100
DATETIME=`date`
DATE=`date +%F`
PINGRES=`ping -c 2 $LOCALVPNIP`
PLOSS=`echo $PINGRES : | grep -oP '\d+(?=% packet loss)'`
echo "$DATETIME : Loss Result : $PLOSS" >> /var/log/vpn/"$DATE".log 2>&1
if [[ "100" -eq "$PLOSS" ]]; then
	/usr/sbin/pppd call vpn.pptp.org >> /var/log/vpn/pppd.log 2>&1
	sleep 10
	route add -host 10.10.10.10 gw 10.10.1.10 >> /var/log/vpn/pppd.log 2>&1
else	
	echo "$DATETIME : Already Running: $LOCALVPNIP" >> /var/log/vpn/"$DATE".log 2>&1
fi

and this is my fucking crontab at /etc/crontab, this script running per minutes with cron.

*/1 * * * * root /bin/bash /root/cron/vpn.sh

add above script at the end line.