Script help needed

Live forum: http://rt2x00.serialmonkey.com/viewtopic.php?t=5158

executivul

12-01-2009 11:53:48

Hello everybody,

I'm asking you to help me with the following problem
I a few computers that are mobile (on vehicles), using the Edimax EW-7318USg (rt73) USB network cards, I need a script (bash) to act as a syncronization daemon, run in background, and when a certain network is discovered (my network) to run a rsync command. My network is an 802.11b/g network, using no security.
I need to connect to the network as fast as possible since I don't know if the vehicle will be around long enough to transfer all data. Or other vehicles will stay for hours and I don't want them to take all ip addresses from the dhcp server by reconnecting again and again.
I have written a simple skeleton but I'm afraid it doesn't cover all possible situations and treat all errors, I need it to be foolproof ), like treating "invalid" associated ap after a failed iwconfig command, etc.

Here is the thing I have until now
[code1xcwo05m]
#!/bin/bash

# suppress all output to default console
exec </dev/null >/dev/null 2>&1

# variables used
VER=4.6.0
HOSTNAM="MIRBUS"
SERVER="10.0.0.1"
DOWNLINK="/mnt"
NET="yes"
ESSID="ciwifi"
DHCP="yes"
WIFIIP="auto"
WIFINETMASK="auto"
WIFIGATEWAY="auto"
WIFIDNS="auto"
# color output variables
. /etc/init.d/rc.status

# if we have net enabled
if [[ $NET == "no" ]]; then
exit 0
fi

# output console
DEBUG="/dev/tty4"

# drivers (modules) are loaded

# testing if we have the network card, if we don't wait for a miracle :)
echo "${cgre}STEP 0:${cnorm} searching for the card:" >> $DEBUG
iwconfig >/tmp/wlan 2>/dev/null
while [ `cat /tmp/wlan | wc -l` -lt 5 ]; do
echo " No card detected... ${rc_error}" >> $DEBUG
sleep 60
iwconfig >/tmp/wlan 2>/dev/null
done
echo " Card detected:" >> $DEBUG

#daemon
while true; do
CONEXIUNENOUA=0
# checking connection
echo "${cgre}STEP 1:${cnorm} Testing server connection:" >> $DEBUG
# try to ssh into server
ssh -o ConnectTimeout=40 $SERVER "date +'%Y/%m/%d %H:%M' >> $UPLINK/$HOSTNAM.last.txt" 2>/dev/null
retcod=$?
# trying to setup the connection
if [ $retcod -ne 0 ] ; then
# test to see if server denied client or it was a network error
if [ $retcod -eq 255 ]; then
echo " Not connected ${rc_attention}" >> $DEBUG
else
echo " Server denies client access, code: $retcod ${rc_error}" >> $DEBUG
fi
# setting up the connection
ifconfig wlan0 up
iwlist wlan0 scan > /tmp/wifiscan
echo "${cgre}STEP 2:${cnorm} Searching for $ESSID, here are the available networks:" >> $DEBUG
cat /tmp/wifiscan | grep ESSID >> $DEBUG
# while i'm not in the coverage area of my network i wait
# is it correct, will it cause trouble endlessly scanning ?????
while [ `cat /tmp/wifiscan | grep $ESSID | wc -l` -eq 0 ];do
ifconfig wlan0 up
iwlist wlan0 scan > /tmp/wifiscan
echo "${cgre}STEP 2:${cnorm} Searching for $ESSID, here are the available networks:" >> $DEBUG
cat /tmp/wifiscan | grep ESSID >> $DEBUG
sleep 15
done
echo " I am in the coverage area of $ESSID ${rc_done}" >> $DEBUG
# i try to connect to the network
iwconfig >/tmp/wlan 2>/dev/null
if [ `cat /tmp/wlan | grep ESSID | grep $ESSID | wc -l` -eq 0 ] || \
[ `cat /tmp/wlan | grep Not-Associated | wc -l` -gt 0 ] || \
[ `cat /tmp/wlan | grep Invalid | grep -v "Invalid misc:" | wc -l` -gt 0 ]; then
echo "${cgre}STEP 3:${cnorm} I try to connect to the network $ESSID" >> $DEBUG
# sometimes i can connect only by issuing the iwconfig with the interface up, other times with the interface down
# so i try both methods, is it correct,will it cause trouble ??????
ifconfig wlan0 down
iwconfig wlan0 essid $ESSID ap auto
ifconfig wlan0 up
iwconfig wlan0 essid $ESSID ap auto
iwconfig >/tmp/wlan 2>/dev/null
CONEXIUNENOUA=1
cat /tmp/wlan | grep ESSID >> $DEBUG
fi

# if i don't have an ip i'm trying to get one
if [ `ifconfig wlan0 | grep "inet addr" | wc -l` -lt 1 ] || [[ $CONEXIUNENOUA -eq "1" ]]; then
echo "${cgre}STEP 4:${cnorm} Trying to get an IP:" >> $DEBUG
# kill the dhcpcd daemon or just wait for it to do it's job? what is the correct answer?
# the dhcpcd can not be pursued to "refresh" the ip, that's why i kill it
dhcpcd -k wlan0 2>&1 >/dev/null
killall dhcpcd
rm -f /var/run/dhcp*
rm -f /var/lib/dhcpcd/*
dhcpcd -t 20 wlan0 2>&1 >/dev/null
if [ $? -gt 0 ]; then
echo " Dhcpcd error, code: $? ${rc_error}" >> $DEBUG
fi
ifconfig wlan0 | grep "inet addr" >> $DEBUG
fi
else
echo " Connected to server ${rc_done}" >> $DEBUG
echo "${cgre}STEP 5:${cnorm} Syncronizing:" >> $DEBUG
#updatez clipuri si playliste
/usr/bin/rsync -vrtz --modify-window=2 --delete --timeout=60 $SERVER:$DOWNLINK /mnt 2>/dev/null >$DEBUG
if [[ $? -eq 0 ]]; then
echo " Sync waas ok ${rc_done}" >> $DEBUG
echo >> $DEBUG
else
echo " Sync error, code: $? ${rc_error}" >> $DEBUG
fi
#wait 5 minutes and do it all over again
sleep 300
fi
# time between retries
sleep 5
done

exit 0
[/code1xcwo05m]

Me being a C/Php programmer I tend to use the if/then/else statement more than Bash style of scripting based on exit codes, break, etc.

Any help in creating a better script is highly appreciated.

Thank you