我该如何编写一个Linux Bash脚本,告诉我哪些计算机在我的局域网中处于开启状态?
如果我能够输入一个IP地址范围作为脚本的输入参数将会很有帮助。
$ 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
以下是上述命令的每个部分的说明:
您可以使用{1..10}
语法生成数字列表,例如..
$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
它还可以用于像mkdir {dir1,dir2}/{sub1,sub2}
这样的操作——这将创建包含sub1
和sub2
的dir1
和dir2
目录。
因此,要生成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命令。由于不同的操作系统和版本会略有不同(我目前正在使用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
接下来,我们需要知道机器是否有响应。
我们可以使用&&
操作符来运行一个命令,如果第一个命令成功,例如:
$ 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的输出,所以我们可以使用重定向符号>
将标准输出重定向到/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
。
mycmd &
来在后台运行进程,但这并不是真正的线程,并且如果你需要多个命令的输出,这可能会变得非常复杂。使用Python/Ruby等语言会更简单。 - dbrfor ip in 192.168.1.{1..10}; do ping -c 1 -t 1 $ip > /dev/null && echo "${ip} is up" & done ; wait
- Robᵩnmap -sP
现已弃用。nmap -sn
是获得相同输出的当前选项。 - bluefor ip in 192.168.23.{1..254}; do (ping -c 1 -t 1 $ip > /dev/null && echo ">>> ${ip} is up"; ) & done
。 - Ivan Borshchov在现实世界中,你可以使用nmap来获取你想要的内容。
nmap -sn 10.1.1.1-255
这将会ping一下10.1.1.1到10.1.1.255范围内的所有地址,并告诉您哪些地址有响应。
当然,如果您确实想将此作为bash练习进行,您可以为每个地址运行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
还有一个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
,但在大多数发行版中,您可以通过软件包管理器获取它。
ping -c 5 -b 10.11.255.255 | sed -n 's/.* \([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p' | sort | uniq
仅供娱乐,以下是另一种方式
#!/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,\),,
有些机器不会响应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]+'
arping
是一个发送 ARP 请求的命令,在大多数 Linux 系统上都存在。例如:
sudo arping -c1 10.0.0.14
如果您已经是root用户,则不需要使用sudo。
10.0.0.14
:您要测试的IP地址-c1
:仅发送一个请求。&
:表示“我不想等待”的字符这是一个非常有用的字符,它使您能够在子进程中启动命令而无需等待其完成(就像线程一样)。
for
循环用于对所有 255 个 IP 地址进行 arping。它使用 seq
命令列出所有数字。wait
:在我们发出请求后,我们希望看到是否有一些回复。为此,我们只需在循环后加上wait
。
wait
类似于其他语言中的join()
函数。()
:括号用于将所有输出解释为文本,以便我们可以将其提供给grep
grep
:我们只想看到回复。第二个grep仅用于突出显示IP。希望对您有所帮助
我解决方案的不好之处在于它在最后打印所有结果。这是因为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]+'
#!/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
正如其他帖子所指出的那样,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
尽管这是一个老问题,它似乎仍然很重要(至少对我来说足够重要,需要处理这个问题)。我的脚本也依赖于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