使用ping
- 优点
- 原生,不用安装软件
- 缺点
- 速度慢
下面的脚本列出 192.168.1.0/24的所有主机,大概需要255秒
#!/bin/bashfunction handler () {echo "will exit"exit 0}trap 'handler' SIGINTfor ip in 192.168.1.{1..255}doping -W 1 -c 1 $ip &> /dev/nullif [ $? -eq 0 ]; thenecho $ip is aliveelseecho $ip is deadfidone
使用fping
- 优点
- 速度快
- 缺点
- 需要安装fping
# 安装fpingbrew install fping # macyum install fping # centosapt install fping # debian
我用的fping 是MacOS X, fping的版本是4.2
用fping去执行,同样256个主机,只需要5-6s
fping -g 192.168.1.0/24 -r 1 -a -s
