如何制作 ping 脚本,并行 ping 一个列表

how to make ping script, ping a list in parallel

我试图通过 运行 并行使我的组 ping 脚本更快。 我需要添加什么才能在输入文件中将命令作为每行一个脚本执行?所有 运行 并行。

#!/bin/bash
# bash script to ping a list, and output it for ready for a CSV file.

Inputlist=./1A/list.txt

cat $Inputlist  | while read output

do
#Check if host is pingable
        ping -c 1 -w 1 "$output" > /dev/null
        if [ $? -eq 0 ]; then
#Output if remote host is pingable
        pingtime=`ping -c 4 "$output" | tail -1| awk -F '/' '{print }'`
        echo "`date '+%Y-%m-%d %H:%M:%S,%3N'`;$output;UP;$pingtime"
    else
#Output if remote host is not pingable
        echo "`date '+%Y-%m-%d %H:%M:%S,%3N'`;$output;Down;Uknown"
    fi
done

我找到了一个类似的 question 但我不明白必须进行哪些更改。

感谢@mark-setchell 我做了这个:

#!/bin/bash

#Ping function
function pingit () {

    target=

    # send stdout/stderr to /dev/null since all we need is the return code

    ping -c 1 -w 1 ${target} >/dev/null 2>&1 &&
    pingtime=`ping -c 1 "${target}" | tail -1| awk -F '/' '{print }'` &&
        echo "`date '+%Y-%m-%d %H:%M:%S,%3N'`;${target};UP;$pingtime" ||
        echo "`date '+%Y-%m-%d %H:%M:%S,%3N'`;${target};Down;Uknown"
}
#input file (<list)
data=$(<./1A/list.txt)

for line in ${data}
do
    # call our function and place the call in the background

    pingit ${line} &
done

# wait for all outstanding background jobs to complete before continuing

wait

# [optional] let operator know we're done.
#echo "Completed."

旧输出:

2018-01-13 15:14:53,136;192.168.2.1;UP;0.606
2018-01-13 15:14:56,208;192.168.2.2;UP;0.456
2018-01-13 15:14:59,231;192.168.2.3;UP;1.486
2018-01-13 15:15:00,244;192.168.2.4;Down;Uknown
2018-01-13 15:15:03,284;192.168.2.5;UP;0.359
2018-01-13 15:15:04,291;192.168.2.6;Down;Uknown
2018-01-13 15:15:05,298;192.168.2.7;Down;Uknown
2018-01-13 15:15:06,308;192.168.2.8;Down;Uknown
2018-01-13 15:15:07,316;192.168.2.9;Down;Uknown
2018-01-13 15:15:08,324;192.168.2.10;Down;Uknown
2018-01-13 15:15:09,330;192.168.2.11;Down;Uknown
2018-01-13 15:15:12,357;192.168.2.12;UP;0.709
2018-01-13 15:15:13,366;192.168.2.13;Down;Uknown
2018-01-13 15:15:14,371;192.168.2.14;Down;Uknown
2018-01-13 15:15:17,389;192.168.2.15;UP;1.107
2018-01-13 15:15:18,397;192.168.2.16;Down;Uknown
2018-01-13 15:15:19,410;192.168.2.17;Down;Uknown
2018-01-13 15:15:20,423;192.168.2.18;Down;Uknown
2018-01-13 15:15:21,431;192.168.2.19;Down;Uknown
2018-01-13 15:15:24,452;192.168.2.20;UP;0.260
2018-01-13 15:15:25,461;192.168.2.21;Down;Uknown
2018-01-13 15:15:26,469;192.168.2.22;Down;Uknown
2018-01-13 15:15:27,486;192.168.2.23;Down;Uknown
2018-01-13 15:15:28,497;192.168.2.24;Down;Uknown
2018-01-13 15:15:31,978;192.168.2.25;UP;119.883
2018-01-13 15:15:32,986;192.168.2.26;Down;Uknown
2018-01-13 15:15:34,017;192.168.2.27;Down;Uknown
2018-01-13 15:15:35,024;192.168.2.28;Down;Uknown
2018-01-13 15:15:36,032;192.168.2.29;Down;Uknown
2018-01-13 15:15:37,042;192.168.2.30;Down;Uknown
2018-01-13 15:15:38,050;192.168.2.31;Down;Uknown
2018-01-13 15:15:39,059;192.168.2.32;Down;Uknown
2018-01-13 15:15:40,068;192.168.2.33;Down;Uknown
2018-01-13 15:15:41,077;192.168.2.34;Down;Uknown
2018-01-13 15:15:42,087;192.168.2.35;Down;Uknown
2018-01-13 15:15:43,097;192.168.2.36;Down;Uknown

新输出(注意时间戳):

