wget Command
[[Email protected] ~] # wget--spider-t 5-q-T 2 www.jd.com [root@oldboy ~] # echo $? #<== Use the return value to determine if the site is normal. 0
Curl Command
1. Use the Curl command return value to determine if the site is normal
[[Email protected] ~] # curl-s-o/dev/null www.xxxx.com [root@zabbix ~] # echo $? 0
2. Get the status code after command execution (200 indicates normal)
[[Email protected] ~] # Curl-i-M 5-s-W "%{http_code}\n"-o/dev/null www.baidu.com200
Method One: wget
#!/bin/shfunctionUsage () {#<== help functionecho $"usage:$0 URL"Exit1}functionCheck_url () {#<== detects URL functions. wget--spider-q-o/dev/null--tries=1-t 5 $ #the <== uses the Wget return value method, where $ $ is the function pass parameter. #curl-s-o/dev/null $ #<== using the Curl return value method is also possible. if[ $?-eq0] then echo"$ is yes."Exit0ElseEcho"is fail."Exit1fi}functionMain () {#<== the main function. if[ $#-ne 1] #<== If multiple arguments are passed in, the print Help function prompts the user. Then usage fi check_url $ #The <== receives the argument of the function, that is, the end of the $* is transmitted here. }main$*#<==, the $* here is
Test
[[Email protected] ~] # sh checkurl.sh www.jd.com www.jd.com is ok[root@oldboy ~] # sh checkurl.sh www.baidu.com www.baidu.com is ok[root@oldboy ~] # sh checkurl.sh 10.0.0.810.0.0.8 is fail
Method Two: Curl
[[Email protected] ~]#Cat checkurl1.sh#!/bin/bashusage () {echo"usage:$0 URL"Exit1}checkurl () {Local num= ' Curl-i-M 5-s-W"%{http_code}\n"-o/dev/null|grep 200|WC-l 'if[$num -eq1]#<== using the acquisition status code, and converted to a number of ways to judge, if 301 is considered correct can also be added egrep filter. Then Echo"$ is OK" ElseEcho"$ is fail"Fi}main () {if[ $#-ne 1]Then usage fi checkurl $}main$*
Test
[[Email protected] ~] # sh checkurl.sh www.jd.com www.jd.com is ok[root@oldboy ~] # sh checkurl.sh www.baidu.com www.baidu.com is ok[root@oldboy ~] # sh checkurl.sh 10.0.0.810.0.0.8 is fail
Article Source: https://mp.weixin.qq.com/s?__biz=MzAxOTE5NjQwOA==&mid=2650114763&idx=1&sn= 4628f1b5080ae882f5672d39b39518ec&chksm= 83cb9a5cb4bc134ab106b0d07a6cbb88ca2ae23d4c6e14325cf4564a0df66e6256dd96c0d557&mpshare=1&scene=23& Srcid=0702cr1hmz7wwthkwmtrhvpx#rd
Zabbix Monitor URL is normal