{"id":206,"date":"2013-07-20T13:00:54","date_gmt":"2013-07-20T05:00:54","guid":{"rendered":"http:\/\/www.zyuns.com\/?p=206"},"modified":"2015-02-15T20:56:20","modified_gmt":"2015-02-15T12:56:20","slug":"linux-xia-li-yong-curl-jian-kong-wang-ye-shell-jiao-ben","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=206","title":{"rendered":"linux\u4e0b\u5229\u7528curl\u76d1\u63a7\u7f51\u9875shell\u811a\u672c"},"content":{"rendered":"<p>#!\/bin\/bash<\/p>\n<p>smail() {<br \/>\nmail -s &#8220;$1&#8243; gjw_apparitor@gmail.com &lt;&lt;EOF<br \/>\n$1<br \/>\n$2<br \/>\n====<br \/>\nreport time: `date +&#8221;%F %T&#8221;`<br \/>\ncurrent user: `whoami`<br \/>\nshell script: `echo $0`<br \/>\n====<br \/>\nEOF<br \/>\n}<\/p>\n<p>ssms() {<br \/>\n\/usr\/local\/feixin\/fetion &#8211;mobile=150000000 &#8211;pwd=******** &#8211;to=13810000000 &#8211;msg-gb=&#8221;fx $1&#8221;<br \/>\n}<\/p>\n<p>cd \/home\/maintain\/gaojianwei\/Script\/<br \/>\nFile=Monitor_IP.txt<br \/>\nsed -i \/.*\/d Curl_Out.txt<br \/>\nsed -i \/.*\/d Curl_Out_1.txt<\/p>\n<p>sed -e &#8216;\/^#\/d;\/^$\/d&#8217; ${File} | while read Ip Port URL<br \/>\ndo<br \/>\n\/usr\/bin\/curl &#8211;connect-timeout 8 &#8211;max-time 12 -o \/dev\/null -s -w %{time_total}:%{size_download}:%{http_code} http:\/\/${URL} -x ${Ip}:${Port} &gt;&gt; Curl_Out.txt<br \/>\necho &#8220;:${Ip}:${URL}&#8221; &gt;&gt; Curl_Out.txt<br \/>\ndone<\/p>\n<p>awk -F&#8221;:&#8221; &#8216;{if(($1*1000&lt;8000)&amp;&amp;($2&gt;0)&amp;&amp;($3==&#8221;200&#8243;||$3==&#8221;301&#8243;||$30==&#8221;302&#8243;||$3==&#8221;401&#8243;)) {} else {print $0 &gt;&gt; &#8220;Curl_Out_1.txt&#8221;}}&#8217; Curl_Out.txt<\/p>\n<p>if [ -s Curl_Out_1.txt ];then<br \/>\nWarning=&#8221;`awk &#8216;{printf(&#8220;%s#&#8221;,$0)}&#8217; Curl_Out_1.txt`&#8221;<br \/>\nssms ${Warning}<br \/>\nsmail CURL_Monitor ${Warning}<br \/>\nfi<\/p>\n<div><\/div>\n<div>\u5907\u6ce8\uff1a<\/div>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div id=\"blog_text\">\n<div>curl\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u4e0b\u7684http\u4e0b\u8f7d\u5de5\u5177\uff0c\u7c7b\u4f3cwget\u3002\u4e0ewget\u76f8\u4f3c\uff0c\u5b83\u4e5f\u53ef\u4ee5\u901a\u8fc7\u53d1\u9001\u6307\u5b9a\u7684http header\u5230\u670d\u52a1\u5668\u6765\u5224\u65ad\u670d\u52a1\u7684\u72b6\u6001\u3002<\/div>\n<div>\u8fd9\u91cc\u4ecb\u7ecd\u4e00\u4e2a\u4f7f\u7528curl\u76d1\u63a7\u9875\u9762\u53ef\u7528\u6027\u7684\u65b9\u6cd5\u3002<\/div>\n<div>\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\uff0c\u6765\u91c7\u96c6\u9875\u9762\u7684\u72b6\u6001\u7801\u3002<strong><span style=\"text-decoration: underline;\">\u5982\u679c\u8fd9\u6761\u547d\u4ee4\u8fd4\u56de\u7ed3\u679c\u4e3a200\uff0c\u8bf4\u660e\u670d\u52a1\u6b63\u5e38\u3002<\/span><\/strong>\u5982\u679c\u8fd4\u56de\u7684\u662f\u5176\u4ed6\u7684\u9875\u9762\uff0c\u8bf4\u660e\u5f02\u5e38\u3002<\/div>\n<div><strong>curl -o \/dev\/null -s -w %{http_code} http:\/\/zys.8800.org\/<\/strong><\/div>\n<div><strong>-o \u53c2\u6570\uff0c\u662f\u628a\u4e0b\u8f7d\u7684\u6240\u6709\u5185\u5bb9\u90fd\u91cd\u5b9a\u5411\u5230\/dev\/null\uff0c-s\u547d\u4ee4\uff0c\u662f\u5c4f\u853d\u4e86curl\u672c\u8eab\u7684\u8f93\u51fa\uff0c\u800c-w\u53c2\u6570\uff0c\u662f\u6839\u636e\u6211\u4eec\u81ea\u5df1\u7684\u9700\u8981\uff0c\u81ea\u5b9a\u4e49\u4e86curl\u7684\u8f93\u51fa\u683c\u5f0f\u3002<\/strong><\/div>\n<div>\u4f7f\u7528\u8fd9\u6761\u547d\u4ee4\uff0c\u518d\u914d\u5408\u90ae\u4ef6\u548c\u77ed\u4fe1\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u9875\u9762\u7684\u53ef\u7528\u6027\u76d1\u63a7\u3002\u5c06\u8fd9\u4e2a\u7a0b\u5e8f\u90e8\u7f72\u5728\u5168\u56fd\u5404\u5730\u7684\u673a\u5668\u4e0a\uff0c\u5c31\u53ef\u4ee5\u5bf9cdn\u7f51\u7edc\u8fdb\u884c\u53ef\u7528\u6027\u76d1\u63a7\u3002<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div id=\"app-share-content\">\n<p>curl\u53ea\u8fd4\u56de\u670d\u52a1\u5668\u54cd\u5e94\u72b6\u6001\uff0c\u4e0d\u8fd4\u56de\u5185\u5bb9\uff0c\u8fd4\u56de200\u662f\u6b63\u5e38\u7684\uff0c\u5176\u5b83\u7684\u4e0d\u6b63\u5e38\uff0c\u7b80\u5355\u7684\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<p>[coomix@localhost ~]$ echo `curl -o \/dev\/null -s -m 10 &#8211;connect-timeout 10 -w %{http_code} &#8220;<a rel=\"nofollow\">http:\/\/www.coomix.net\/index.jsp<\/a>&#8220;`<br \/>\n200<br \/>\n[coomix@localhost ~]$ echo `curl -o \/dev\/null -s -m 10 &#8211;connect-timeout 10 -w %{http_code} &#8220;<a rel=\"nofollow\">http:\/\/www.coomix.net\/index5.jsp<\/a>&#8220;`<br \/>\n404<\/p>\n<p>====================================================<\/p>\n<p>\u76d1\u63a7\u673a\u5668\u5217\u8868\u6587\u4ef6\uff1a<\/p>\n<p>server.list<\/p>\n<p>server1<\/p>\n<p>server2<\/p>\n<p>server3<\/p>\n<p>&nbsp;<\/p>\n<p>\u5efa\u7acb\u76d1\u63a7\u811a\u672c\uff1a webstatus.sh<\/p>\n<p>&nbsp;<\/p>\n<p>#!\/bin\/sh<br \/>\nmonitor_dir=\/home\/admin\/monitor\/<br \/>\nif [ ! -d $monitor_dir ]; then<br \/>\nmkdir $monitor_dir<br \/>\nfi<br \/>\ncd $monitor_dir<br \/>\nweb_stat_log=web.status<br \/>\nif [ ! -f $web_stat_log ]; then<br \/>\ntouch $web_stat_log<br \/>\nfi<br \/>\nserver_list_file=server.list<br \/>\nif [ ! -f $server_list_file ]; then<br \/>\necho &#8220;`date &#8216;+%Y-%m-%d %H:%M:%S&#8217;` ERROR:$server_list_file NOT exists!&#8221; &gt;&gt;$web_stat_log<br \/>\nexit 1<br \/>\nfi<br \/>\n#total=`wc -l $server_list_file|awk &#8216;{print $1}&#8217;`<br \/>\nfor website in `cat $server_list_file`<br \/>\ndo<br \/>\nurl=&#8221;http:\/\/$website\/app.htm&#8221;<br \/>\nserver_status_code=`curl -o \/dev\/null -s -m 10 &#8211;connect-timeout 10 -w %{http_code} &#8220;$url&#8221;`<br \/>\nif [ &#8220;$server_status_code&#8221; = &#8220;200&#8221; ]; then<br \/>\necho &#8220;`date &#8216;+%Y-%m-%d %H:%M:%S&#8217;` visit $website status code 200 OK&#8221; &gt;&gt;$web_stat_log<br \/>\nelse<br \/>\necho &#8220;`date &#8216;+%Y-%m-%d %H:%M:%S&#8217;` visit $website error!!! server can&#8217;t connect at 10s or stop response at 10 s, send alerm sms &#8230;&#8221; &gt;&gt;$web_stat_log<br \/>\necho &#8220;!app alarm @136xxxxxxxx\u00a0 server:$website can&#8217;t connect at 10s or stop response at 10s &#8230;&#8221; | nc smsserver port &amp;<br \/>\nfi<br \/>\ndone<br \/>\nexit 0<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e3b\u8981\u662f\u5229\u7528 curl -o \/dev\/null -s -m 10 &#8211;connect-timeout 10 -w %{http_code} &#8220;$url&#8221; \u8fd4\u56de\u72b6\u6001\u7801\u662f\u5426200,\u5982\u679c10s\u6ca1\u6709\u8fd4\u56de200\u72b6\u6001\u7801\uff0c\u5219\u53d1\u8b66\u62a5<\/p>\n<p>&nbsp;<\/p>\n<p>\u6700\u540e\u8ba9linux \u5b9a\u65f6\u6267\u884c\u811a\u672c\uff1a<\/p>\n<p>crontab -e<\/p>\n<p>*\/10 * * * * \/home\/admin\/app\/bin\/webstatus.sh<\/p>\n<p>\u8fd9\u6837\u6bcf\u969410\u5206\u949f\u5c31\u4f1a\u6267\u884c\u4e00\u6b21<\/p>\n<p>\u8fd9\u4e2a\u662f\u53e6\u5916\u4e00\u79cd\u811a\u672c\u5199\u6cd5\uff1a<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>while read URL<br \/>\ndo<br \/>\necho `date`<br \/>\nresult=`curl -o \/dev\/null -s -m 10 &#8211;connect-timeout 10 -w %{http_code}\u00a0 $URL`<br \/>\ntest=`echo $result`<br \/>\nif [[\u00a0 &#8220;$test&#8221; = &#8220;200&#8221;\u00a0 ]]<br \/>\nthen<br \/>\necho &#8220;$URL is ok&#8221;<br \/>\nelse<br \/>\necho &#8220;test err&#8221;<br \/>\n\/usr\/sbin\/sendmail -t &lt;&lt; EOF<br \/>\nFrom:SD-Detect<br \/>\nTo:13918888888@139.com,13800000000@139.com<br \/>\nSubject:Detected $URL<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n${URL} is err!!<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nEOF<br \/>\nfi<br \/>\ndone &lt; \/root\/jiankong\/httplist.txt<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>#!\/bin\/bash smail() { mail -s &#8220;$1&#8243; gjw_appa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-206","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=206"}],"version-history":[{"count":1,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/206\/revisions"}],"predecessor-version":[{"id":474,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/206\/revisions\/474"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}