I need a custom script that checks OpenVPN connection

Post new topic   Reply to topic    DD-WRT Forum Index -> Advanced Networking
Goto page Previous  1, 2, 3, 4
Author Message
mkaand
DD-WRT User


Joined: 06 Jan 2008
Posts: 307
Location: Istanbul

PostPosted: Sun Jul 30, 2017 14:11    Post subject: Reply with quote
Thank you very much mchaley. But my system a little bir complicated. I use too much services and it effects each other. dnsmasq and ubound services has special config files. I changed my scripts a little bit. Now it works perfectly. I fixed my problem. My latest fix bold values:

vpn-pass.sh file it runs every hour:

Quote:

#!/bin/bash
#
######################################################################
#
# vpn-pass.sh @ VERSION 1.1
#
# VPNBook Password Updater by Kaan Dogan
#
######################################################################

file=/tmp/openvpncl/credentials
statuscode=`curl -sL -w "%{http_code}\\n" "http://xxxxxxxxxxxx/vpnbook.php" -o /dev/null`
oldpassword=$(cat "$file")
oldpassword=$(echo $oldpassword| cut -d' ' -f 2)

vpnpasssrc="http://xxxxxxxxxxxxxxxxxxx/vpnbook.php"
newpassword=$( curl -s "$vpnpasssrc" )

if [ "$statuscode" == "200" ]; then
if [ "$newpassword" != "$oldpassword" ]; then
nvram set openvpncl_pass="$newpassword";
nvram commit;
reboot;
fi
fi
kill $(ps | grep "custom.sh" | grep -v "grep" | cut -d " " -f1)
kill $(ps | grep "ping" | grep -v "grep" | cut -d " " -f1)
kill $(ps | grep "custom.sh" | grep -v "grep" | cut -d " " -f2)
kill $(ps | grep "ping" | grep -v "grep" | cut -d " " -f2)

sh /tmp/custom.sh


custom.sh file:

Quote:

kill $(ps | grep "vpn-pass.sh" | grep -v "grep" | cut -d " " -f1)
kill $(ps | grep "vpn-pass.sh" | grep -v "grep" | cut -d " " -f2)

testserver="pandora.com"

REACHEABLE=0;
while [ $REACHEABLE -ne "1" ];
do
ping -q -W 5 $testserver &> /dev/null; REACHEABLE=$?;
if [ $REACHEABLE -eq 0 ]
then
echo "Pingable"
else
echo "Not Pingable"
stopservice openvpn -f
stopservice dnsmasq -f
sleep 1
stopservice openvpn -f
openvpn --config /jffs/openvpncl/openvpn.conf --route-up /jffs/openvpncl/route-up.sh --route-pre-down /jffs/openvpncl/route-down.sh --daemon
sed -i '/port=0/d' /tmp/dnsmasq.conf
sed -i '/resolv-file=\/tmp\/resolv.dnsmasq/d' /tmp/dnsmasq.conf
dnsmasq --conf-file=/tmp/dnsmasq.conf
fi
done


_________________
Kaan's World | @mkaand | PLEX Archive | Trakt.tv
Sponsor
mkaand
DD-WRT User


Joined: 06 Jan 2008
Posts: 307
Location: Istanbul

PostPosted: Wed Aug 09, 2017 22:14    Post subject: Reply with quote
I changed my system a little bit. Because if VPN down I can ping thru my WAN. Instead of heavy pings I simplified my code. Every hour vpn-pass.sh checks vpnbook password. If it was changed, it will change on router and reboot. If the VPN password didn't change, it starts custom.sh. It checks the traceroute result. I know my VPN gateway is 10.10.0.1 works perfectly.


vpn-pass.sh file it runs every hour:

Quote:

#!/bin/bash
#
######################################################################
#
# vpn-pass.sh @ VERSION 1.2
#
# VPNBook Password Updater by Kaan Dogan
#
######################################################################

file=/tmp/openvpncl/credentials
statuscode=`curl -sL -w "%{http_code}\\n" "http://xxxxxxxxxxxx/vpnbook.php" -o /dev/null`
oldpassword=$(cat "$file")
oldpassword=$(echo $oldpassword| cut -d' ' -f 2)

vpnpasssrc="http://xxxxxxxxxxxxxxxxxxx/vpnbook.php"
newpassword=$( curl -s "$vpnpasssrc" )

if [ "$statuscode" == "200" ]; then
if [ "$newpassword" != "$oldpassword" ]; then
nvram set openvpncl_pass="$newpassword";
nvram commit;
reboot;
fi
fi
#kill $(ps | grep "custom.sh" | grep -v "grep" | cut -d " " -f1)
#kill $(ps | grep "ping" | grep -v "grep" | cut -d " " -f1)
#kill $(ps | grep "custom.sh" | grep -v "grep" | cut -d " " -f2)
#kill $(ps | grep "ping" | grep -v "grep" | cut -d " " -f2)

sh /tmp/custom.sh


custom.sh file:

Quote:

#!/bin/bash
#kill $(ps | grep "vpn-pass.sh" | grep -v "grep" | cut -d " " -f1)
#kill $(ps | grep "vpn-pass.sh" | grep -v "grep" | cut -d " " -f2)

testserver="pandora.com"
traceroute $testserver -m 2 > /tmp/traceroute;

if grep -q '10.10.0.1' '/tmp/traceroute'; then

echo Traceable;

else

echo "Not Traceable"
stopservice openvpn -f
stopservice dnsmasq -f
sleep 1
stopservice openvpn -f
openvpn --config /jffs/openvpncl/openvpn.conf --route-up /jffs/openvpncl/route-up.sh --route-pre-down /jffs/openvpncl/route-down.sh --daemon
sed -i '/port=0/d' /tmp/dnsmasq.conf
sed -i '/resolv-file=\/tmp\/resolv.dnsmasq/d' /tmp/dnsmasq.conf
dnsmasq --conf-file=/tmp/dnsmasq.conf

fi

_________________
Kaan's World | @mkaand | PLEX Archive | Trakt.tv
Goto page Previous  1, 2, 3, 4 Display posts from previous:    Page 4 of 4
Post new topic   Reply to topic    DD-WRT Forum Index -> Advanced Networking All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum