运行并行的 bash 函数。

huangapple go评论54阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月19日 22:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定