Pinging numerous addresses at the same time and collecting results
Hi there,
I'm currently facing a problem that I can't really think of solving. The problem is as follows: I need to write a script that figures out what address the proxy server in that location has. The way I can find this out is by pinging a set number of IP-addresses. For example if 192.168.100.120 answers I know that the proxy address is 10.120.10.10, if 192.168.100.170 answers I know that the proxy address is 10.170.10.10. This would be ok if there only was two or three possible locations but my problem is that there is already about 30 locations which could rise into the 100's soon. I can ping them one after another but going through 100 pings makes it quite slow. What I'd want to happen is the following. - The script spawns/forks as many simultaneous pings as possible - If one of these pings get a 'hit' it writes it to result file - The script reads this value and pairs it with a proxy address and writes this to the setting for Firefox. The theory of this looks well cool and should work but as usual for me I have no real idea of how to make this into a working script. Does anyone have any bright ideas about how to implement this? Thanks in advance, Mike |
You could use nmap, which will ping ranges of IPs with the -sP option. For example:
Code:
nmap -sP 192.168.1-3.* You might want to add the -n option to prevent resolution to names so you always get IPs back, and filter the output with awk or something to get a simple list of IPs, i.e. Code:
nmap -n -sP 192.168.1-3.* |awk '/^Host/ { print $2 }' |
Thanks for your reply but it wouldn't work for this special scenario.
I need to use ping since we can only use what's being installed as standard with the distribution we are using. Besides, using Nmap kills the firewalls/proxies that we have (read: Bordermanager) so that's not an option. Once again, thanks for your suggestion, I'm really grateful for any ideas. // Mike |
The most ubiquitous tools are going to be a simple borne style shall and ping I guess. You'd have to watch out for different ping versions on Linux, the BSDs, other Unixes (things like return values have a nasty trick of changing subtly across the different *nixes...
Components of the script would be a function to turn an IP spec with ranges into a list of IPs, i.e. which takes a parameter like "192.168.1.1-3" and returns "192.168.1.1 192.168.1.2 192.168.1.3". Then you can iterate over the list and call another function which does the actual ping. ping itself would end up being called and the return value tested something like this: Code:
ping -W 3 -c 3 $IP > /dev/null Clearly if you do all the pings one after the other it's going to be very slow, so you'll want to use shell job control to achieve some degree of parallelism. Launching off groups of ping in the background and calling "wait" is the way to do this. To my mind easiest (if not optimal) way to do it would be split the list of IPs into groups of some number, launch that number of IP checks in parallel, wait for the result, and then do the next group. If you have multiple ping appending to some log file in parallel, you might have trouble with un-ordered writing to the log file... this is probably the hardest part to work around. I am not sure if the shell append re-direction operator >> will suffer from this problem or not - perhaps. If this is the case, you might create a directory for the results and touch a file for each positive result, deleting them once you've finished the check. |
How about this:
Code:
#!/bin/bash |
This looks awesome! I'll cut/paste and test straight away! Many thanks for taking the time to help me with this.
// Mike |
All times are GMT -5. The time now is 07:13 AM. |