Solved

bash script report if interface is not pingable

Posted on 2009-05-06
30
726 Views
Last Modified: 2013-12-26
Hi I need assistance with my script which main purpose would be to ping a set of interfaces and it it sees the output of "Destination Host unreachable it will report the NIC that is not reachable"

Thanx,

Michael
#!/bin/bash
 
INT=$(/sbin/ifconfig -a | awk '/(cast)/ { print $2 }' | cut -d':' -f2 | head -4)
PING=$(ping -c 5 -i 2 $INT")                                   
ADMINS=myemail@gmail.com
 
for i in $INT;
 do
   $PING|grep "Destination Host Unreachable" 
 if [ 
done

Open in new window

0
Comment
Question by:dpoper1
[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
  • 14
  • 13
  • 3
30 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 24319498


#!/bin/bash
 
INT=$(/sbin/ifconfig -a | awk '/(cast)/ { print $2 }' | cut -d':' -f2 | head -4)
PING=$(ping -c 5 -i 2 $i")                                  
ADMINS=myemail@gmail.com
 
for i in $INT;
 do
   $PING|grep "Destination Host Unreachable"
   if [ $? -eq 0 ]
   then
       echo "$i not reachable"
   fi
done
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 125 total points
ID: 24319528
correction

#!/bin/bash
 
INT=$(/sbin/ifconfig -a | awk '/(cast)/ { print $2 }' | cut -d':' -f2 | head -4)
#PING=$(ping -c 5 -i 2 $i")                                  
ADMINS=myemail@gmail.com
 
for i in $INT;
 do
   ping -c 5 -i 2 $i | grep "Destination Host Unreachable"
   if [ $? -eq 0 ]
   then
       mailx -s "$i not reachable" $ADMINS
   fi
done
0
 

Author Comment

by:dpoper1
ID: 24319667
Hi I have tried it, I have change the email address from myemail@gmail.com, I have brought down an interface in the server in which I am working on but I do not get the interface being down. I do not get any emails in my mailbox. Also the mail command works on my debian box.

Can you assist me in getting the script to work.

Thanx

#!/bin/bash
 
INT=$(/sbin/ifconfig -a | awk '/(cast)/ { print $2 }' | cut -d':' -f2 | head -4)
ADMINS=myemail@gmail.com
 
for i in $INT;
 do
   ping -c 5 -i 2 $i | grep "Destination Host Unreachable"
   if [ $? -eq 0 ]
   then
       mail -s "$i not reachable on $hostname" $ADMINS
   fi
done

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:omarfarid
ID: 24319714
can you run it as

bash -x file # put script file name

and post output
0
 

Author Comment

by:dpoper1
ID: 24319751
++ /sbin/ifconfig -a
++ awk '/(cast)/ { print $2 }'
++ cut -d: -f2
++ head -4
+ INT='10.5.0.4
192.168.10.123'
+ ADMINS=myemail@gmail.com
+ for i in '$INT'
+ ping -c 5 -i 2 10.5.0.4
+ grep 'Destination Host Unreachable'
+ '[' 1 -eq 0 ']'
+ for i in '$INT'
+ ping -c 5 -i 2 192.168.10.123
+ grep 'Destination Host Unreachable'
+ '[' 1 -eq 0 ']'


0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24319836
it is not getting into the then part which means the grep or ping are not correct. you may verify this part

ping -c 5 -i 2 $i | grep "Destination Host Unreachable"
0
 
LVL 5

Expert Comment

by:0ren
ID: 24319924
for i in $(ip a l | grep -w inet | awk '{print $2}' | sed 's/\/.*//g')
do
       ping -c 1 -W 1 $i > /dev/null
       [ $? != 0 ] && echo "$i is down" && mail -s "$i not reachable on $hostname"  myemail@gmail.com
done
0
 

Author Comment

by:dpoper1
ID: 24319998
The output of the command  ping -c 5 -i 2 $i            works but the problem is that I think is that if the output is given to a file then the grep works.


0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24320059
can you show the output of

ping -c 5 -i 2 $i

when interface is down
0
 

Author Comment

by:dpoper1
ID: 24320091
the script hangs on the first interface and it does not go to the second one, when I press CTRL-C goes to the next, and it reports all of the interfaces down
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24320319
change

ping -c 5 -i 2 $i

to

ping -c 2 -i 1 -W 2 $i
0
 

Author Comment

by:dpoper1
ID: 24320515
samething, here is the output

ping: unknown host addr:10.5.0.4
addr:10.5.0.4 is down
^C
(Interrupt -- one more to kill letter)
^Cping: unknown host addr:192.168.10.123
addr:192.168.10.123 is down
^C
(Interrupt -- one more to kill letter)
^Cping: unknown host addr:127.0.0.1
addr:127.0.0.1 is down
^C
(Interrupt -- one more to kill letter)
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24321871
does ping work from command line or it gives the same? are you using dns on your system? check the following files for correct config.:

/etc/resolv.conf

and

/etc/nsswitch.conf (see the hosts entry)
0
 

Author Comment

by:dpoper1
ID: 24321944
Hi, the command line ping works fine, I am not using dns everything is local.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24321974
can you show the output of the following commands from command line:

 ping -c 2 -i 1 -W 2 10.5.0.4
 echo $?
 
 ping -c 2 -i 1 -W 2 10.5.0.4  |  grep 'my name'
 echo $?
0
 

Author Comment

by:dpoper1
ID: 24321987
root@node04:~# ping -c 2 -i 1 -W 2 10.5.0.3                      
PING 10.5.0.3 (10.5.0.3) 56(84) bytes of data.
From 66.101.202.21 icmp_seq=1 Destination Net Unreachable
From 66.101.202.21 icmp_seq=2 Destination Net Unreachable

--- 10.5.0.3 ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1004ms

root@node04:~# ping -c 2 -i 1 -W 2 10.5.0.3  |  grep "unreachable"
root@node04:~#

Please take a look at the output


0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24322167
Please note the string "Destination Net Unreachable" is different from "Destination Host Unreachable", you need to change the grep string accordingly :

ping -c 2 -i 1 -W 2 10.5.0.3  |  grep -i "Destination Net Unreachable"
0
 

Author Comment

by:dpoper1
ID: 24322390
It works fine, but now how can we integrate that with the script
0
 
LVL 5

Expert Comment

by:0ren
ID: 24323245
did you try my script ?


#!/bin/bash
for i in $(ip a l | grep -w inet | awk '{print $2}' | sed 's/\/.*//g')
do
       ping -c 1 -W 1 $i > /dev/null
       [ $? != 0 ] && echo "$i is down" && mail -s "$i not reachable on $hostname"  myemail@gmail.com
done
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24324598
#!/bin/bash
 
INT=$(/sbin/ifconfig -a | awk '/(cast)/ { print $2 }' | cut -d':' -f2 | head -4)
#PING=$(ping -c 5 -i 2 $i")                                  
ADMINS=myemail@gmail.com
 
for i in $INT;
 do
   ping -c 2 -i 1 -W 2 10.5.0.3  |  grep -i "Destination Net Unreachable"
   if [ $? -eq 0 ]
   then
       mailx -s "$i not reachable" $ADMINS
   fi
done
0
 
LVL 5

Expert Comment

by:0ren
ID: 24324627
no need to change -W to 2 seconds.
if pinging  local interface anything above 1 second is as if the NIC is not working.
0
 

Author Comment

by:dpoper1
ID: 24329393
Hi I have setup 2 servers each with 2 interfaces and when I run the script I put the ip of the other server and it hangs while sending the email.

this the ouput of node 2

root@node02:~# ./ping.sh
From 10.5.0.2 icmp_seq=1 Destination Host Unreachable
From 10.5.0.2 icmp_seq=2 Destination Host Unreachable
 Interface 10.5.0.2 not pingable
^C
(Interrupt -- one more to kill letter)
^C^C^C^Croot@node02:~#


0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24332727
if the interface is down, it could be that your default gateway is on that subnet. what is the output of

netstat -rn
0
 

Author Comment

by:dpoper1
ID: 24344567
I am able to ping it, and it is within the subnet itself, I am able to ping it from the command line. My main concern is the script it does not provide the right output, and here is the output for the netstat -rn command

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt     Iface
192.168.10.0    0.0.0.0         255.255.255.0     U                  0 0           0       eth1
10.5.0.0             0.0.0.0         255.255.255.0     U                  0 0           0       eth0
10.10.0.0           0.0.0.0         255.255.0.0         U                  0 0           0        eth2
10.11.0.0           0.0.0.0         255.255.0.0         U                  0 0           0        eth3
0.0.0.0         192.168.10.1    0.0.0.0                UG                 0 0           0        eth1


0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24345065
the default gateway is through eth1, so if eth1 is down you can not reach outside your network. What output the script gives that you think is wrong?
0
 

Author Comment

by:dpoper1
ID: 24345088
Hi, let;s forget about the default gateway because I am not using any default gateway for the 10.5.x.x network only for the 192.168.X.X, so am able to ping interfaces and to bring them up and down.

there is a problem with the script, I would need your assistance sorting it out.

Regards,

Michael
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24346624
what is the problem with the script?
0
 

Author Comment

by:dpoper1
ID: 24355761
Hi for some reason the script hangs. Now I have a monitoring server that it will ping the interfaces on different hosts, now if an interface is down on a given host, it will test the rest of the interfaces and it will try to ssh into the node that will report interface "Unreachable", to that node out of the working interfaces.
Also it should provide 2 types of alerts, interface on server down, multiple interfaces on server down.

I will appreciate your input and/or suggestions.

Regards,


Michael
0
 

Author Comment

by:dpoper1
ID: 24356784
so far I have this to test the interfaces if they are up or down

#!/bin/bash
 
HOST1="192.168.10.120 10.5.0.1 10.10.0.1 10.11.0.1"
HOST2="192.168.10.121 10.5.0.2 10.10.0.2 10.11.0.2"
HOST3="192.168.10.122 10.5.0.3 10.10.0.3 10.11.0.3"
HOST4="192.168.10.123 10.5.0.4 10.10.0.4 10.11.0.4"
HOST5="192.168.10.124 10.5.0.5 10.10.0.5 10.11.0.5"
 
# Number of times to ping the Interfaces
 
#!/bin/bash
 
HOST1="192.168.10.120 10.5.0.1 10.10.0.1 10.11.0.1"
HOST2="192.168.10.121 10.5.0.2 10.10.0.2 10.11.0.2"
HOST3="192.168.10.122 10.5.0.3 10.10.0.3 10.11.0.3"
HOST4="192.168.10.123 10.5.0.4 10.10.0.4 10.11.0.4"
HOST5="192.168.10.124 10.5.0.5 10.10.0.5 10.11.0.5"
 
# Number of times to ping the Interfaces
 
PING=3
 
# Alert me when an interface in a server is down
SUBJECT="Ping failed"
EMAILID="michael.dellagranda@gmail.com"
for int in $HOST1 $HOST2 $HOST3 $HOST4 $HOST5
do
  count=$(ping -c $PING $int | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  if [ $count -eq 0 ]; then
    # 100% failed
    echo "Host : $int is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID
  fi
done

Open in new window

0
 

Author Comment

by:dpoper1
ID: 24357872
I have re-arrange the script in order for the script to let me know if the interface is internal external intercluster or intracluster. Now when it sends out messages I need to have the script determine if the ip starts with 10.5 then it is an internal ip, 10.10 an intercluster ip so on and so forth.

#!/bin/bash
 
EXTERNAL="192.168.10.120 192.168.10.121 192.168.10.122 192.168.10.123 192.168.10.124"
INTERNAL="10.5.0.1 10.5.0.2 10.5.0.3 10.5.0.4 10.5.0.5"
INTERCLUSTER="10.10.0.1 10.10.0.2 10.10.0.3 10.10.0.4 10.11.0.5"
INTRACLUSTER="10.11.0.1 10.11.0.2 10.11.0.3 10.11.0.4 10.11.0.5"
 
PING=3
DATE=$(date +"%T %m/%d/%Y")
SUBJECT="Ping failed"
ADMINS=myemail@gmail.com
 
for int in $EXTERNAL $INTERNAL $INTERCLUSTER $INTRACLUSTER
do
  count=$(ping -c $PING $int | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  if [ $count -eq 0 ]; then
    # 100% failed
     if [ 
     echo "Host : $int is down (ping failed) at $DATE" | mail -s "$SUBJECT" $ADMINS
  fi
done

Open in new window

0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CLI command keep running after close 7 56
AWK: Pythagoras script 6 35
Shell script issue 4 54
exchange, squid, proxy, linux 6 38
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…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

749 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