[英]Linux script with curl to check webservice is up
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 块
curl -sL -w "%{http_code}\\n" "http://www.google.com/" -o /dev/null
-s
= Silent cURL's output -s
= Silent cURL 的输出-L
= Follow redirects -L
= 跟随重定向-w
= Custom output format -w
= 自定义输出格式-o
= Redirects the HTML output to /dev/null
-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
。
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 -f --fail HTTP 错误时静默失败(根本没有输出)
-s --silent Silent mode -s --silent 静默模式
-I --head Show document info only -I --head 仅显示文档信息
Note:笔记:
depending on needs you can also remove the "-I" because in some cases you need to do a GET and not a HEAD根据需要,您还可以删除“-I”,因为在某些情况下您需要执行 GET 而不是 HEAD
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
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这将通过 wget 2>&1
将标准错误通过管道检查标题到标准输出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我知道不卷曲,但仍然是一个解决方案
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}" "$1" -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 "$1 is DENIED with ${http}"
;;
[5]*)
exitStatus=5
echo "$1 is ERROR with ${http}"
;;
*)
exitStatus=6
echo "$1 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
Use this:用这个:
curl -o $CURL_OUTPUT -s -w %{http_code}\\n%{time_total}\\n $URL > $TMP_FILE 2>&1
cat $TMP_FILE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.