Skip to content

Instantly share code, notes, and snippets.

@Avasz
Created May 20, 2016 09:01
Show Gist options
  • Save Avasz/21731d2ecfc1998c6cf8e340508186cd to your computer and use it in GitHub Desktop.
Save Avasz/21731d2ecfc1998c6cf8e340508186cd to your computer and use it in GitHub Desktop.
Switch to CDMA if Ethernet is down and switch back to Ethernet once it is up. Tolerance time = 1minutes
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
dir=/home/odroid/Switcher/
#Check for interface
INTERFACE=`head -n1 $dir/switcher.log | awk '{print $3}'`
eth_iface=`ls /sys/class/net | grep eth`
CDMA_NUM=`sed '2q;d' $dir/switcher.log | awk '{print $3}'`
hit=`cat $dir/hit`
new_hit=$(($hit+1))
#First ping hydrology.gov.np and check if internet is working or not
function pinger()
{
if [ "`ping -c 5 hydrology.gov.np`" ]
then
INTERNET=1 #YES INTERNET
else
INTERNET=0 #NO INTERNET
fi
}
function cdma()
{
ifconfig $eth_iface down
pon.wvdial
sleep 15
if [ $? -eq 0 ]
then
pinger
if [ "$INTERNET" == "1" ]
then
sed -i "1s/$INTERFACE/CDMA/" $dir/switcher.log
sed -i "2s/$CDMA_NUM/$(($CDMA_NUM+1))/" $dir/switcher.log
exit
fi
fi
ifconfig $eth_iface down
}
function ethernet()
{
sleep 5
ifconfig $eth_iface down
ifconfig $eth_iface up
timeout 5 dhclient $eth_iface
if [ $? -eq 0 ]
then
pinger
if [ "$INTERNET" == "1" ]
then
sed -i "1s/$INTERFACE/ETHERNET/" $dir/switcher.log
sed -i "2s/$CDMA_NUM/0/" $dir/switcher.log
if [ "`ifconfig | grep ppp0`" ]
then
poff.wvdial
dhclient eth0
fi
else
cdma
fi
else
ifconfig $eth_iface down
if [ "$INTERFACE" == "CDMA" ]
then
exit
else
cdma
fi
fi
dhclient $eth_iface
}
pinger
if [ "$INTERFACE" == "ETHERNET" ] && [ "$INTERNET" == "1" ]
then
if [ "`ifconfig | grep ppp0`" ]
then
poff.wvdial
ifconfig ppp0 down
dhclient eth0
fi
exit
elif [ "$INTERFACE" == "ETHERNET" ] && [ "$INTERNET" == "0" ]
then
#first try to get ethernet working
ethernet
elif [ "$INTERFACE" == "CDMA" ] && [ "$INTERNET" == "1" ]
then
ethernet
elif [ "$INTERFACE" == "CDMA" ] && [ "$INTERNET" == "0" ]
then
ethernet
else
continue
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment