Linux ping 网络范围并返回响应主机的最快方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14038606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:07:23  来源:igfitidea点击:

Fastest way to ping a network range and return responsive hosts?

linuxperformancebashnetworkingping

提问by

Constraints:
1. Speed matters.
2. I am allowed to ping once.

限制条件:
1. 速度很重要。
2.我可以ping一次。

I'm debating whether to use Python or shellscripting. Is there a method faster than bash?

我正在争论是使用 Python 还是 shellscripting。有没有比 更快的方法bash

Here is the current code,

这是当前的代码,

for ip in $(seq int1 int2); do
    ping -c 1 xxx.xxx.xxx.$ip | grep "bytes from" &
done

Anything faster than this?

还有比这更快的吗?

采纳答案by Roman Newaza

You should use NMAP:

您应该使用 NMAP:

nmap -T5 -sP 192.168.0.0-255

回答by Sunny R Gupta

Try this for a unique list.

试试这个以获得一个独特的列表。

ping -c 5 -b 10.10.0.255 | grep 'bytes from' | awk '{ print $4 }' | sort | uniq

ping -c 5 -b 10.10.0.255 | grep 'bytes from' | awk '{ print $4 }' | sort | uniq

another method (fetches live hosts):

另一种方法(获取实时主机):

fping -ag 192.168.1.0/24

fping -ag 192.168.1.0/24

回答by Mike Redrobe

The following (evil) code runs more than TWICE as fast as the nmap method

以下(邪恶)代码的运行速度是 nmap 方法的两倍以上

for i in {1..254} ;do (ping 192.168.1.$i -c 1 -w 5  >/dev/null && echo "192.168.1.$i" &) ;done

takes around 10 seconds, where the standard nmap

大约需要 10 秒,标准的 nmap

nmap -sP 192.168.1.1-254

takes 25 seconds...

需要 25 秒...

回答by brendonmartino

Try both of these commands and see for yourself why arp is faster:

尝试这两个命令,看看为什么 arp 更快:

PING:

平:

for ip in $(seq 1 254); do ping -c 1 10.185.0.$ip > /dev/null; [ $? -eq 0 ] && echo "10.185.0.$ip UP" || : ; done

对于 $(seq 1 254) 中的 ip;做 ping -c 1 10.185.0.$ip > /dev/null; [$? -eq 0 ] && echo "10.185.0.$ip UP" || :; 完毕

ARP:

ARP:

for ip in $(seq 1 254); do arp -n 10.185.0.$ip | grep Address; [ $? -eq 0 ] && echo "10.185.0.$ip UP" || : ; done

对于 $(seq 1 254) 中的 ip;做 arp -n 10.185.0.$ip | grep地址;[$? -eq 0 ] && echo "10.185.0.$ip UP" || :; 完毕

回答by DiegoRG

This script runs on Git Bash (MINGW64) on Windows and return a messages depending of the ping result.

此脚本在 Windows 上的 Git Bash (MINGW64) 上运行,并根据 ping 结果返回消息。

#!/bin/bash
# should be something like "19.62.55"

if [ -z "" ]
  then
    echo "No identify of the network supplied, i.e. 19.62.55"
else
    ipAddress=

    for i in {1..256} ;do 
    (
        {
        ping -w 5 $ipAddress.$i ; 
        result=$(echo $?);
        } &> /dev/null


        if [ $result = 0 ]; then
            echo Successful Ping From : $ipAddress.$i
        else
            echo Failed Ping From : $ipAddress.$i
        fi &);
    done

fi

回答by Vilas Joshi

This is python code for the ping in range of the 192.168.0.0-192.168.0.100 . You can change for loop as you comfort.

这是 192.168.0.0-192.168.0.100 范围内 ping 的 python 代码。您可以根据需要更改 for 循环。

# -*- coding: utf-8 -*-
import socket
import os
import sys

up_ip =[] #list to store the ip-addresses of server online
for x in range(100):  #here range is 0-100. You can change the range according to your comfort
    server_ip = '192.168.0.'+ str(x)
    rep = os.system('ping -c 1 ' + server_ip)

    if rep == 0:
        upip.append(server_ip)
        print '*******************Server Is Up****************\n'
    else:
        print 'server is down \n'

print up_ip

回答by rudyrockstar

BSD's

BSD的

for i in $(seq 1 254); do (ping -c1 -W5 192.168.1.$i >/dev/null && echo "192.168.1.$i" &) ;done

对于我在 $(seq 1 254); do (ping -c1 -W5 192.168.1.$i >/dev/null && echo "192.168.1.$i" &) ;done