Register
It is currently Sun Dec 21, 2014 9:06 pm

Problem bash script to check the internet connection?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Oct 24, 2012 8:47 pm   

Joined: Mon Oct 22, 2012 5:17 am
Posts: 2
I need a script to check the dialup internet connection if it is disconnected will run the pon, when get connected exit the script, but if connection fails will try 3 times to connect, finally after third fail of try sould shutdown the computer. this is the one i get but it is not working and i want to be simple pretty script:

Quote:
#!/bin/bash

WTIME="60"

#here I defined the logfile's location
logfile=/home/user/pppd/con.log

#the heart of this script lies right here
#just ping google.com with one packet, pipe it's stderr to egrep and match either of those words
#egrep's -c tells to output the number of matches
status=$(ping -c 1 google.com 2>&1 | egrep -c "\<unknown\>|\<unreachable\>")

if [ $status -eq 0 ]; then

#the -n says stay in this line after outputting
#print an empty line
echo "" >> $logfile
echo -n "Already Connected - " >> $logfile

#output date to the logfile
date >> $logfile

#exit with success code

exit 0
fi

if [ ! $status -eq 0 ]; then

echo "" >>$logfile
echo -n "Not Connected - " >> $logfile
date >> $logfile
poff -a 2>&1 >> $logfile
pon 2>1 >> $logfile
sleep $WTIME
exit 1
else
exit 0
fi


if [ ! $status -eq 0 ]; then

echo "" >>$logfile
echo -n "Not Connected - " >> $logfile
date >> $logfile
poff -a 2>&1 >> $logfile
pon 2>1 >> $logfile
sleep $WTIME
exit 1
else
exit 0
fi

if [ ! $status -eq 0 ]; then

echo "" >>$logfile
echo -n "Not Connected - " >> $logfile
date >> $logfile
poff -a 2>&1 >> $logfile
pon 2>1 >> $logfile
sleep $WTIME
exit 1
else
exit 0
fi

if [ ! $status -eq 0 ]; then
echo "Connection failed shutting down pc- $(date +%Y-%m-%d-%H:%M:%S)" | mail -s "pon.sh" root@localhost
/sbin/shutdown -h now
fi
exit 0


Top
 Profile  
 PostPosted: Tue Nov 06, 2012 7:50 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Not my best work as for a start it has no logging however it may provide you with some help;

Code:
#!/bin/bash

wtime=60
remotehost=google.com

# function to test connection status
function pingtest {
  # we don't need to examine what ping actually says, just success or failure
  ping -qc 3 "$remotehost" >/dev/null
  return $?
}

# function to attempt reconnection
function reconnect {
  # I don't use these so not tested at all and you'll want logging
  poff -a 2>/dev/null >/dev/null
  sleep 1
  pon 2>1 >/dev/null
  sleep $wtime

  # now return connection status
  pingtest
  return $?
}

# now the main program
if ! pingtest
then

  # attempt to reconnect
  if ! reconnect
  then

    # attempt reconnect second time
    if ! reconnect
    then

      # try reconnect third time
      if ! reconnect
      then
        echo "Connection doomed to failure"
      fi

    fi # close second connection if
  fi # close first connection if

fi # pingtest if


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 1 guest


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 post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP