views:

217

answers:

1

I'm very low with shell scripts..

I need to check with a cron (no problem for this) if my connection is up. If it isn't, i want to call some scripts to reconnect.

I was thinking about using grep and ping to some site (or ip), then check the returned string.

Then call my command:

sudo pppd call speedtch

I need a clue thanks! Or there are better methods to do this?

Since my call need sudo, there's a batch way to call it without password input?

+2  A: 

pinging works well, use this command line:

if ping -W 5 -c 1 google.com >/dev/null; then
    echo "Internet is up."
fi

This uses the following command line arguments to ping:

  • -W 5: Time out waiting for a response after five seconds.
  • -c 1: Only send one ping request. Default of most ping implementation is to send unlimited packages.
  • Also the output of ping is redirected to /dev/null to avoid useless mails from cron.

As for your sudo question, you can configure that in the sudoers file.

Use the following line in sudo:

fromuser     ALL=(root) NOPASSWD: /usr/bin/mycommand

You'll need to replace the following strings:

  • fromuser: Replace with the user who runs the cronjob
  • /usr/bin/mycommand: Replace with the path of the command you want to execute

With this configuration the user "fromuser" will always be able to execute /usr/bin/mycommand without entering a password. You'll want to be careful with that of course for security reasons.

Patrice
can you explain me this? ping -W 5 -c 1 google.com >/dev/nullthank you!
avastreg
I edited the answer to include the used command line options. Hope that helps.
Patrice
ok thanks again..another question: if my connection is not up, then `ping -W 5 -c 1 google.com >/dev/null;` return false?i don't explain to me how you can check the "if" if you send all the output to /dev/null.. :(
avastreg
Every Unix command has an exit status. It's 0 in the case of success and any other value otherwise. That exit status is what we check above and it has nothing to do with the standard output. Your shell usually does not show the exit status of a command. See http://tldp.org/LDP/abs/html/exit-status.html for details.
Patrice