Bash script to detect nonpingable hosts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to detect nonpingable hosts
# 1  
Old 06-02-2013
Bash script to detect nonpingable hosts

I have a script to detect if a host is pingable or not. The problem is that I would like it to put the nonpingable hosts in one file and the pingable hosts in another. I have come up with this so far:

Code:
for ip in `cat /tmp/testlist2`; do ping -c 3 $ip >/dev/null && echo "$ip is up" || echo "$ip is down"; done>/tmp/s4.file
cat /tmp/s4.file |grep -i down >/tmp/f1.file
cat /tmp/s4.file |grep -i up >/tmp/sup.file

This works but it is clumsy. If I try to just do a redirect after the ping it doesn't work, though. What I get is only the first host name that the ping is tried on. Would I need another loop to get the hosts in two different files? Any suggestions?
# 2  
Old 06-02-2013
Here is a port of ping test I did make some time ago, maybe you can use it.
Code:
[[ "$(ping -c 1 -w 192.168.0.33 | awk '/received / {print $4}')" == "0" ]] && echo "nok" || echo "ok"


Example: You can change output of up/down to separete files if you like
Code:
#!/bin/bash
for ip in $(cat /tmp/testlist2); do
        if [[ "$(ping -c 1 -w 1 $ip | awk '/received/ {print $4}')" == "0" ]]
        then
                echo "$(date) $ip is down" >> /tmp/log
        else
                echo "$(date) $ip is up" >> /tmp/log
        fi
done


Last edited by Jotne; 06-02-2013 at 03:14 PM..
This User Gave Thanks to Jotne For This Post:
# 3  
Old 06-02-2013
Quote:
Originally Posted by newbie2010
I have a script to detect if a host is pingable or not. The problem is that I would like it to put the nonpingable hosts in one file and the pingable hosts in another. I have come up with this so far:

Code:
for ip in `cat /tmp/testlist2`; do ping -c 3 $ip >/dev/null && echo "$ip is up" || echo "$ip is down"; done>/tmp/s4.file
cat /tmp/s4.file |grep -i down >/tmp/f1.file
cat /tmp/s4.file |grep -i up >/tmp/sup.file

This works but it is clumsy. If I try to just do a redirect after the ping it doesn't work, though. What I get is only the first host name that the ping is tried on. Would I need another loop to get the hosts in two different files? Any suggestions?
You could try something like:
Code:
rm -f /tmp/f1.file /tmp/sup.file
for ip in $(cat /tmp/testlist2)
do      ping -c 3 $ip >/dev/null && echo "$ip is up" >> /tmp/sup.file || echo "$ip is down" >> /tmp/f1.file
done


Last edited by Don Cragun; 06-02-2013 at 02:50 PM.. Reason: Reversed the redirections...
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 06-02-2013
A bit more elegant, store only hosts:
Code:
file=/tmp/testlist2
while read ip
do
 ping -c 3 $ip >/dev/null &&
 echo $ip
done < $file > $file.up
fgrep -vxf $file.up $file > $file.down

# 5  
Old 06-02-2013
One snag in that post MadeInGermany.

Hosts that are a substring of others will cause mismatch with grep (eg DEV and DEV01)

Last edited by Chubler_XL; 06-02-2013 at 05:49 PM.. Reason: Removed statement about ping exit code - seems to set error condition if no packets received
# 6  
Old 06-02-2013
And therefore the -x option!
Another variant with descriptor magic:
Code:
file=/tmp/testlist2
while read ip
do
 ping -c 3 $ip >/dev/null &&
 echo $ip ||
 echo $ip >&3 
done < $file > $file.up 3> $file.down

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

How to copy a tar file on a series of remote hosts and untar it on those hosts?

Am trying to copy a tar file onto a series of remote hosts and untar it at the destination. Need to do this without having to do multiple ssh. Actions to perform within a single ssh session via shell script - copy a file - untar at destination (remote host) OS : Linux RHEL6 (3 Replies)
Discussion started by: sankasu
3 Replies

2. Shell Programming and Scripting

How to detect url in use in a script?

Hello, I have a small script and it runs from web application in below format: pipe:///path_to_myscript.sh url1 url2 url3 myscript.sh: #!/bin/bash count=0 while do count=$((count+1)) exec 3>&1 ((ffmpeg -i $1 ...... -f mpegts pipe:1 2>/dev/null 1>&3 ) 2>&1 | \ while read LINE; do echo... (9 Replies)
Discussion started by: baris35
9 Replies

3. UNIX for Advanced & Expert Users

A script to detect system type

Hi forum, So I am trying to determine the OS type with the following script: #!/usr/bin/sh OStype1=`uname -s` Sunos1=SunOs if then echo "This system is Linux" exit 0 elif then echo "This system is SunOs" exit 0 elif (1 Reply)
Discussion started by: dampio
1 Replies

4. Shell Programming and Scripting

Detect DST from a date entered by user in bash

I am trying to write a bash script that takes in a users input of a specific date in a format such as MM/DD/YYYY and returns whether or not that specific date was during daylight savings time or not. Is there a specific function that checks this? Does the date command have a way to do this? I am... (1 Reply)
Discussion started by: conman1985
1 Replies

5. Shell Programming and Scripting

Check connectivity with multiple hosts - BASH script available here

Hi everyone! Some time ago, I had to check connectivity with a big list of hosts, using different formats (protocol://server:port/path/, server:port, ....). I developed a script that checks the connectivity using different commands (ping, telnet, nc, curl). It worked for me so I'm sharing it... (9 Replies)
Discussion started by: Fr3dY
9 Replies

6. Shell Programming and Scripting

How to write bash script for creating user on multiple Linux hosts?

I wonder whether someone can help me with what I'm trying to achieve Basically, the objective is one script to create new user on more than 70 linux hosts if required. Everything works apart from the highlighted part. It gave me an output passwd: Unknown user name ''. when try to set... (35 Replies)
Discussion started by: fugeulu
35 Replies

7. AIX

aix tcp wrappers hosts.allow hosts.deny?

hi all just installed the netsec.options.tcpwrapper from expansion pack, which used to be a rpm, for my aix 6.1 test box. it is so unpredictable. i set up the hosts.deny as suggested for all and allow the sshd for specific ip addresses/hostnames. the tcpdchk says the hosts allowed and... (0 Replies)
Discussion started by: wf201626
0 Replies

8. Shell Programming and Scripting

BASH Script to Detect and List USB Flash Drives

Hello. This is my first post to this forum. I've read many of the posts over the last two or three years and I've learned a lot. I'm creating a live Linux distribution using the Linux Live Scripts -- just as a hobby project -- and I'm wanting to create an automated way for a user to copy the... (7 Replies)
Discussion started by: godzillarama
7 Replies

9. UNIX for Dummies Questions & Answers

how to detect my script is already running

I have a script which must not be run more than once at any given time. THis script will be scheduled to run every 20 mins as a cron job. In my script can i have logic to say if this script is already running from the previous cron, then exit. How do i go about doing that. If you describe the... (11 Replies)
Discussion started by: rmulchandani
11 Replies

10. UNIX for Dummies Questions & Answers

hosts.allow & hosts.deny

Hi! Im trying to use host.allow & host.deny to resrtic access to my sun machine, but it doesnt seem to work... I want to allow full access from certain IPīs (ssh,http,ftp,etc...) but deny all kind of conections from outsideworld, the way that im doing that is: hosts.allow ALL:127.0.0.1... (2 Replies)
Discussion started by: Sorrento
2 Replies
Login or Register to Ask a Question