千家信息网

shell脚本实现快速ping网段内的IP地址

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,有时候想看看网段中有哪些IP被用了,可以用这个快速ping做一个初步的判断。可以自己指定测试IP网段,并将测试结果整理排序到文件。#!/bin/bash#V1.0 2019-09-10#Ping te
千家信息网最后更新 2025年12月02日shell脚本实现快速ping网段内的IP地址

有时候想看看网段中有哪些IP被用了,可以用这个快速ping做一个初步的判断。可以自己指定测试IP网段,并将测试结果整理排序到文件。


#!/bin/bash

#V1.0 2019-09-10

#Ping test shell script by tutor


clear

>ip-up.txt

>ip-down.txt


while true;

do

read -p "Please input the ip segment for ping test(like 192.168.100) : " segment



if [ -z $segment ] ; then


segment="192.168.100"


fi



echo -n "the ip segment is ${segment} , are you sure to continue [y/n] ? "


read action


if [ -z $action ] ; then

break

fi


if [ "$action" = "y" ]; then

break

fi

done


echo "ping test is ready to run! "


for i in {1..254}

do

{

ping -c2 -W1 ${segment}.${i} &>/dev/null

if [ $? -eq 0 ]; then

echo "${segment}.$i is up" &>/dev/null >> ip-up.txt

else

echo "${segment}.$i is down" &>/dev/null >> ip-down.txt

fi

}&

done


wait

sort -n -k 4 -t . ip-up.txt -o ip-up.txt

cat ip-up.txt

sort -n -k 4 -t . ip-down.txt -o ip-down.txt

cat ip-down.txt


echo "ping test are finished!"


0