linux-ping网络范围并返回响应主机的最快方法?
限制条件:
1.速度很重要。
2.我被允许ping一次。
我正在辩论是使用Python还是shellscripting。 有没有比bash更快的方法?
这是当前代码,
for ip in $(seq int1 int2); do
ping -c 1 xxx.xxx.xxx.$ip | grep "bytes from" &
done
有什么比这更快的吗?
6个解决方案
75 votes
您应该使用NMAP:
nmap -T5 -sP 192.168.0.0-255
Roman Newaza answered 2020-06-30T01:10:43Z
33 votes
以下(无效)代码的运行速度比nmap方法快两倍以上
for i in {1..254} ;do (ping 192.168.1.$i -c 1 -w 5 >/dev/null && echo "192.168.1.$i" &) ;done
大约需要10秒,其中标准的nmap
nmap -sP 192.168.1.1-254
需要25秒...
Mike Redrobe answered 2020-06-30T01:11:12Z
12 votes
尝试此操作以获得唯一列表。
fping -ag 192.168.1.0/24
另一种方法(获取活动主机):
fping -ag 192.168.1.0/24
Sunny R Gupta answered 2020-06-30T01:11:36Z
3 votes
尝试以下两个命令,并亲自了解为什么arp更快:
ping:
对于$(seq 1254)中的ip; 执行ping -c 1 10.185.0。$ ip> / dev / null; [$? -eq 0] &&回显“ 10.185.0。$ ip UP” || :; 做完了
ARP:
对于$(seq 1254)中的ip; 做arp -n 10.185.0。$ ip | grep地址; [$? -eq 0] &&回显“ 10.185.0。$ ip UP” || :; 做完了
brendonmartino answered 2020-06-30T01:12:13Z
1 votes
这是ping范围192.168.0.0-192.168.0.100的python代码。 您可以在舒适时更改循环。
# -*- 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)
rep = os.system('ping -c 1 ' + server_ip)
if rep == 0:
upip.append(server_ip)
print '*******************Server Is Up****************\n'
else:
print 'server is down \n'
print up_ip
Vilas Joshi answered 2020-06-30T01:12:33Z
0 votes
该脚本在Windows的Git Bash(MINGW64)上运行,并根据ping结果返回一条消息。
#!/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
DiegoRG answered 2020-06-30T01:12:53Z
本文探讨了在限定条件下(如仅能Ping一次且需快速完成)进行网络扫描的最佳实践。比较了使用NMAP、Shell脚本及Python等不同方法的速度与效率,推荐使用NMAP进行快速网络探测。

767

被折叠的 条评论
为什么被折叠?



