批量探測一個網段內主機線上狀態的指令碼(初)
Net8()
{
for ((j=$[$NetMaskS+1]; j<9;j++ ))do
IPMAXS=${IPMAXS}1
done
IPMAXS10=$((2#$IPMAXS))
echo "最小主機地址為:$Net1.0.0.1"
echo "最大主機地址位:$IPMAXS10.255.255.254"
}
Net16()
{
for ((j=$[$NetMaskS+1]; j<9;j++ ))do
IPMAXS=${IPMAXS}1
done
IPMAXS10=$((2#$IPMAXS))
echo "最小主機地址為:$Net1.$Net2.0.1"
echo "最大主機地址位:$Net1.$IPMAXS10.255.254"
}
Net24()
{
for ((j=$[$NetMaskS+1]; j<9;j++ ))do
IPMAXS=${IPMAXS}1
done
IPMAXS10=$((2#$IPMAXS))
echo "最小主機地址為:$Net1.$Net2.$Net3.1"
echo "最大主機地址位:$Net1.$Net2.$IPMAXS10.254"
echo "" > /app/ip.log
for (( i=$Net3; i<=$IPMAXS10; i++ ));do
for (( j=1; j<=254; j++ ));do
(
ping -w 2 -c 2 "${Net1}.${Net2}.${i}.$j" &> /dev/null
if [ $? -eq 0 ];then
echo " ${Net1}.${Net2}.${i}.${j}連通" | tee -a /app/ip.log
fi
)&
done
done
}
Net32()
{
for ((j=$[$NetMaskS+1]; j<9;j++ ))do
if [ $j -eq 8 ];then
IPMAXS=${IPMAXS}0
else
IPMAXS=${IPMAXS}1
fi
done
IPMAXS10=$((2#$IPMAXS))
echo "最小主機地址為:$Net1.$Net2.$Net3.$[$Net4+1]"
echo "最大主機地址位:$Net1.$Net2.$Net3.$IPMAXS10"
echo "" > /app/ip.log
for (( i=$[$Net4+1]; i<=$IPMAXS10; i++ ));do
(
ping -w 2 -c 2 "${Net1}.${Net2}.${Net3}.${i}" &> /dev/null
if [ $? -eq 0 ];then
echo "${Net1}.${Net2}.${Net3}.${i}連通" | tee -a /app/ip.log
fi
)&
done
}
read -p "請輸入網段:" Net
read -p "請輸入掩碼:" NetMask
Net1=$(echo "$Net" | cut -d. -f1)
Net2=$(echo "$Net" | cut -d. -f2)
Net3=$(echo "$Net" | cut -d. -f3)
Net4=$(echo "$Net" | cut -d. -f4)
Net12=$(echo "obase=2;$Net1"|bc)
Net22=$(echo "obase=2;$Net2"|bc)
Net32=$(echo "obase=2;$Net3"|bc)
Net42=$(echo "obase=2;$Net4"|bc)
if [ $NetMask -gt 0 -a $NetMask -lt 8 ];then
NetMaskS=$NetMask
IPMAXS=$(echo "$Net12" | cut -c 1-$NetMaskS)
Net8
fi
if [ $NetMask -ge 8 -a $NetMask -lt 16 ];then
if [ $NetMask -eq 8 ];then
IPMAXS=""
else
NetMaskS=$[$NetMask-8]
IPMAXS=$(echo "$Net22" | cut -c 1-$NetMaskS)
fi
Net16
fi
if [ $NetMask -ge 16 -a $NetMask -lt 24 ];then
if [ $NetMask -eq 16 ];then
IPMAXS=""
else
NetMaskS=$[$NetMask-16]
IPMAXS=$(echo "$Net32" | cut -c 1-$NetMaskS)
fi
Net24
fi
if [ $NetMask -ge 24 -a $NetMask -lt 32 ];then
if [ $NetMask -eq 24 ];then
IPMAXS=""
else
NetMaskS=$[$NetMask-24]
IPMAXS=$(echo "$Net42" | cut -c 1-$NetMaskS)
fi
Net32
fi