2009-02-02 82 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