feat: 扫描子网可达主机脚本

This commit is contained in:
LouisFonda 2024-07-15 14:45:23 +08:00
parent a4d375af41
commit 6a12113be3
Signed by: yigencong
GPG Key ID: 36FE627068AA2092

View File

@ -0,0 +1,25 @@
#!/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[@]}"