2018-01-13 15:27:43,517;192.168.2.1;UP;1.769
2018-01-13 15:27:43,520;192.168.2.2;UP;1.299
2018-01-13 15:27:43,521;192.168.2.12;UP;0.944
2018-01-13 15:27:43,521;192.168.2.20;UP;0.910
2018-01-13 15:27:43,522;192.168.2.15;UP;0.581
2018-01-13 15:27:43,522;192.168.2.5;UP;2.443
2018-01-13 15:27:43,532;192.168.2.3;UP;10.964
2018-01-13 15:27:43,774;192.168.2.25;UP;1.891
2018-01-13 15:27:44,511;192.168.2.18;Down;Uknown
2018-01-13 15:27:44,512;192.168.2.21;Down;Uknown
2018-01-13 15:27:44,513;192.168.2.16;Down;Uknown
2018-01-13 15:27:44,515;192.168.2.7;Down;Uknown
2018-01-13 15:27:44,509;192.168.2.4;Down;Uknown
2018-01-13 15:27:44,522;192.168.2.27;Down;Uknown
2018-01-13 15:27:44,523;192.168.2.30;Down;Uknown
2018-01-13 15:27:44,525;192.168.2.11;Down;Uknown
2018-01-13 15:27:44,524;192.168.2.22;Down;Uknown
2018-01-13 15:27:44,527;192.168.2.8;Down;Uknown
2018-01-13 15:27:44,519;192.168.2.19;Down;Uknown
2018-01-13 15:27:44,530;192.168.2.28;Down;Uknown
2018-01-13 15:27:44,530;192.168.2.10;Down;Uknown
2018-01-13 15:27:44,531;192.168.2.36;Down;Uknown
2018-01-13 15:27:44,527;192.168.2.14;Down;Uknown
2018-01-13 15:27:44,535;192.168.2.31;Down;Uknown
2018-01-13 15:27:44,527;192.168.2.33;Down;Uknown
2018-01-13 15:27:44,538;192.168.2.6;Down;Uknown
2018-01-13 15:27:44,539;192.168.2.26;Down;Uknown
2018-01-13 15:27:44,535;192.168.2.32;Down;Uknown
2018-01-13 15:27:44,538;192.168.2.29;Down;Uknown
2018-01-13 15:27:44,537;192.168.2.24;Down;Uknown
2018-01-13 15:27:44,538;192.168.2.13;Down;Uknown
2018-01-13 15:27:44,539;192.168.2.9;Down;Uknown
2018-01-13 15:27:44,535;192.168.2.35;Down;Uknown
2018-01-13 15:27:44,541;192.168.2.34;Down;Uknown
2018-01-13 15:27:44,542;192.168.2.17;Down;Uknown
2018-01-13 15:27:44,535;192.168.2.23;Down;Uknown

如果您没有安装 parallel 或想要获取更多信息而不仅仅是 ping 的退出状态

 $ cat ping.sh
 #!/bin/bash

 func_ping()
 {
    # ping once and send errors to bg and only grep time
    tt=$(ping -c 1 "" 2>/dev/null | grep -o -E 'time=[0-9]+.*')
    if [ $? -eq 0 ]; then # Check the exit code and print
      echo "$(date '+%Y-%m-%d %H:%M:%S,%3N') $url Up  ping $tt";
    else
       echo "$(date '+%Y-%m-%d %H:%M:%S,%3N') $url Down"
    fi
 }

 >ping.log # This make ping.log zero length, thus empty
 while read -r url
 do
    # Run on background by using '&' and save output to log for later viewing
    func_ping "$url" &>> ping.log &
 done <ping.txt

结果:

$ cat ping.log 
2018-01-13 08:19:52,529 www.timbukhh.com Down
2018-01-13 08:19:52,532 www.whosebug.com Up  ping time=33.1 ms
2018-01-13 08:19:52,538 www.timbukhh.com Down
2018-01-13 08:19:52,544 www.timbukhh.com Down
2018-01-13 08:19:52,595 www.timbukhh.com Down
2018-01-13 08:19:52,603 www.timbukhh.com Down
2018-01-13 08:19:52,602 www.whosebug.com Up  ping time=41.9 ms
2018-01-13 08:19:52,645 www.google.com Up  ping time=84.4 ms
2018-01-13 08:19:52,663 www.whosebug.com Up  ping time=34.3 ms
2018-01-13 08:19:52,682 www.whosebug.com Up  ping time=40.1 ms
2018-01-13 08:19:52,683 www.whosebug.com Up  ping time=41.4 ms
2018-01-13 08:19:52,690 www.whosebug.com Up  ping time=37.3 ms
2018-01-13 08:19:52,696 www.whosebug.com Up  ping time=39.8 ms