Fastest way to ping a network range and return responsive hosts?

LinuxPerformanceBashNetworkingPing

Linux Problem Overview


Constraints:

  1. Speed matters.
  2. I am allowed to ping once.

I'm debating whether to use Python or shellscripting. Is there a method faster than bash?

Here is the current code,

for ip in $(seq int1 int2); do
    ping -c 1 xxx.xxx.xxx.$ip | grep "bytes from" &
done

Anything faster than this?

Linux Solutions


Solution 1 - Linux

You should use NMAP:

nmap -T5 -sP 192.168.0.0-255

Solution 2 - Linux

The following (evil) code runs more than TWICE as fast as the nmap method

for i in {1..254} ;do (ping 192.168.1.$i -c 1 -w 5  >/dev/null && echo "192.168.1.$i" &) ;done

takes around 10 seconds, where the standard nmap

nmap -sP 192.168.1.1-254

takes 25 seconds...

Solution 3 - Linux

Try this for a unique list.

ping -c 5 -b 10.10.0.255 | grep 'bytes from' | awk '{ print $4 }' | sort | uniq

another method (fetches live hosts):

fping -ag 192.168.1.0/24

Solution 4 - Linux

Try both of these commands and see for yourself why arp is faster:

PING:

for ip in $(seq 1 254); do ping -c 1 10.185.0.$ip > /dev/null; [ $? -eq 0 ] && echo "10.185.0.$ip UP" || : ; done

ARP:

for ip in $(seq 1 254); do arp -n 10.185.0.$ip | grep Address; [ $? -eq 0 ] && echo "10.185.0.$ip UP" || : ; done

Solution 5 - Linux

This is python code for the ping in range of the 192.168.0.0-192.168.0.100. You can change for loop as you comfort.

# -*- coding: utf-8 -*-
import socket
import os
import sys

up_ip =[] #list to store the ip-addresses of server online
for x in range(100):  #here range is 0-100. You can change the range according to your comfort

	server_ip = '192.168.0.'+ str(x)
	print "Trying ,server_ip,... \n"

	rep = os.system('ping -c 1 ' + server_ip)

	if rep == 0:
		up_ip.append(server_ip)
		print '******************* Server Is Up **************** \n'
	else:
		print 'server is down \n'

print up_ip

Solution 6 - Linux

This script runs on Git Bash (MINGW64) on Windows and return a messages depending of the ping result.

#!/bin/bash
#$1 should be something like "19.62.55"

if [ -z "$1" ]
  then
    echo "No identify of the network supplied, i.e. 19.62.55"
else
	ipAddress=$1
	
	for i in {1..256} ;do 
	(
		{
		ping -w 5 $ipAddress.$i ; 
		result=$(echo $?);
		} &> /dev/null
		

		if [ $result = 0 ]; then
			echo Successful Ping From : $ipAddress.$i
		else
			echo Failed Ping From : $ipAddress.$i
		fi &);
	done

fi

Solution 7 - Linux

BSD's

for i in $(seq 1 254); do (ping -c1 -W5 192.168.1.$i >/dev/null && echo "192.168.1.$i" &) ;done

Solution 8 - Linux

If time matters (and I know the feeling):

for i in {1..254}; do
echo -n -e "$i      \r" 	
timeout --preserve-status .2  ping -c1 -q 10.0.0.$i  &> /dev/null
[ $? -eq 0 ]&&echo 10.0.0.$i is happy to serve to his master!
done # as I am

The command:

timeout --preserve-status .2  ping -c1 -q 10.0.0.107 &> /dev/null  ;echo $? 

returns 143 with non-existing PC and 0 with existing PC and waits for the answer just and only for .2 second because TIME MATTERS! And therefore I have to look for my lost keys now.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1552586View Question on Stackoverflow
Solution 1 - LinuxRoman NewazaView Answer on Stackoverflow
Solution 2 - LinuxMike RedrobeView Answer on Stackoverflow
Solution 3 - LinuxSunny R GuptaView Answer on Stackoverflow
Solution 4 - LinuxbrendonmartinoView Answer on Stackoverflow
Solution 5 - LinuxVilas JoshiView Answer on Stackoverflow
Solution 6 - LinuxDiego4016View Answer on Stackoverflow
Solution 7 - LinuxrudyrockstarView Answer on Stackoverflow
Solution 8 - LinuxxerostomusView Answer on Stackoverflow