如何编写一个Linux bash脚本,告诉我我的局域网中哪些计算机是开启的?

33

我该如何编写一个Linux Bash脚本,告诉我哪些计算机在我的局域网中处于开启状态?

如果我能够输入一个IP地址范围作为脚本的输入参数将会很有帮助。


1
使用Nagios http://www.nagios.org/ 可能是一个不错的主意,而不是每次都运行脚本,但如果你很匆忙,它非常有用。 (+1) - Alex Bolotov
16个回答

77
我建议使用nmap的ping-scan标志,
$ nmap -sn 192.168.1.60-70

Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2009-04-09 20:13 BST
Host machine1.home (192.168.1.64) appears to be up.
Host machine2.home (192.168.1.65) appears to be up.
Nmap finished: 11 IP addresses (2 hosts up) scanned in 0.235 seconds

话虽如此,如果您想自己书写(这也很公正),以下是我会这样做:

for ip in 192.168.1.{1..10}; do ping -c 1 -t 1 $ip > /dev/null && echo "${ip} is up"; done

以下是上述命令的每个部分的说明:

生成IP地址列表

您可以使用{1..10}语法生成数字列表,例如..

$ echo {1..10}
1 2 3 4 5 6 7 8 9 10

它还可以用于像mkdir {dir1,dir2}/{sub1,sub2}这样的操作——这将创建包含sub1sub2dir1dir2目录。

因此,要生成IP列表,我们可以执行以下操作:

$ echo 192.168.1.{1..10}
192.168.1.1 192.168.1.2 [...] 192.168.1.10

循环

在bash中循环时,您可以使用for关键字:

$ for thingy in 1 2 3; do echo $thingy; done
1
2
3

ping命令

接下来是ping命令。由于不同的操作系统和版本会略有不同(我目前正在使用OS X),因此ping命令也会有所不同。

默认情况下(在OS X版本的ping上),它会一直ping直到被中断,这对于我们来说不起作用,因此ping -c 1只会尝试发送一个数据包,这应该足以确定计算机是否处于活动状态。

另一个问题是超时值,在这个ping版本中似乎为11秒。可以使用-t标志进行更改。一秒钟应该足以检查本地网络上的计算机是否正常工作。

因此,我们将使用以下ping命令:

$ ping -c 1 -t 1 192.168.1.1
PING 192.168.1.1 (192.168.1.1): 56 data bytes

--- 192.168.1.1 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss

检查ping结果

接下来,我们需要知道机器是否有响应。

我们可以使用&&操作符来运行一个命令,如果第一个命令成功,例如:

$ echo && echo "It works"

It works
$ nonexistantcommand && echo "This should not echo"
-bash: nonexistantcommand: command not found

很好,我们可以这样做:

ping -c 1 -t 1 192.168.1.1 && echo "192.168.1.1 已经上线!"

另一种方法是使用ping的退出码。如果ping命令执行成功,它将以退出码0(成功)退出;如果失败,则为非零代码。在bash中,您可以使用变量$?获取最后一个命令的退出码。

因此,要检查命令是否有效,我们需要执行以下操作:

ping -c 1 -t 1 192.168.1.1;
if [ $? -eq 0 ]; then
    echo "192.168.1.1 is up";
else 
    echo "ip is down";
fi

隐藏ping输出

最后一件事,我们不需要看到ping的输出,所以我们可以使用重定向符号>将标准输出重定向到/dev/null,例如:

$ ping -c 1 -t 1 192.168.1.1 > /dev/null && echo "IP is up"
IP is up

要重定向 stderr (丢弃 ping: sendto: Host is down 信息),您可以使用 2>,例如:

$ errorcausingcommand
-bash: errorcausingcommand: command not found
$ errorcausingcommand 2> /dev/null
$

脚本

因此,为了将所有内容组合起来..

for ip in 192.168.1.{1..10}; do  # for loop and the {} operator
    ping -c 1 -t 1 192.168.1.1 > /dev/null 2> /dev/null  # ping and discard output
    if [ $? -eq 0 ]; then  # check the exit code
        echo "${ip} is up" # display the output
        # you could send this to a log file by using the >>pinglog.txt redirect
    else
        echo "${ip} is down"
    fi
