26 lines
553 B
Bash
26 lines
553 B
Bash
#!/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[@]}"
|