shell中使用curl获取http请求的状态码
#!/bin/bash checkPort() { #检测http端口是否正常 # 输入一个端口,返回以该端口请求url的http状态码。 # http_code会有3个状态:000=超时,200=正常,其他不正常 # 返回0=超时,1=正常,2=程序错误 url="http://127.0.0.1:"$1"/" logNotice "检测HTTP端口\t->\t$url" http_code=`curl -Is -m 10 -w %{http_code} -o /dev/null $url` if [ $http_code -eq 0 ];then # 0=超时 logErr "http_code\t->\t$http_code" logErr "检测结果\t->\t请求超时或端口未打开" return 0 elif [ $http_code -eq 200 ];then logSucess "http_code\t->\t$http_code" logSucess "检测结果\t->\t状态正常" # 1=200 ok return 1 else # 2=程序错误,状态码非200 logErr "http_code\t->\t$http_code" logErr "检测结果\t->\t应用程序错误" return 2 fi } checkPort 80 echo $?
其中logErr、logSucess、LogNotice是使用了SHELL输出带颜色文本里预先定义的方法,可以输出带颜色文本。
这篇文章还没有人留言,快来抢沙发吧。