done

或者,使用 && 方法,在一行代码中实现:
for ip in 192.168.1.{1..10}; do ping -c 1 -t 1 $ip > /dev/null && echo "${ip} is up"; done

问题

速度很慢。每个ping命令大约需要1秒钟(因为我们将-t超时标志设置为1秒钟)。一次只能运行一个ping命令。显然的解决方法是使用线程,这样可以并发运行命令,但这已经超出了bash的使用范畴。

“Python线程 - 第一个示例”解释了如何使用Python线程模块编写多线程ping程序。尽管在那时,我再次建议使用nmap -sn


有没有办法在BASH中创建和启动线程? 这可能可以通过ruby/python脚本打开网络连接来完成,但是是否可以在BASH中实现创建多个线程的能力? - CodeJoust
你可以通过在命令后面加上 mycmd & 来在后台运行进程,但这并不是真正的线程,并且如果你需要多个命令的输出,这可能会变得非常复杂。使用Python/Ruby等语言会更简单。 - dbr
2
for ip in 192.168.1.{1..10}; do ping -c 1 -t 1 $ip > /dev/null && echo "${ip} is up" & done ; wait - Robᵩ
2
更新:nmap -sP现已弃用。nmap -sn是获得相同输出的当前选项。 - blue
为什么不将其并行化:for ip in 192.168.23.{1..254}; do (ping -c 1 -t 1 $ip > /dev/null && echo ">>> ${ip} is up"; ) & done - Ivan Borshchov
显示剩余2条评论

14

在现实世界中,你可以使用nmap来获取你想要的内容。

nmap -sn 10.1.1.1-255

这将会ping一下10.1.1.1到10.1.1.255范围内的所有地址,并告诉您哪些地址有响应。

当然,如果您确实想将此作为bash练习进行,您可以为每个地址运行ping并解析输出结果,但那是另外一个故事了。


11
假设我的网络地址是10.10.0.0/24,如果我对广播地址进行ping测试,例如:
ping -b 10.10.0.255

我将从该网络上未阻止其ICMP ping端口的所有计算机中获取答案。

64 bytes from 10.10.0.6: icmp_seq=1 ttl=64 time=0.000 ms
64 bytes from 10.10.0.12: icmp_seq=1 ttl=64 time=0.000 ms 
64 bytes from 10.10.0.71: icmp_seq=1 ttl=255 time=0.000 ms 

所以你只需要提取第4列,例如使用awk:

ping -b 10.10.0.255 | grep 'bytes from' | awk '{ print $4 }'

10.10.0.12:
10.10.0.6:
10.10.0.71:
10.10.0.95:

好的,你将会得到重复的内容,并且可能需要移除冒号。

来自评论的编辑: -c选项限制ping的次数 由于脚本将会结束,我们也可以限制唯一IP的数量

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

1
ping -b 10.10.0.255 | awk '{ print $4 }' | sort | uniq 将删除重复项 - Ben
@Ben:你应该在调用ping时添加-c选项,否则它不会终止。 - Jochen Walter
-b选项并非所有ping命令都支持。例如,在我的OpenSuse上是可以的,但在我的Mac上不行,您仍然可以尝试不使用此选项。 - chburd

8

还有一个fping工具:

fping -g 192.168.1.0/24

或者:

fping -g 192.168.1.0 192.168.1.255

或者只显示活着的主机:

fping -ag 192.168.1.0/24

它可以并行地对主机进行ping操作,因此扫描速度非常快。我不知道有哪个发行版在默认安装中包含fping,但在大多数发行版中,您可以通过软件包管理器获取它。


2
此外,使用chburd提出的“ping广播地址”的方法,这个管道应该能够为您解决问题:
ping -c 5 -b 10.11.255.255 | sed -n 's/.* \([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p' | sort | uniq

当然,您需要将广播地址更改为您的网络地址。

2

仅供娱乐,以下是另一种方式

     #!/bin/bash
     nmap -sP 192.168.1.0/24 > /dev/null 2>&1 && arp -an | grep -v 不完整的 | awk '{print$2}' | sed -e s,\(,, | sed -e s,\),,


