weixin_46223869
你家轩哥
2021-01-18 22:41

shell脚本ping整个网段,核心代码基本相同,但执行速度相差特别大有好心的大佬解惑吗?

  • ubuntu
  • linux
  • unix
  • centos
#!/bin/bash
clear
>ip-up.txt
>ip-down.txt

read -p "please input addr" ipc
for i in {1..254}
 do
  ping -c 2 -W 1 $ipc.$i &>/dev/null
  
  if [ $? -eq 0 ] ; then
     echo "$ipc.$i is up" &>/dev/null >>ip-up.txt
	else
	echo "$ipc.$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"

这个是慢的那个。。

#!/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条回答