【问题标题】:How to use xargs -P 20 with curl and list of hosts如何将 xargs -P 20 与 curl 和主机列表一起使用
【发布时间】:2020-01-19 23:53:44
【问题描述】:

还有一个 nube 的问题.. 我有一个脚本,它正在逐一检查列表中的主机以获取 http 响应。如何使用 xargs 或其他方法将其升级为 multitrhead?

#!/bin/bash
response="200"
cat list.txt | while read string
do
test=$(curl -I --path-as-is -s -k "http://"$string"/index.html" | head -n1)
if grep -q "response" <<< "$test"; then
echo $string " has response " $response
fi
done

【问题讨论】:

  • 如果您有最新版本的 curl (7.66+),您可以使用 --parallel and --parallel-max 命令行选项来执行多个并行请求。 (在我撰写此评论(2020 年 1 月)时,您可能没有足够新的版本,因为 stable Debian package 目前是 v7.64。但将来此功能将更广泛地使用。

标签: bash curl xargs


【解决方案1】:

下面的代码 sn-p 可以做到(注意 response_code200 的确切比较):

code=200
xargs -P20 -I@ sh -c "test \$(curl -I -s -w "%{response_code}" http://@/index.html -o/dev/null) -eq $code && echo @ has code $code" < list.txt

如果您更喜欢 grepping ,那么请执行以下操作:

code=200
xargs -P20 -I@ sh -c "curl -I -s http://@/index.html | head -1 | grep -qw $code && echo @ has code $code" < list.txt

然后是稍微优化的一个,允许同时指定并行性 (-P20),同时还允许每个 curl 调用处理多个 URL(-n4,更少分叉):

code=200
xargs -n4 -P20 -I@ curl -I -s -w "%{response_code}: @\n" http://@/index.html -o/dev/null | egrep "^$code:" < list.txt

【讨论】:

  • 即使 list.txt 中的第二个 ip 的代码为 200,它也会显示“sh: 1: test: not found”
  • 可能从 sh -c 更改为 bash -c 会有所帮助。
猜你喜欢
  • 2016-12-27
  • 2019-04-12
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
  • 2015-07-11
  • 2018-07-30
  • 1970-01-01
相关资源
最近更新 更多