多线程ping脚本 [英] Multi-threaded ping script
本文介绍了多线程ping脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个
#! /bin/bash
cd ~
hostname=`hostname`
cat /opt/ip.txt | while read line;
do
# do something with $line here
RES=`ping -c 2 -q $line | grep "packet loss"`
echo "---" >> /opt/os-$hostname.txt
echo "---"
echo "$line $RES" >> /opt/os-$hostname.txt
echo "$line $RES"
done
如何使脚本多线程?我想提高性能.
How I can make the script multi-threaded? I would like to speed up the performance.
推荐答案
您可以使用<(...)
表示法启动子流程,然后将cat
所有输出一起使用:
You can use the <(...)
notation for starting a subprocess and then cat
all the outputs together:
myping() {
ping -c 2 -q "$1" | grep "packet loss"
}
cat <(myping hostname1) <(myping hostname2) ...
要为此使用循环,您将需要先构建命令:
To use a loop for this, you will need to build the command first:
cat /opt/ip.txt | {
command='cat'
while read line
do
command="$command "'<'"(myping $line)"
done
eval "$command"
}
如果您真的想要原始的定界---
,我建议在myping
中添加一个echo "---"
.
If you really want the delimiting ---
of your original, I propose to add an echo "---"
in the myping
.
如果还要将输出附加到文件,请使用tee
:
If you want to append the output to a file as well, use tee
:
eval "$command" | tee -a /opt/os-$hostname.txt
这篇关于多线程ping脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文