几年前有人问,我刚好因为论文写了一段script , 但最终论文不是这题目
所以就分享出来给有需要的人用,自己也可以改 , 我是在 FreeBSD 12.0 - 14.1 都没问题,
在其它 unix or unix-like 应该也可以,授权采用 CC BY-SA
#!/usr/local/bin/bash
chec[Bk_host() {
ip=$1
result=`/sbin/ping -W 1 -c 1 $ip | grep 'bytes from '`
ping_status=$?
nc_result=`nc -z -w 1 $ip 80 > /dev/null 2>&1 ; echo $?`
if [ $ping_status -gt 0 ] && [ "$nc_result" == "0" ]; then
echo -e "`date +'%Y/%m/%d %H:%M:%S'` - host $ip \033[0;33mPing No echo , Service Active\033[0m"
elif [ $ping_status -gt 0 ]; then
echo -e "`date +'%Y/%m/%d %H:%M:%S'` - host $ip is \033[0;31mdown\033[0m"
else
if [ "$nc_result" != "0" ]; then
port_result="Service Inactive"
else
port_result="Service Active"
fi
echo -e "`date +'%Y/%m/%d %H:%M:%S'` - host $ip is \033[0;32mok\033[0m -`echo $result | cut -d ':' -f 2` $port_resul
t"
fi
}
for ip in $(cat /root/ips-home); do
check_host $ip &
done
wait
/root/ips-home
192.168.1.1
192.168.1.2
192.168.1.197
192.168.1.198
192.168.1.245
2024/09/16 22:09:05 - host 192.168.1.245 is ok - icmp_seq=0 ttl=64 time=0.098 ms Service Inactive
2024/09/16 22:09:05 - host 192.168.1.197 is ok - icmp_seq=0 ttl=64 time=0.244 ms Service Active
2024/09/16 22:09:05 - host 192.168.1.198 is ok - icmp_seq=0 ttl=64 time=0.234 ms Service Active
2024/09/16 22:09:07 - host 192.168.1.2 Ping No echo , Service Active
2024/09/16 22:09:08 - host 192.168.1.1 is down
需要的人可以按照自已的环境自已再去修改