LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2012, 10:13 PM   #1
linux2man
Member
 
Registered: Oct 2006
Posts: 34

Rep: Reputation: 16
Check ping result and restart


Hello,
I want to check defined IP reachability from my host every 3 second and if it's unreachable log the data/time in file and restart only eth0 interface and if it's reachable log it in another file

Any ideas please
 
Old 08-26-2012, 11:41 PM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
First, why restart eth0 if the destination address is unreachable? This does not necessarily mean that eth0 is down. It could be that the host is unreachable for a number of different reasons.

That being asked, there a two ways of doing this. The first is the simplest and can be achieved by a script that runs the ping command every 3 seconds and logs the result. You can then set this script to automatically run when you log in. Here is a simple script (assuming you're using BASH):
Code:
#! /bin/bash
dest_IP='a.b.c.d'
success_file='/path/to/success/log/file'
fail_file='/path/to/fail/log/file'

while true
  ping -q -c1 "$dest_IP" > /dev/null
  if [ $? = 0 ]; then
    echo "$dest_IP reached on $(date)" >> "$success_file"
  else
    echo "$dest_IP unreachable on $(date)" >> "$fail_file"
    ifconfig eth0 down    #Assumes you're using ifconfig and not NetworkManager
    ifconfig eth0 up
  fi
  sleep 3s    #Wait 3 seconds
loop
Because of the ifconfig, you may need to run this as root.

The second and what I believe is the better way, is to create cronjob. First create a script as above, but slightly modified:
Code:
#! /bin/bash
dest_IP='a.b.c.d'
success_file='/path/to/success/log/file'
fail_file='/path/to/fail/log/file'

ping -q -c1 "$dest_IP" > /dev/null
if [ $? = 0 ]; then
  echo "$dest_IP reached on $(date)" >> "$success_file"
else
  echo "$dest_IP unreachable on $(date)" >> "$fail_file"
  ifconfig eth0 down    #Assumes you're using ifconfig and not NetworkManager
  ifconfig eth0 up
fi
Now create a crontab file with the crontab -e command:
Code:
# This crontab file runs the script that pings a particular host every 3 seconds
#
*/3 * * * * /absolute/path/to/ping/script
The cronjob will run your script every 3 seconds.

I have not tested the script so they may need tweaking.

How it puts on the right track.
 
Old 08-28-2012, 07:20 PM   #3
linux2man
Member
 
Registered: Oct 2006
Posts: 34

Original Poster
Rep: Reputation: 16
Thanks towheedm,
The cronjob will run every 3min, anyway loop appear fine
 
Old 08-29-2012, 08:45 PM   #4
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
Quote:
Originally Posted by linux2man View Post
Thanks towheedm,
The cronjob will run every 3min
That's true, sorry for that.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ping result?what it means. bruse Linux - Networking 1 12-14-2005 03:16 AM
ping result. bruse Linux - Networking 1 11-18-2005 04:53 PM
ping result. bruse Linux - Networking 1 11-18-2005 04:52 PM
can't get ping result netkepala Linux - Networking 2 09-04-2003 05:05 AM
Ping result analysis yuzuohong Linux - Networking 3 06-25-2003 08:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 05:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration