你家轩哥 2021-01-18 22:41 采纳率: 0%
浏览 17

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

#!/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!"

这个是快的那个

 

两个脚本速度差距特别大,求大佬解惑。。。拜托了 

 

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-07 18:26
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)