发送ping到子网上的每个IP

时间:2009-02-02 13:16:35

标签: networking shell ping arp

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

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

强制执行arp解决方案?

15 个答案:

答案 0 :(得分:100)

并非所有计算机都有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

答案 1 :(得分:41)

我建议在掩码选项中使用 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 的不同之处在于您可以指定任意数量的目标   在命令行上,或指定包含目标列表的文件   要ping。而不是发送到一个目标,直到它超时或   回复, fping 将发送一个ping数据包并继续下一个   以循环方式瞄准目标。

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

答案 2 :(得分:38)

广播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选项)

答案 3 :(得分:16)

在Bash shell中:

#!/bin/sh

COUNTER=1

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

答案 4 :(得分:10)

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

nmap -sP 192.168.1.*

答案 5 :(得分:8)

我刚刚解决了这个问题,但答案并不能让我满意。 所以我自己动手:

echo $(seq 254) | xargs -P255 -I% -d" " ping -W 1 -c 1 192.168.0.% | grep -E "[0-1].*?:"
  • 优势1:您无需安装任何其他工具
  • 优势2:速度很快。它为每个1s的ping(“-W 1”)执行并行操作。所以它将以1s完成:)
  • 优势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

答案 6 :(得分: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

答案 7 :(得分:5)

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

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

答案 8 :(得分:4)

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

答案 9 :(得分:2)

在linux下,我认为ping -b 192.168.1.255可以工作(192.168.1.255是192.168.1。*的广播地址)但是IIRC在windows下无效。

答案 10 :(得分:1)

检查this blog post是否符合您的要求。

答案 11 :(得分: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

答案 12 :(得分: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

答案 13 :(得分: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文件中查看所有连接的主机

答案 14 :(得分:-3)

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