Solved

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

Posted on 2013-01-24
4
224 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
4 Comments
 
LVL 5

Accepted Solution

by:
Dilip Patidar earned 500 total points
Comment Utility
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
Comment Utility
#!/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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now