Ping from a list of hosts

Greetings,

I have typed a list of computer hosts on my network. I am looking to write a shell script that process this list pinging each host once then sending the output to another file. The list of host is a single column (one host per line). I am new to shell scripting. This is what I have come up with but it does not work.

#/bin/sh
for i in 'cat iplist'
do; ping -c 1 $i
done

When I run the script I send the stdout to another files such as:

imac%./script.sh > newfile.

I gather that the script I made does not know how to process each line in the file as a new ping command. I more familiar with VB scripting where I would treat the text files as a database or an array and process each item or a specific column. Thanks for any help

Tim

Message was edited by: bornpilot

Message was edited by: bornpilot

Message was edited by: bornpilot

imac, Mac OS X (10.5.7), emac

Posted on May 27, 2009 10:03 AM

Reply
5 replies

May 27, 2009 10:50 AM in response to bornpilot

you might try changing your ping to ping -c1 "${i}"

or just add something like this to your .bash_login and/or .bashrc and/or .profile:
alias scanlan="ping -c6 `ifconfig | grep broadcast | awk '{print $6}'` | grep icmp | awk '{print \$4}' | cut -f1 -d: | sort | uniq | xargs -n1 arp $1"

(I do a ping -c6 because I found that sometimes packet collisions (I am presupposing) kept me from hearing all replies with just ping -c1. The uniq cleared out duplicate responses. Also, I needed to escape the $4 in the awk ( \ $4 ) in my .bash_login in order for the command to work but when entering long hand at the command line, I didn't.)

May 27, 2009 11:22 AM in response to j.v.

Thanks for the suggestion but when I made the first change you suggested the computer shot me this error (ping: cannot resolve cat iplist: Unknown host). The file does live in the directory the script is located.

As for your second suggestion I would gather I would add the script to each host system? If that is the case that would not be posable in my solution.

The list I have created is rather long and I am running the ping to verify if the host exists on my network. I unable to visit each host physically.

Thanks for the suggestion on adding the ping count list to six.

Tim

May 27, 2009 11:39 AM in response to bornpilot

#/bin/sh
for i in 'cat iplist'
do; ping -c 1 $i
done


Looks like you just need to tweak your script in a few spots:
Insert a ! after #, swap the ' with `, and delete the semicolon:
<pre style="border: 1px solid #ddd; padding-left: .75ex; padding-top: .25em; padding-bottom: .25em; margin-top: .5em; margin-bottom: .5em; margin-left: 1ex; max-width: 30ex; overflow: auto; font-size: 10px; font-family: Monaco, 'Courier New', Courier, monospace; color: #444; background: #eee; line-height: normal">#!/bin/sh
for i in `cat iplist`
do ping -c 1 $i
done</pre>
--
Cole

May 28, 2009 1:11 PM in response to bornpilot

bornpilot wrote:
As for your second suggestion I would gather I would add the script to each host system?

No. Only to the .bash_login of the user account on the computer that you will be issuing the ping to the entire subnet, to get the IP address, MAC address, and hostname of every other computer on the subnet (you'll get the IP address of your computer, too, but it won't give MAC address and hostname because your own computer isn't in your computer's arp tables)

What this command does when it does the ping, it is pinging the IP address of the result of the stuff inside the backticks. That stuff is the result of an ifconfig command of the computer that you are on, filtering just the broadcast IP address out of all the junk returned by ifconfig. The 6th field of the broadcast line of ifconfig is the broadcast address. So you're pinging the broadcast address and everybody on the subnet responds. The icmp "grep" filter rejects lines that for whatever reason don't have icmp in them (can't give any examples but ran across some when I was building this way back when). The awk '{print $\4}' extracts the IP address of every ping response of every computer on the subnet. They are extracted with an appended colon, which the cut removes. Then they are sorted numerically by IP address, uniq removes any duplicate responses from the multiple pings, and then the list of responding IP addresses is passed to arp, which gives back the MAC address and hostname of each responding IP address on your subnet. I've also executed this command on my work computer, whose subnet is 255.255.254.0 and almost every IPA is in use there. So I know it works. Just remember, if done from the keyboard, it's awk '{print $4}' and in the .bash_login, when you are defining the alias command, it's awk '{print \$4}'. Don't know why, just that if you don't it doesn't work. You could probably build your alias to cat the results to a file, or put it as a standalone script file launched by launchd or cron.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Ping from a list of hosts

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.