英文:
Run bash function with parallel
问题
我有一个Bash脚本来检查超过30000个站点。
我的函数是:
check_site() {
host=$1; ip=$2
resp=$(curl -i -m1 -H "Host: $host" http://$ip 2>&1)
echo "$resp" | grep -Eo "$ok" > /dev/null
if [ $? -ne 0 ]; then
echo -e "Command: curl -i -m1 -H \"Host: $host\" http://$ip 2>&1" >> "${outlog}"
echo -e "Block failed: $host:\n\"$resp\"\n\n" >> "${outlog}"
httpresponse=$(echo "$resp" | grep HTTP/1.1)
echo "$host $ip $httpresponse" >> "${faillog}"
fi
}
然后我调用这个函数:
...
check_site $host $ip
...
是否可能并行调用我的函数并传递参数$host
和$ip
?
英文:
I have a bash script to check more 30000 sites.
my function is:
check_site() {
host=$1; ip=$2
resp=$(curl -i -m1 -H "Host: $host" http://$ip 2>&1)
echo "$resp" | grep -Eo "$ok" > /dev/null
if [ $? -ne 0 ]; then
echo -e "Command: curl -i -m1 -H \"Host: $host\" http://$ip 2>&1" >> "${outlog}"
echo -e "Block failed: $host:\n\"$resp\"\n\n" >> "${outlog}"
httpresponse=$(echo "$resp" | grep HTTP/1.1)
echo "$host $ip $httpresponse" >> "${faillog}"
fi
}
and I call the function:
...
check_site $host $ip
...
Is it possible to use parallel call my function passing arguments $host
and $ip
?
答案1
得分: 2
我解决了将输入变量写入一个txt文件并使用gnu-parallel
的问题:
...
echo "$host $ip" >> "${tempfile}"
...
export -f check_site
export outlog="/tmp/out.log"
export faillog="/tmp/fail.log"
parallel --colsep ' ' -j 252 -a "${tempfile}" check_site
英文:
I solved writing input variables in a txt file and using gnu-parallel
:
...
echo "$host $ip" >> "${tempfile}"
...
export -f check_site
export outlog="/tmp/out.log"
export faillog="/tmp/fail.log"
parallel --colsep ' ' -j 252 -a "${tempfile}" check_site
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论