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?
This question is related to
linux
performance
bash
networking
ping
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
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
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...
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
BSD's
for i in $(seq 1 254); do (ping -c1 -W5 192.168.1.$i >/dev/null && echo "192.168.1.$i" &) ;done
Try both of these commands and see for yourself why arp is faster:
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
for ip in $(seq 1 254); do arp -n 10.185.0.$ip | grep Address; [ $? -eq 0 ] && echo "10.185.0.$ip UP" || : ; done
Source: Stackoverflow.com