#!/bin/bash # psuedocode (with if thens and gotos vs. while dos -- should revise) # maxtries = 3 (??) # count = 0 # start: ping ISP (with parameters to limit to say 4 pings instead of continuous) # count = count + 1 # if icmp echo request == 'yes' # then # fetchmail # sendmail -q # else # if count <= maxtries (wow, do I forget valid syntax ;-) ) # then # wait 30 (??) (give the modem a chance to connect) # goto start # else # issue "Internet connection lost, could not restart" message # fi # fi # pseudocode # let "count = 0" # success=false # while let "count <= 3" # do # let "count = $count + 1" # ping -c 4 206.245.176.211 # # if [[ `grep -i " 0% packet loss" $HOME/rhkmailping.txt` ]] \ # then # # Ping successful -- connected and no wrong bytes # fetchmail # # Fetchmail complete success uncertain -- for now, # # assume we're successful if we get here # success=true # # check for outgoing mail in queue before kicking # mailq # # rhksendmailq is a file in /bin with permissions -rwsr--r-x root root # # (i.e., owned by root, executable by anyone, and suid) # # containing /usr/sbin/sendmail -q > /home/dad/temp.txt # # to make it more secure, I should consider making it executable by the # # dad group only (or similar) # rhksendmailq # kick mail queue # break # the while loop # else # sleep 30 # give modem a chance to connect after pinging # fi # done # # check mailq whether connected or not # mailq # # if not success # # then # # log "Mail not retrieved" at date, time # # fi # alternate logic (without goto and added test of mailq): # dadmail.log must exist before this program is run # /bin/rm $HOME/dadmail.log if ! test -e $HOME/dadmail.log ; then touch $HOME/dadmail.log ; fi echo /n >> $HOME/dadmail.log echo `date` Starting ipr_getmail.scr >> $HOME/dadmail.log let "count = 0" success=false while let "count <= 3" do let "count = $count + 1" ## ip address in next line is my ISP's nameserver -- replace ping -c 4 206.245.176.211 > $HOME/rhkmailping.txt cat $HOME/rhkmailping.txt echo `date` Ping results: >> $HOME/dadmail.log cat $HOME/rhkmailping.txt >> $HOME/dadmail.log echo `date` `grep -i " 0% packet loss" $HOME/rhkmailping.txt` >> $HOME/dadmail.log echo `date` `grep -i "wrong" $HOME/rhkmailping.txt` >> $HOME/dadmail.log if [[ `grep -i " 0% packet loss" $HOME/rhkmailping.txt` ]] \ # && ![[ `grep -i "wrong" $HOME/rhkmailping.txt` ]] #Ignore wrong bytes for now then echo Connected # and no wrong bytes" echo `date` Ping successful >> $HOME/dadmail.log fetchmail > $HOME/temp.txt cat $HOME/temp.txt cat $HOME/temp.txt >> $HOME/dadmail.log echo `date` Fetchmail complete success uncertain >> $HOME/dadmail.log ## test for fetchmail success and set success = true ## for now, assume we're successful if we get here success=true ## check for outgoing mail in queue before kicking ## revise next line for several reasons -- ideally ## * show mailq output on standard out so it is incorporated ## in mail from cron ## * and log to a log file (which file?) echo `date` connected -- mailq before sendmail >> $HOME/dadmail.log mailq > $HOME/temp.txt cat $HOME/temp.txt cat $HOME/temp.txt >> $HOME/dadmail.log # rhksendmailq is a file in /bin with permissions -rwsr--r-x root root # (i.e., owned by root, executable by anyone, and suid) # containing /usr/sbin/sendmail -q > /home/dad/temp.txt # to make it more secure, I should consider making it executable by the # dad group only (or similar) rhksendmailq # kick mail queue echo `date` results of sendmail -q >> $HOME/dadmail.log cat $HOME/temp.txt cat $HOME/temp.txt >> $HOME/dadmail.log break # the while loop else sleep 30 # give modem a chance to connect after pinging fi done ## check mailq whether connected or not echo `date` mailq after sendmail or not connected >> $HOME/dadmail.log mailq > $HOME/temp.txt cat $HOME/temp.txt cat $HOME/temp.txt >> $HOME/dadmail.log # if not success # then # log "Mail not retrieved" at date, time # fi # echo `date` TEST >> dadmail.log # an old ping # ping -c 4 206.245.176.211 | grep -c "0% packet loss" # old grep "| grep "64 octets" -A 4 |" # ping -c 4 206.245.176.211 | grep "64 octets" -A 4 | grep -c "0% Packet Loss" # Another tidbit: # if [[ `mailq | tee mailq.txt | grep -c 'is empty'` ]] ; then echo 'yes' ; else echo 'no' ;fi # cat mailq.txt # This doesn't work right, sometimes 0% packet loss but wrong bytes # if [[ `ping -c 4 206.245.176.211 | grep -i " 0% packet loss" | tee temp.txt` ]] # then # echo "Not connected" # sleep 30 # give modem a chance to connect after pinging # else # echo "Connected"