向子网上的每个 IP 发送 ping

发布于 2024-07-13 19:16:26 字数 147 浏览 17 评论 0原文

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

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

强制执行 arp 解析吗?

Is there a command line based way to send pings to each computer in a subnet?
Like

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

to enforce arp resolution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

迷乱花海 2024-07-20 19:16:26

并非所有机器都有可用的 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

Not all machines have nmap available, but it's a wonderful tool for any network discovery, and certainly better than iterating through independent ping commands.

$ 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
歌入人心 2024-07-20 19:16:26

我建议将 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

来自 man :

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

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

I would suggest the use of fping with the mask option, since you are not restricting yourself in ping.

fping -g 192.168.1.0/24

The response will be easy to parse in a script:

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
...

Note: Using the argument -a will restrict the output to reachable ip addresses, you may want to use it otherwise fping will also print unreachable addresses:

fping -a -g 192.168.1.0/24

From man:

fping differs from ping in that you can specify any number of targets
on the command line, or specify a file containing the lists of targets
to ping. Instead of sending to one target until it times out or
replies, fping will send out a ping packet and move on to the next
target in a round-robin fashion.

More info: http://fping.org/

野味少女 2024-07-20 19:16:26

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

Broadcast 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!)
...

(Add a -b option on Linux)

能怎样 2024-07-20 19:16:26

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

echo $(seq 254) | xargs -P255 -I% -d" " ping -W 1 -c 1 192.168.0.% | grep -E "[0-1].*?:"
  • 优点 1:您不需要安装任何额外的工具
  • 优点 2:速度快。 它并行执行所有操作,并为每个 1 秒 ping 超时(“-W 1”)。 所以1s就完成了:)
  • 优点3:输出是这样的
来自 192.168.0.16 的 64 字节:icmp_seq=1 ttl=64 time=0.019 ms 
  来自 192.168.0.12 的 64 字节:icmp_seq=1 ttl=64 时间=1.78 ms 
  来自 192.168.0.21 的 64 字节:icmp_seq=1 ttl=64 时间=2.43 ms 
  来自 192.168.0.1 的 64 字节:icmp_seq=1 ttl=64 时间=1.97 ms 
  来自 192.168.0.11 的 64 字节:icmp_seq=1 ttl=64 时间=619 ms 
  

编辑:
这里与脚本相同,当你的 xargs 没有 -P 标志时,就像 openwrt 中的情况一样(我刚刚发现)

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

I just came around this question, but the answers did not satisfy me.
So i rolled my own:

echo $(seq 254) | xargs -P255 -I% -d" " ping -W 1 -c 1 192.168.0.% | grep -E "[0-1].*?:"
  • Advantage 1: You don't need to install any additional tool
  • Advantage 2: It's fast. It does everything in Parallel with a timout for every ping of 1s ("-W 1"). So it will finish in 1s :)
  • Advantage 3: The output is like this
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

Edit:
And here is the same as script, for when your xargs do not have the -P flag, as is the case in openwrt (i just found out)

for i in $(seq 255);
do
 ping -W 1 -c 1 10.0.0.$i | grep 'from' &
done
请远离我 2024-07-20 19:16:26

在 Bash shell 中:

#!/bin/sh

COUNTER=1

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

In Bash shell:

#!/bin/sh

COUNTER=1

while [ $COUNTER -lt 254 ]
do
   ping 192.168.1.$COUNTER -c 1
   COUNTER=$(( $COUNTER + 1 ))
done
山人契 2024-07-20 19:16:26

命令行实用程序 nmap 也可以执行此操作:

nmap -sP 192.168.1.*

The command line utility nmap can do this too:

nmap -sP 192.168.1.*
装纯掩盖桑 2024-07-20 19:16:26
for i in $(seq 1 254); do ping -c1 -t 1 192.168.11.$i; done

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

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

Adding a -t 1 waits only one second before exiting. This improves the speed a lot if you just have a few devices connected to that subnet.

人生百味 2024-07-20 19:16:26
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
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
北风几吹夏 2024-07-20 19:16:26

这是对上面 @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

This is a modification of @david-rodríguez-dribeas answer above, which runs all the pings in parallel (much faster) and only shows the output for ip addresses which return the ping.

export COUNTER=1
while [ $COUNTER -lt 255 ]
do
    ping $1$COUNTER -c 1 -w 400 | grep -B 1 "Lost = 0" &
    COUNTER=$(( $COUNTER + 1 ))
done
酒几许 2024-07-20 19:16:26

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

Under linux, I think ping -b 192.168.1.255 will work (192.168.1.255 is the broadcast address for 192.168.1.*) however IIRC that doesn't work under windows.

毅然前行 2024-07-20 19:16:26

我来晚了,但这里有一个我为此目的制作的小脚本,我在 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

I came late but here is a little script I made for this purpose that I run in Windows PowerShell. You should be able to copy and paste it into the ISE. This will then run the arp command and save the results into a .txt file and open it in notepad.

# 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
剧终人散尽 2024-07-20 19:16:26
#!/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
#!/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
黑白记忆 2024-07-20 19:16:26

这个脚本在 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

然后查看所有连接的主机输出.txt 文件

This Script works well in OpenWRT

In a new file put this code ,

#!/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

Set execution permision and launch it

root@r1:/# nping 192.168.1

then view all host connected in out.txt file

-黛色若梦 2024-07-20 19:16:26

要 ping 子网中的所有 IP,请在终端中使用以下命令(预安装 nmap,如果需要,可使用brew install nmap):

sudo nmap -sn -PE 192.168.N.0/24

其中 192.168.N.0/24,
必须设置 N - 其中您将数字放在 0 'n 255 之间,
如果您的子网中有 IP,例如 192.168.110.20 -> 用这个:

sudo nmap -sn -PE 192.168.110.0/24

To ping all IPs in subnet, use this command in Terminal (pre-install nmap, if needed with brew install nmap):

sudo nmap -sn -PE 192.168.N.0/24

where 192.168.N.0/24,
is must set up of N - where u put ur number between 0 'n 255,
if you have IP in subnet like 192.168.110.20 -> use this:

sudo nmap -sn -PE 192.168.110.0/24
与之呼应 2024-07-20 19:16:26

在 macOS 上,我使用像这样的 bash 一行命令,速度非常快:

for i in $(seq 254); do { ping -W 100 -c 1 -n 192.168.0.$i | grep "bytes from" & }; done

因为 xargs 没有按预期工作(macOS xargs 不支持 -d)。 我还在 ping 中添加了 -n 以不查找 DNS 名称(节省时间)。 -W 100 对于让我的设备有足够的时间响应 ping 是必要的。

On macOS I use a bash one line command like this which is super fast:

for i in $(seq 254); do { ping -W 100 -c 1 -n 192.168.0.$i | grep "bytes from" & }; done

as xargs didn't work as expected (-d is not supported by macOS xargs). I also added -n to ping to not lookup the DNS name (saves time). The -W 100 was necessary to give my devices enough time to respond to the ping.

雪花飘飘的天空 2024-07-20 19:16:26
for i in $(seq 1 254); do ping -c1 192.168.11.$i; done
for i in $(seq 1 254); do ping -c1 192.168.11.$i; done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文