带有 curl 检查 webservice 的 Linux 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12747929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux script with curl to check webservice is up
提问by Ahmet Karakaya
I have a webservice provided at http://localhost/test/testweb
我有一个提供的网络服务 http://localhost/test/testweb
I want to write a script to check if webservice is up with curl
我想编写一个脚本来检查 web 服务是否已启动 curl
If there a curl parameter given, returns 200 OK
ok true false so that I can use it is if-else block in linux script
如果给出了 curl 参数,则返回200 OK
ok true false 以便我可以使用它是 linux 脚本中的 if-else 块
采纳答案by Burhan Khalid
curl -sL -w "%{http_code}\n" "http://www.google.com/" -o /dev/null
-s
= Silent cURL's output-L
= Follow redirects-w
= Custom output format-o
= Redirects the HTML output to/dev/null
-s
= Silent cURL 的输出-L
= 按照重定向-w
= 自定义输出格式-o
= 将 HTML 输出重定向到/dev/null
Example:
例子:
[~]$ curl -sL -w "%{http_code}\n" "http://www.google.com/" -o /dev/null
200
I would probably remove the \\n
if I were to capture the output.
\\n
如果我要捕获输出,我可能会删除它。
回答by Thomas Nordquist
That will check the headers via wget 2>&1
pipes the stderr to stdout
grep
filters
-O /dev/null
just throws the content of the page
这将通过 wget2>&1
管道检查标题,stderr 到 stdout
grep
过滤器
-O /dev/null
只会抛出页面的内容
if [ "\`wget http://example.org/ -O /dev/null -S --quiet 2>&1 | grep '200 OK'\`" != "" ];
then
echo Hello;
fi;
I know not curl, but still a solution
我知道不卷曲,但仍然是一个解决方案
回答by Michael Cole
I needed a better answer to this, so I wrote the script below.
我需要一个更好的答案,所以我写了下面的脚本。
The fakePhrase is used to detect ISP "Search Assist" adware HTTP resposnes.
fakePhrase 用于检测 ISP “Search Assist”广告软件 HTTP resposnes。
#!/bin/bash
fakePhrase="verizon"
siteList=(
'http://google.com'
'https://google.com'
'http://wikipedia.org'
'https://wikipedia.org'
'http://cantgettherefromhere'
'http://searchassist.verizon.com'
)
exitStatus=0
function isUp {
http=`curl -sL -w "%{http_code}" "" -o temp_isUp`
fakeResponse=`cat temp_isUp | grep $fakePhrase`
if [ -n "$fakeResponse" ]; then
http=$fakePhrase
fi
case $http in
[2]*)
;;
[3]*)
echo 'Redirect'
;;
[4]*)
exitStatus=4
echo " is DENIED with ${http}"
;;
[5]*)
exitStatus=5
echo " is ERROR with ${http}"
;;
*)
exitStatus=6
echo " is NO RESPONSE with ${http}"
;;
esac
}
for var in "${siteList[@]}"
do
isUp $var
done
if [ "$exitStatus" -eq "0" ]; then
echo 'All up'
fi
rm temp_isUp
exit $exitStatus
回答by sudhir kumar mishra
Use this:
用这个:
curl -o $CURL_OUTPUT -s -w %{http_code}\n%{time_total}\n $URL > $TMP_FILE 2>&1
cat $TMP_FILE
回答by Mohamed EL HABIB
Same as @burhan-khalid, but added --connect-timeout 3
and --max-time 5
.
与@burhan-khalid 相同,但添加了--connect-timeout 3
和--max-time 5
。
test_command='curl -sL \
-w "%{http_code}\n" \
"http://www.google.com:8080/" \
-o /dev/null \
--connect-timeout 3 \
--max-time 5'
if [ $(test_command) == "200" ] ;
then
echo "OK" ;
else
echo "KO" ;
fi
回答by Zibri
I use:
我用:
curl -f -s -I "http://example.com" &>/dev/null && echo OK || echo FAIL
-f --fail Fail silently (no output at all) on HTTP errors
-s --silent Silent mode
-I --head Show document info only
-f --fail HTTP 错误时静默失败(根本没有输出)
-s --silent 静默模式
-I --head 仅显示文档信息