62

是否有基于命令行的方式向子网中的每台计算机发送 ping?像

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

强制执行arp解析?

4

14 回答 14

118

并非所有机器都nmap可用,但它是任何网络发现的绝佳工具,而且肯定比通过独立ping命令迭代更好。

$ nmap -n -sP 10.0.0.0/24

在 2009-02-02 07:41 CST 开始 Nmap 4.20 ( http://insecure.org )
主机 10.0.0.1 似乎已启动。
主机 10.0.0.10 似乎已启动。
主机 10.0.0.104 似乎已启动。
主机 10.0.0.124 似乎已启动。
主机 10.0.0.125 似乎已启动。
主机 10.0.0.129 似乎已启动。
Nmap 完成:在 2.365 秒内扫描了 256 个 IP 地址(最多 6 个主机)
于 2009-02-02T13:48:28.113 回答
68

我建议将fping与 mask 选项一起使用,因为您不会限制自己使用 ping。

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

来自男人:

fpingping的不同之处在于您可以在命令行上指定任意数量的目标,或者指定一个包含要 ping 的目标列表的文件。fping不会在超时或回复之前发送到一个目标,而是发送一个 ping 数据包并以循环方式移动到下一个目标。

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

于 2015-08-17T14:11:44.713 回答
47

广播 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!)
...

-b(在 Linux 上添加一个选项)

于 2009-02-02T13:20:23.303 回答
18

在 Bash 外壳中:

#!/bin/sh

COUNTER=1

while [ $COUNTER -lt 254 ]
do
   ping 192.168.1.$COUNTER -c 1
   COUNTER=$(( $COUNTER + 1 ))
done
于 2009-02-02T13:21:19.760 回答
15

我刚想到这个问题,但答案并不让我满意。所以我推出了自己的:

echo $(seq 254) | xargs -P255 -I% -d" " ping -W 1 -c 1 192.168.0.% | grep -E "[0-1].*?:"
  • 优势一:无需安装任何额外工具
  • 优势二:速度快。它并行执行所有操作,每次 ping 1s (" -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 中的情况一样(我刚刚发现)

for i in $(seq 255);
do
 ping -W 1 -c 1 10.0.0.$i | grep 'from' &
done
于 2016-02-23T14:11:50.137 回答
10

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

nmap -sP 192.168.1.*
于 2009-02-02T13:48:19.113 回答
7
for i in $(seq 1 254); do ping -c1 -t 1 192.168.11.$i; done

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

于 2016-11-27T16:09:51.397 回答
6
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
于 2014-01-23T21:47:48.260 回答
6

这是对上述@david-rodríguez-dribeas 答案的修改,它并行运行所有 ping(快得多),并且只显示返回 ping 的 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
于 2014-01-26T23:49:18.007 回答
3

在 linux 下,我认为 ping -b 192.168.1.255 可以工作(192.168.1.255 是 192.168.1.* 的广播地址)但是 IIRC 在 Windows 下不起作用。

于 2009-02-02T13:25:06.290 回答
1

我来晚了,但这是我为此目的编写的一个小脚本,我在 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
于 2018-01-21T02:58:53.340 回答
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
于 2013-04-07T01:10:59.693 回答
0

该脚本在OpenWRT中运行良好

在一个新文件中放入此代码,

#!/bin/sh
echo "Online IPs" > out.txt
COUNTER=1

while [ $COUNTER -lt 255 ]
do
 ping $1.$COUNTER -c 1 -w 400 | grep "time" >> out.txt &
 COUNTER=$(( $COUNTER + 1 ))
done
killall ping

设置执行权限并启动它

root@r1:/# nping 192.168.1

然后查看 out.txt 文件中连接的所有主机

于 2021-03-30T10:09:14.097 回答
-3
for i in $(seq 1 254); do ping -c1 192.168.11.$i; done
于 2015-10-21T12:53:41.040 回答