Solved

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

Posted on 2013-01-24
4
233 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
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

825 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