LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Problem bash script to check the internet connection? (http://www.linuxquestions.org/questions/programming-9/problem-bash-script-to-check-the-internet-connection-4175433467/)

shams 10-22-2012 06:09 AM

Problem bash script to check the internet connection?
 
I need a bash script that check the internet connection if not connected will run the pon, it will try 3 times with delay of 60 seconds if fail to connect then should shutdown the computer.
This is script i get from the net and modefied, just making one try not the 3 times and don't know how put the shutdown command:

Code:

#!/bin/bash

#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  #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

  for i in `seq 3`;
 do

#output to log
  echo "" >>$logfile
  echo -n "Not Connected - " >> $logfile
  date >> $logfile

# disconnect all instances of ppp and then reconnect
#not that you cannot have &>> to append both stderr and stdout
#so I redirected stderr to stdout and then appended the stdout to the log
  poff -a 2>&1 >> $logfile

#switch dsl-provider with your connection name
  pon 2>1 >> $logfile
  echo "conneting wait ..."
  sleep 60

  exit 1
 done
 fi
  exit 0


Rupadhya 10-23-2012 04:23 PM

On Fedora (and perhaps others) there is a command line program called shutdown. Try this.

Code:

$ which shutdown
/usr/sbin/shutdown

Then you have shutdown. Which you can invoke in your script as:
Code:

shutdown now
Which means shutdown immediately. If you add a -r it will reboot your machine.

- Raj

Habitual 10-23-2012 04:54 PM

Code:

halt
is pretty common across distros...

David the H. 10-25-2012 02:14 PM

The script really needed a bit of cleaning, so I ended up rewriting it almost from scratch. I hope I followed your requirements correctly.

I removed most of your comments, but added some of my own where I made major changes.


Code:

#!/bin/bash

#data should ideally not be hardcoded into a script,
#so set up the site name here as well.
logfile=/home/user/pppd/con.log
sitename=google.com


#(code that's run multiple times, such as poff/pon, could be set up here)
#(in functions, too.  But it's a simple script, so not really necessary.)


#***main operation***#
#if ping test succeeds, log it.
#else if it fails, run the reconnect and/or restart.

if ping -c 1 "$sitename" &>/dev/null ; then

    echo >> "$logfile"
    echo "Already Connected - $( date )" >> "$logfile"

else

    while (( ++i <= 3 )); do

        echo >> "$logfile"
        echo "Not Connected - $( date )" >> "$logfile"

        poff -a 2>&1  >> "$logfile"
        pon -a 2>&1 >> "$logfile"

        echo "connecting wait ..."

        sleep 60

    done

    #now try pinging again, and shut down if it fails.
    #halt probably needs to be run as root though -- sudo?
    #no need to specify an exit code, since we're shutting down everything.

    if ping -c 1 "$sitename" &>/dev/null ; then

        echo "Succeeded in reconnecting.  Exiting."

    else

        echo "Attempts to reconnect failed.  Shutting down the system."
        halt

    fi

fi

exit 0


Rupadhya 10-25-2012 03:24 PM

The script works for me, although I don't have PPP so it isn't executing
poff -a 2>&1 >> "$logfile"
pon -a 2>&1 >> "$logfile"


The rewrite looks good to me.

- Raj

David the H. 10-25-2012 05:47 PM

Quote:

Originally Posted by Rupadhya (Post 4814909)
The script works for me, although I don't have PPP so it isn't executing

Actually neither do I. I can check the script syntax, but I have to take it on faith that the commands in it do what I imagine they do.


As for those two commands, you could probably even enclose them both in a single command grouping, and redirect the entire output at once, like this:

Code:

{ poff -a && pon -a ;} 2>&1 >>"$logfile"

shams 10-26-2012 09:24 PM

Thanks David the H. for the script, the script has some problems, first when the ping is successfull the script didn't free the shell until i press the enter.
Second big problem is when internet is not connected the script connecting the isp with the first attempt but when the sleep time passed the script didn't exit but the "poff -a" command again disconnecting the connection and then while loop trying it is second attmpt and so on, means with the successfull connection the script didn't exit and while loop is running for the 3 times.

shams 10-28-2012 10:12 AM

Problem bash script to check the internet connection?
 
Can you please modify this script little more, after the second try in loop the script will run some defferent commands:

Quote:

poff -a
setserial
rmmod cdc_acm
modprob cdc_acm
pon
If the above commands fails to connect to the isp then will shutdown the computer.

David the H. 10-28-2012 02:53 PM

Quote:

...when the ping is successfull the script didn't free the shell until i press the enter.
I'm not sure why this would happen. AFAICT, the script should simply continue to the end after a successful test. If worse comes to worst, just stick another 'exit 0' in the appropriate place to force it to exit there.

Quote:

...means with the successfull connection the script didn't exit and while loop is running for the 3 times.
Yeah, I see where I went wrong there. Move the ping test into the while loop, and use break to exit the loop, (or exit to leave the script entirely, as I said above) if it successfully reconnects.


Code:

  while (( ++i <= 3 )); do

        echo >> "$logfile"
        echo "Not Connected - $( date )" >> "$logfile"

        poff -a 2>&1  >> "$logfile"
        pon -a 2>&1 >> "$logfile"

        echo "connecting wait ..."

        if ping -c 1 "$sitename" &>/dev/null ; then

            echo "Succeeded in reconnecting.  Exiting."
            break    #or 'exit 0'

        else

            echo "Attempts to reconnect failed.  Shutting down the system."
            halt

        fi

        sleep 60

    done


As for your new requirement, simply add a test so that it runs different commands if i is equal to 2 (or 3?). I recommend using a case statement instead of if's; it's cleaner and more efficient. But I'll leave it up to you to implement. You should do at least some of the work yourself, right? ;)

shams 10-28-2012 09:27 PM

Thanks again for the reply, sorry but the script got more problems, when the net is disconnected and run the script just execute the all commnds to the last and shutting down the computer in the first attempt and the loop running 3 times, i change the halt to the echo so not realy shutdown the pc:

Quote:

connecting wait ...
Attempts to reconnect failed. Shutting down the system.
shutting down the pc...action taken
connecting wait ...
Attempts to reconnect failed. Shutting down the system.
shutting down the pc...action taken
connecting wait ...
Attempts to reconnect failed. Shutting down the system.
shutting down the pc...action taken


All times are GMT -5. The time now is 11:12 AM.