shell-scripts/scripts/network/check-subnet.sh

26 lines
553 B
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
#Program:
# 扫描子网可达的主机
#History:
# 2024/07/14 LouisFonda<yigencong@yahoo.com>
subnet="192.168.1"
reachable_hosts=()
# 循环遍历子网中的所有IP地址
for (( i=1; i<=255; i++ ))
do
ip="$subnet.$i"
# 发送单个ICMP回显请求等待1秒钟最多发送1次
ping -c 1 -W 1 "$ip" > /dev/null 2>&1
# 检查ping命令的退出状态
if [ $? -eq 0 ]; then
echo "$ip 可达"
reachable_hosts+=("$ip")
fi
done
echo "可达主机列表:"
echo "${reachable_hosts[@]}"