Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
does anyone know a good tool/way to ping sweep multiple devices and report and the devices that are down? I have a txt file with numerous IPs (separated by carriage) that i need to scan. I want to know which devices are currently down.
I use nmap but it only shows the machines that are up.
You can do "ping -c 1 <hostname>" to send a single ping to a host.
You can run "echo $?" to see the return code for the ping. A return code of 0 means successful and a return code of 1 (or any greater than 0) means it was unsuccessful.
So if your list of hosts is named "list" then you could do a for loop to test each one:
Code:
for host in $(cat list)
do ping -c 1 $host >/dev/null 2>&1
if [ $? -gt 0 ]
then echo $host is down
else echo $host is up
fi
done
The redirect to /dev/null prevents the output of the ping command from showing up so you only see the output of your script.
Note that ping doesn't necessarily prove a host is "down". It might be a bad switch port, network card, network cable or other things that prevent it from being accessed. You could also put in a longer ping count with the -c to try it multiple times before reporting it "down".
We use it extensively here to monitor almost everything. You can check many things other than a simple ping such as filesystem states, service/process states, DB sizes and connectivity, web pages and almost anything you can think of. You can even design checks that work in lieu of standard ping (e.g. some Windows clusters have the same IP active on both nodes at the same time which causes the ICMP ping to apppear as failing because the return code is non-zero.)
i have a (rather) large bash script that does something similar. i have it take into account duplicate packets, damaged packets, and then yesterday i found a new one, filtered content or something...
this won't make a whole lot of sense without the rest of it, but here's an example. the MIP is the machine ip, MNAME is machine name (defined above this portion, obviously). there's error checking and stuff in the if / else parts, but i took that out of here. just defines the error type and the loss amount / type. then there are different settings for whether it's 100% loss, greater-than-acceptable (defined by a limit set in the file), ANY (greater than 0 but less than what's acceptable) and so on
Code:
# note: no need to do a -w unless you're flooding; -c "x" will be 1/sec anyway
echo -e "\nPinging machine $MNAME"
LOSSLINE=`ping -c ${PING_COUNT} $MIP | grep loss`
echo "LOSSLINE is $LOSSLINE"
##########
# If there are duplicates, get the duplicates and the loss
# report duplicates as an option, along with loss
echo $LOSSLINE | grep duplicates
DC="$?"
if [ "$DC" = "0" ]
then
# There were duplicate packets
else
echo $LOSSLINE | grep errors
ER="$?"
if [ "$ER" = "0" ]
then
# There were errors in the packets
else
echo $LOSSLINE | grep damaged
DM="$?"
if [ "$DM" = "0" ]
then
# There were damaged packets
else
# No duplicates, damaged, or errors (normal operation)
fi
fi
fi
# This line is to make sure that the value for LOSS is actually a number
expr $LOSS / 1 > /dev/null 2>&1
PC="$?"
if [ "$PC" != "0" ] && [ "$PC" != "1" ]
then
# No matter what, everything here will mean complete
# loss for our purposes. Just need to trap the reasons
it does this in a while loop of however many hosts are defined in the variables above this part and sends the appropriate errors, whether it's email only or (in my case) sends to an oracle dtabase to report the error. there are various exit points along the way in case it encounters errors with this, that, or the other. there are a few redundant things in here, especially the way it checks the amount of variables a few times, but i wanted to minimize the error points
Code:
set | grep -v '=$' | grep ^MACHINE > $PLIST
PCOUNT=`cat $PLIST | wc -l`
# Initialize primary loop
p=1
while [ "$p" -le "$PCOUNT" ]
do
LINE="`cat $PLIST | head -$p | tail -1`"
MIP="`cat $PLIST | head -$p | tail -1 | awk -F\= '{print $2}' | awk -F\, '{print $1}'`"
check_ip
if [ -z "$OC_ER" ]
then
cat $PLIST | head -$p | tail -1 >> $MLIST
fi
p=$[$p+1]
done
i have it creating files in its own tmp folder so that if there's a point of failure i can look at it and send it to someone if they don't believe me
Last edited by glamiss; 05-24-2012 at 02:23 PM.
Reason: adding info
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.