你可以将 <<grep -v incomplete | awk '{print$2}'>> 合并为 <<awk '!/incomplete/{print$2}'>>。 - hlovdal

1

有些机器不会响应ping命令(例如防火墙)。

如果你只想访问本地网络,可以使用以下命令:

(for n in $(seq 1 254);do sudo arping -c1 10.0.0.$n &  done ; wait) | grep reply | grep --color -E '([0-9]+\.){3}[0-9]+'

解释部分!

  1. arping 是一个发送 ARP 请求的命令,在大多数 Linux 系统上都存在。

例如:

sudo arping -c1 10.0.0.14

如果您已经是root用户,则不需要使用sudo。

  • 10.0.0.14:您要测试的IP地址
  • -c1:仅发送一个请求。

  1. &:表示“我不想等待”的字符

这是一个非常有用的字符,它使您能够在子进程中启动命令而无需等待其完成(就像线程一样)。


  1. 这里的 for 循环用于对所有 255 个 IP 地址进行 arping。它使用 seq 命令列出所有数字。

  1. wait:在我们发出请求后,我们希望看到是否有一些回复。为此,我们只需在循环后加上waitwait类似于其他语言中的join()函数。

  1. ():括号用于将所有输出解释为文本,以便我们可以将其提供给grep


  1. grep:我们只想看到回复。第二个grep仅用于突出显示IP。

希望对您有所帮助

编辑20150417:Maxi更新!

我解决方案的不好之处在于它在最后打印所有结果。这是因为grep有足够大的缓冲区来放置一些行。 解决方案是向第一个grep添加--line-buffered。 像这样:

(for n in $(seq 1 254);do sudo arping -c1 10.0.0.$n &  done ; wait) | grep --line-buffered reply | grep --color -E '([0-9]+\.){3}[0-9]+'

我感谢提供arping作为替代方案,并且防火墙可能不会响应ping的参考。 - Tim Holt

1
#!/bin/bash
#Get the ip address for the range
ip=$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}' | cut -d"." -f1,2,3)

# ping test and list the hosts and echo the info

for range in $ip ; do  [ $? -eq 0 ] && ping -c 1 -w 1 $range > /dev/null 2> /dev/null && echo "Node $range is up" 
done

1

正如其他帖子所指出的那样,nmap 是正确的选择,但是这里介绍一下如何在 bash 中执行 ping 扫描的等效操作。我不会使用广播 ping,因为现在很多系统都配置为不响应广播 ICMP。

for i in $(seq 1 254); do
    host="192.168.100.$i"
    ping -c 1 -W 1 $host &> /dev/null
    echo -n "Host $host is "
    test $? -eq 0 && echo "up" || echo "down"
done

1

尽管这是一个老问题,它似乎仍然很重要(至少对我来说足够重要,需要处理这个问题)。我的脚本也依赖于nmap,所以除了可以定义要扫描的接口和自动生成IP范围(至少在某种程度上),没有什么特别之处。

这就是我想出来的。

#!/bin/bash
#Script for scanning the (local) network for other computers 

command -v nmap >/dev/null 2>&1 || { echo "I require nmap but it's not installed. Aborting." >&2; exit 1; }

if [ -n ""$@"" ];  then
    ip=$(/sbin/ifconfig $1 | grep 'inet '  | awk '{ print $2}' | cut -d"." -f1,2,3 )
    nmap -sP $ip.1-255
else
    echo -e "\nThis is a script for scanning the (local) network for other computers.\n"
    echo "Enter Interface as parameter like this:"
    echo -e "\t./scannetwork.sh $(ifconfig -lu | awk '{print $2}')\n"

    echo "Possible interfaces which are up are: "   
    for i in $(ifconfig -lu)
    do
        echo -e "\033[32m \t $i \033[39;49m"
    done

    echo "Interfaces which could be used but are down at the moment: "
    for i in $(ifconfig -ld)
    do
        echo -e "\033[31m \t $i \033[39;49m"
    done
    echo
fi

一个备注:这个脚本是在OSX上创建的,因此在Linux环境下可能会有一些变化。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接