2009-02-02 50 views
37

是否有一种基于命令行的方式将ping发送到子网中的每台计算机? Like发送ping到子网上的每个IP地址

for(int i = 1; i < 254; i++) 
    ping(192.168.1.i); 

要强制执行arp分辨率吗?

+1

`for i in $(seq 1 254);做ping -c1 -t 1 192.168.11。$ i;完成“ - 它的本地,没有第三方工具。 – YumYumYum 2017-08-17 13:26:37

回答

26

我会建议使用使用mask选项fping,因为你没有限制自己在平。

fping -g 192.168.1.0/24 

的反应将很容易在脚本解析:

192.168.1.1 is alive 
192.168.1.2 is alive 
192.168.1.3 is alive 
192.168.1.5 is alive 
... 
192.168.1.4 is unreachable 
192.168.1.6 is unreachable 
192.168.1.7 is unreachable 
... 

注意:使用该参数-a将限制输出可达的IP地址,你可能想使用它,否则fping也将打印可达地址:

fping -a -g 192.168.1.0/24 

从人:

在fping不同于您可以指定任意数量的命令行上的目标 ,或指定包含目标 名单ping一个文件。取代发送到一个目标,直到超时或 回复,fping将发送一个ping数据包,并以循环方式移动到下一个 目标。

更多信息:http://fping.org/

37

ping广播:

$ ping 192.168.1.255 
PING 192.168.1.255 (192.168.1.255): 56 data bytes 
64 bytes from 192.168.1.154: icmp_seq=0 ttl=64 time=0.104 ms 
64 bytes from 192.168.1.51: icmp_seq=0 ttl=64 time=2.058 ms (DUP!) 
64 bytes from 192.168.1.151: icmp_seq=0 ttl=64 time=2.135 ms (DUP!) 
... 

(添加Linux上的-b选项)

+1

注意:您可能需要在其中添加“-b”,具体取决于版本/平台 – 2009-02-02 13:21:33

+2

此外,并非所有操作系统都会响应广播ping(默认情况下)。 – 2009-02-02 13:22:12

+2

在IPv6中使用“ff02 :: 1”。 – Keltia 2009-02-02 13:34:35

14

在Bash shell中:

#!/bin/sh 

COUNTER=1 

while [ $COUNTER -lt 254 ] 
do 
    ping 192.168.1.$COUNTER -c 1 
    COUNTER=$(($COUNTER + 1)) 
done 
+2

您可能想要在那里添加一个“-c 1”选项到ping命令...... – 2009-02-02 13:26:47

2

在Linux下,我认为平-b 192.168.1.255将工作(192.168.1.255是192.168.1。*的广播地址),但是在Windows下不起作用的IIRC。

7

命令行实用程序的nmap也可以这样做:

nmap -sP 192.168.1.* 
96

并非所有机器都有nmap可用,但它是任何网络发现一个奇妙的工具,当然不是通过独立ping命令迭代更好。

 
$ nmap -n -sP 10.0.0.0/24 

Starting Nmap 4.20 (http://insecure.org) at 2009-02-02 07:41 CST 
Host 10.0.0.1 appears to be up. 
Host 10.0.0.10 appears to be up. 
Host 10.0.0.104 appears to be up. 
Host 10.0.0.124 appears to be up. 
Host 10.0.0.125 appears to be up. 
Host 10.0.0.129 appears to be up. 
Nmap finished: 256 IP addresses (6 hosts up) scanned in 2.365 seconds 
+0

最好的答案是日期,因为它是第一个与现实不兼容的现实,即不是所有的子网都是相同的大小,并且使用/ 24符号可以概括为任何大小的子网。 – 2009-02-02 14:03:39

0
#!/bin/sh 

COUNTER=$1 

while [ $COUNTER -lt 254 ] 
do 
echo $COUNTER 
ping -c 1 192.168.1.$COUNTER | grep 'ms' 
COUNTER=$(($COUNTER + 1)) 
done 

#specify start number like this: ./ping.sh 1 
#then run another few instances to cover more ground 
#aka one at 1, another at 100, another at 200 
#this just finds addresses quicker. will only print ttl info when an address resolves 
3
FOR /L %i in (1,1,254) DO PING 192.168.1.%i -n 1 -w 100 | for /f "tokens=3 delims=: " %j in ('find /i "TTL="') do echo %j>>IPsOnline.txt 
6

这是的变形@大卫-罗德里格斯dribeas以上回答,它运行在所有并联的坪(更快),并且仅示出了用于返回平的IP地址的输出。

export COUNTER=1 
while [ $COUNTER -lt 255 ] 
do 
    ping $1$COUNTER -c 1 -w 400 | grep -B 1 "Lost = 0" & 
    COUNTER=$(($COUNTER + 1)) 
done 
-2
for i in $(seq 1 254); do ping -c1 192.168.11.$i; done 
5

我只是来解决这个问题,但答案没有满足我。 所以我推出我自己的:

echo $(seq 254) | xargs -P255 -I% -d" " ping -W 1 -c 1 192.168.0.% | grep -E "[0-1].*?:" 
  • 优势一:你不需要安装任何额外的工具
  • 优势二:它速度快。它以1s的每次ping命令执行并行操作(“-W 1”)。因此,将完成在1秒:)
  • 优势3:输出是这样
64 bytes from 192.168.0.16: icmp_seq=1 ttl=64 time=0.019 ms 
64 bytes from 192.168.0.12: icmp_seq=1 ttl=64 time=1.78 ms 
64 bytes from 192.168.0.21: icmp_seq=1 ttl=64 time=2.43 ms 
64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=1.97 ms 
64 bytes from 192.168.0.11: icmp_seq=1 ttl=64 time=619 ms 

编辑: 这里是一样的剧本,当你的xargs的没有-P标志,如在OpenWrt的情况下(I刚刚发现)

for i in $(seq 255); 
do 
ping -W 1 -c 1 10.0.0.$i | grep 'from' & 
done 
1

for i in $(seq 1 254); do ping -c1 -t 1 192.168.11.$i; done

添加-t 1退出前仅等待一秒钟。如果您只有少数设备连接到该子网,这会提高速度。

0

我来晚了,但这是我为此目的而创建的一个小脚本,我在Windows PowerShell中运行。您应该可以将其复制并粘贴到ISE中。这将运行arp命令并将结果保存到.txt文件中并在记事本中打开它。

# Declare Variables 
$MyIpAddress 
$MyIpAddressLast 

# Declare Variable And Get User Inputs 
$IpFirstThree=Read-Host 'What is the first three octects of you IP addresses please include the last period?' 
$IpStart=Read-Host 'Which IP Address do you want to start with? Include NO periods.' 
$IpEnd=Read-Host 'Which IP Address do you want to end with? Include NO periods.' 
$SaveMyFilePath=Read-Host 'Enter the file path and name you want for the text file results.' 
$PingTries=Read-Host 'Enter the number of times you want to try pinging each address.' 

#Run from start ip and ping 
#Run the arp -a and output the results to a text file 
#Then launch notepad and open the results file 
Foreach($MyIpAddressLast in $IpStart..$IpEnd) 
{$MyIpAddress=$IpFirstThree+$MyIpAddressLast 
    Test-Connection -computername $MyIpAddress -Count $PingTries} 
arp -a | Out-File $SaveMyFilePath 
notepad.exe $SaveMyFilePath