Solved

Check Interface state(up/down) and grep 'recieved' in ping

Posted on 2013-01-24
4
237 Views
Last Modified: 2014-10-27
Hi experts.

How can I accomplish the following:


if(eth0 recives icmp reply && interface eth1 is down)
ifconfig eth1 up
else
ifconfig eth1 down

I am using CentOS 5.6
0
Comment
Question by:databoks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 5

Accepted Solution

by:
Dilip Patidar earned 500 total points
ID: 38814212
Hello,

You can try this one.

If ping succeeds then bring $iface up.

If ping fails then bring $iface down.



#!/bin/bash


timeout=3               # delay between checks
iface="eth0"            # which interface to bring up/down
pingip='8.8.8.8'        # what to ping
isdown=-1               # indicate whether the interface is up(0) or down(1)
                         # start in unknown state

while true; do
    if ping -q -c 2 "$pingip"; then       # if ping is succeeds bring interface up
        if [ "$isdown" -ne 0 ]; then      # if not already up
            ifconfig "$iface" up && isdown=0
            printf ":: iface brought up: %s\n" "$iface"
        fi
    elif [ "$isdown" -ne 1 ]; then        # if ping failed, bring interface down, if not already down
        ifconfig "$iface" down && isdown=1
        printf ":: iface brought down: %s\n" "$iface"
    fi
    sleep "$timeout"
done

Thanks And Regard's
Dilip Patel
0
 
LVL 3

Expert Comment

by:tikejhya
ID: 38873031
#!/bin/bash
ping -c 2 'xxx.xxx.xxx.xxx' > /dev/null
if [ $? -eq 0 ]; then
echo "Alive";
else
echo "Dead";
fi


You can put forloop of ips.


IPS='1.1.1.1 2.2.2.2 3.3.3.3'
for ip in IPS;do
{
ping -c 2 '$ip' > /dev/null
if [ $? -eq 0 ]; then
echo "Alive";
else
echo "Dead";
fi
}
done
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question