Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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