Saturday, February 23, 2008

Bash Scripting: A new beginning

Recently, I found that due to excessive amount of work, I have to automate tasks as much as possible. So, here I am learning to script as much as possible in my linux box. I've just started to script in Bash and here's the first working script.

#!/bin/bash
#
# This script suppose to setup the machine to connect to Jalawave
# using rfcomm0 interface and reports any error it encounters.
# The following is the logic.
# 1. Check whether the prerequisite is met. Abort if not.
# 2. Check whether rfcomm0 exists. If it isn't, then bind it
# using rfcomm.
# 3. Check whether interface ppp0 exists. If it doesn't exist,
# execute "wvdial bluetooth" in the background and wait
# until ppp0 interface created.
# 4. Check whether the default gateway to Jalawave has been
# created by the route program. Create one if it's not
# been created yet.
# 5. Update the nameserver configuration to the right value
#

# Error codes
E_NOT_ROOT=66 # Todo: you have to fix the error code
E_WVDIAL_STOP=67

COM_DEVICE=rfcomm0
PPP_INTERFACE=ppp0
JALA_GW=10.6.6.6

#
# 1. Check whether the prerequisite is met. Abort if it isn't.
#

#
# Check whether we are superuser
#
if [ "$UID" -ne 0 ]; then
echo "Error! You are not a superuser. You have to run this script as superuser.";
exit $E_NOT_ROOT;
else
echo "Executing script to connect to Jalawave via bluetooth 3G modem.."
fi

#
# Check whether awk exists
#
if [ ! -x /usr/bin/awk ];then
echo "Error! Awk isn't installed. Aborting.."
exit 0
fi

#
# Check whether grep exists
#
if [ ! -x /usr/bin/grep ];then
echo "Error! Grep isn't installed. Aborting.."
exit 0
fi


#
# 2. Check whether rfcomm0 exists. If it isn't, then bind it
# using rfcomm.
echo "Configuring $COM_DEVICE.."

PHY_COM=$(ls /dev | grep $COM_DEVICE | awk '{print $1}')
if [ "$PHY_COM" != "$COM_DEVICE" ]; then
echo "$COM_DEVICE doesn't exist. Creating $COM_DEVICE.."

# Todo: handle other device(s) in the future
rfcomm bind $COM_DEVICE
fi

#
# 3. Check whether interface ppp0 exists. If it doesn't exist,
# execute "wvdial bluetooth" in the background and wait
# until ppp0 interface created.
#
PHY_PPP=$(ifconfig | grep $PPP_INTERFACE | awk '{print $1}')
if [ "$PHY_PPP" != "$PPP_INTERFACE" ]; then
echo "Executing wvdial in the background.."
wvdial bluetooth 2>/var/log/wvdial_log.txt &
echo "You can examine Wvdial output at /var/log/wvdial_log.txt"

# wait until the PPP device created
echo "Waiting for $PPP_INTERFACE to be created.."
while [ "$PHY_PPP" != "$PPP_INTERFACE" ]
do
PHY_PPP=$(ifconfig | grep '^ppp[0-9]' | awk '{print $1}')

# check whether the background wvdial process is still alive
WVDIAL_PS=$(ps -C wvdial | awk '{print $4}' | grep 'wvdial')
if [ "$WVDIAL_PS" != "wvdial" ]; then
echo "Wvdial terminated abruptly. Aborting.."
exit $E_WVDIAL_STOP
fi

# delay a little bit
sleep 1
done

else
echo "$PPP_INTERFACE interface exists as $PHY_PPP"
fi

#
# 4. Check whether the default gateway to Jalawave has been
# created by the route program. Create one if it's not
# been created yet.
#
echo "Checking for Jalawave gateway.."
MY_GW=$(route -n | awk '{print $2}' | grep $JALA_GW)
if [ "$MY_GW" != "JALA_GW" ]; then
echo "Jalawave gateway doesn't exist!"
echo "Creating default route to Jalawave gateway.."
route add default gw $JALA_GW $PPP_INTERFACE
echo "Default route created"
fi

#
# 5. Update the nameserver configuration to the right value
#
echo "Updating nameserver setting.."
cat /etc/ppp/resolv.conf > /etc/resolv.conf

echo "Connected."
exit 0


Of course this one still has a lot of bugs. I will refine it from time to time.
The second script is used to disconnect from the ISP.


#!/bin/bash
#
# This script suppose to disconnect from Jalawave
# The following is the logic.
# 1. Check whether the prerequisites are met.
# 2. Kill wvdial.
# 3. Wait for the ppp0 interface to be destroyed.
# 4. Release rfcomm0 binding.
#

# Error codes
E_NOT_ROOT=66 # Todo: you have to fix the error code

RFCOMM_DEVICE=rfcomm0
PPP_INTERFACE=ppp0

#
# 1. Check whether the prerequisite is met. Abort if it isn't.
#
if [ "$UID" -ne 0 ]; then
echo "Error! You are not a superuser. You have to run this script as superuser.";
exit $E_NOT_ROOT;
else
echo "Executing script to disconnect from Jalawave via bluetooth 3G modem.."
fi

#
# 2. Kill wvdial.
#

# check whether the background wvdial process is still alive
WVDIAL_PS=$(ps -C wvdial | awk '{print $4}' | grep 'wvdial')
if [ "$WVDIAL_PS" == "wvdial" ]; then
echo "Killing wvdial process.."
killall wvdial
fi

#
# 3. Wait until the PPP_INTERFACE destroyed before releasing
# rfcomm0 binding.
#

PHY_PPP=$(ifconfig | grep '^ppp[0-9]' | awk '{print $1}')

if [ "$PHY_PPP" == "$PPP_INTERFACE" ]; then
echo "Waiting for $PPP_INTERFACE to be destroyed.."
fi

while [ "$PHY_PPP" == "$PPP_INTERFACE" ]
do
PHY_PPP=$(ifconfig | grep '^ppp[0-9]' | awk '{print $1}')
sleep 1
done

#
# 4. Release rfcomm0 binding.
#
if [ -e "/dev/$RFCOMM_DEVICE" ]; then
echo "Releasing $RFCOMM_DEVICE binding.."
rfcomm release $RFCOMM_DEVICE
fi

echo "Disconnected."

exit 0

Post a Comment