如何将 xargs -P 20 与 curl 和主机列表一起使用
How to use xargs -P 20 with curl and list of hosts
再问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
下面的代码片段可以做到(请注意 response_code
与 200
的确切比较):
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 调用处理的不仅仅是 1 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
再问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
下面的代码片段可以做到(请注意 response_code
与 200
的确切比较):
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 调用处理的不仅仅是 1 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