首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

Linux下令之curl - 强大的网络传输工具

2013-02-24 
Linux命令之curl - 强大的网络传输工具html xmlnshttp://www.w3.org/1999/xhtmlheadmeta http-equ

Linux命令之curl - 强大的网络传输工具
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<meta name="title" content="欢迎您 - 上海腾一" />
<meta name="keyword" content="上海腾一,融合通信,电子商务,语音通信,数据通信,基站工程外包托管,物联网,网站建设,电子商务解决方案,移动互联网,短信,彩信,呼叫中心,多方会议,PBX,IVR,电子商务策划方案,设备代维,网络优化,通信工程,电信增值,3G" />
<meta name="description" content="上海腾一信息技术有限公司专注于电信增值、通信工程、电子商务等领域,拥有近十年的经验。" />
<title>
欢迎您 - 上海腾一
</title>

?

插播一下广告:上海腾一信息技术有限公司是一家致力于通信工程、电子商务和电信增值业务的公司,请访问官方网址: http://www.sunrisecorp.net/ 。

?

?

此处省略掉网页内容 。


</body>
</html>[root@jfht ~]#

示例二 查看响应头信息

[root@jfht ~]# curl -I http://www.sunrisecorp.net/
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"17801-1285643951000"
Last-Modified: Tue, 28 Sep 2010 03:19:11 GMT
Content-Type: text/html
Content-Length: 17801
Date: Tue, 12 Oct 2010 12:49:20 GMT

[root@jfht ~]#

?

示例三 检查网页是否可正常访问

以下是一个tomcat监控脚本的部分内容。

?Linux下令之curl - 强大的网络传输工具Linux下令之curl - 强大的网络传输工具
    # usage: check_once <port> <path># 使用curl检查网页是否可以正常访问,如果不能访问就重启tomcat。check_once(){ echo echo "$(date) Tomcat check once" # 2008.08.21 -I/--head -s/--silent #if curl -s -I --connect-timeout 5 --max-time 10 http://localhost:$1/; then # 2010.02.16 add 200 OK test if curl -s -I --connect-timeout 5 --max-time 10 http://localhost:$1/$2 | grep -q '200 OK'; then echo "$(date) Tomcat maybe OK" else echo "$(date) Tomcat maybe FAULT" restart fi}# usage: check_loop <port> <path># 每隔一分钟检查一次网页是否正常check_loop(){ while true; do sleep 60 check_once $1 $2 >> $CATALINA_HOME/logs/check.$(date +%Y-%m-%d).log done}# usage: monitor <path># 对path指定的本机网页进行监控# 2008.06.26# 2010.09.20 add path parametermonitor(){ PORT=80 if grep 'Connector port="80"' $CATALINA_HOME/conf/server.xml; then PORT=80 elif grep 'Connector port="8080"' $CATALINA_HOME/conf/server.xml; then PORT=8080 else echo "Cannot detect server port for Tomcat" return 12 fi echo "Tomcat server port is $PORT" if status; then check_loop $PORT "$1" & #check_loop $PORT "$1" fi}?

    ?

    这个脚本的执行方式是 monitor <path>,比如monitor main/index.html。下面是执行时输出的日志信息片段。

    ?

    2010年 10月 09日 星期六 15:20:38 CST? Tomcat check once
    2010年 10月 09日 星期六 15:20:46 CST? Tomcat maybe OK

    2010年 10月 09日 星期六 15:21:46 CST? Tomcat check once
    2010年 10月 09日 星期六 15:21:57 CST? Tomcat maybe FAULT
    Tomcat is now running, not stopped: 0
    Tomcat is now running, not stopped: 1
    Tomcat is now running, not stopped: 2
    Tomcat is now running, not stopped: 3
    Tomcat is now running, not stopped: 4
    Tomcat is now running, not stopped: 5
    Tomcat is now running, not stopped: 6
    Tomcat is now running, not stopped: 7
    Tomcat is now running, not stopped: 8
    Tomcat is now running, not stopped: 9
    Tomcat killed use SIGKILL
    Tomcat stopped
    ????? Starting tomcat

    2010年 10月 09日 星期六 15:23:09 CST? Tomcat check once
    2010年 10月 09日 星期六 15:23:09 CST? Tomcat maybe OK

    2010年 10月 09日 星期六 15:24:09 CST? Tomcat check once
    2010年 10月 09日 星期六 15:24:09 CST? Tomcat maybe OK

    ?

    示例四 另一个检查网页是否正常的脚本

    ?

    ?Linux下令之curl - 强大的网络传输工具Linux下令之curl - 强大的网络传输工具
      # 要检查的网页地址URL="http://www.sunrisecorp.net/"# usage: curlit(){ curl --connect-timeout 15 --max-time 30 --head --silent "$URL" | grep 'HTTP/1.1 200 OK'}# 只有MIN_ALARM次访问失败时才告警MIN_ALARM=10# doit(){echo "===== $(now) ====="if ! curlit; thenecho "$(now) bbdx nss access failed"N=1if [ -e curlit_error ]; thenN="$(cat curlit_error)"N=$[N+1]fiecho "$(now) N=$N"echo $N >curlit_errorif [ "$N" == "$MIN_ALARM" ]; thenecho "$(now) do notify"touch curlit_errornotify_curlit_errorfielseif [ -e curlit_error ]; thenecho "$(now) recovered"N=$(cat curlit_error)echo "$(now) N=$N"rm -f curlit_errorif [ "$N" -ge "$MIN_ALARM" ]; thennotify_curlit_recoveredfififi}doit >>log/curlit.log 2>&1?

      ?

      示例五 使用HttpPost上传数据

      一个用于http post的脚本。

      ?Linux下令之curl - 强大的网络传输工具Linux下令之curl - 强大的网络传输工具
        #!/bin/shMS=1350514xxxxTM=$(date +"%Y%m%d%H%M%S")DT=$(date +"%Y%m%d")cat <<EOF >reqtmp.xml<?xml version="1.0" encoding="GBK" ?><OwnPlatForm> <OrigDomain>QZT</OrigDomain> <HomeDomain>BOSS</HomeDomain> <ActivityCode>T5100001</ActivityCode> <ActionCode>0</ActionCode> <TransIDO>$TM</TransIDO> <TransIDH></TransIDH> <ProcessTime>$TM</ProcessTime> <CutOffDay>$DT</CutOffDay> <TestFlag>0</TestFlag> <Content> <![CDATA[ <BizProcReq> <IDType>01</IDType> <IDValue>$MS</IDValue> <UserCity>14</UserCity> <UserCounty>1419</UserCounty> <OprCode>01</OprCode> <BizType>51</BizType> <OprTime>$TM</OprTime> <OprSrc>27</OprSrc> <ProductInfo> <PrdCode>510001</PrdCode> </ProductInfo> </BizProcReq> ]]> </Content></OwnPlatForm>EOFcat reqtmp.xmlURL="http://10.32.140.230:7092/fcgi-bin/UIG_NEWINT"curl --verbose --upload-file reqtmp.xml --header "Content-Type: text/xml" "$URL" --output rsptmp.xmlcat rsptmp.xml

        ?

        示例六 使用proxy的脚本?Linux下令之curl - 强大的网络传输工具Linux下令之curl - 强大的网络传输工具
          # usage: do_sync_once <mobile> <codes> <area_id> <opening>do_sync_once(){ mobile=$1 codes=$2 area_id=$3 opening=$4?? curl --silent --max-time 60 --proxy http://10.32.187.170:8080 "http://host/boss/sync.jsp?seq=1251747862492&mobile=$mobile&serviceCodes=$codes&areaId=$area_id&opening=$opening"}

          ?

          示例七 使用Google AJAX Search API进行搜索?Linux下令之curl - 强大的网络传输工具Linux下令之curl - 强大的网络传输工具
            # usage: google_search <STR># Google搜索google_search(){ REF="http://codingstandards.iteye.com/" KEY="ABQIAAAAHg_ENG5Yq9pOZd19v64gyxTMcdcN4KfyGCBxustvF1FXdNe4WBQOej_ZiBgIK6-a4M3hTxcVfSkt2g" STR="$1" # 采用网页搜索 curl --retry 5 -e "$REF" "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=$STR&hl=zh-CN&key=$KEY" 2>/dev/null # 采用博客搜索 #curl -e $REF "http://ajax.googleapis.com/ajax/services/search/blogs?v=1.0&q=$STR&hl=zh-CN" 2>/dev/null #curl --retry 5 -e $REF "http://ajax.googleapis.com/ajax/services/search/blogs?v=1.0&q=$STR&hl=zh-CN" 2>/dev/null #curl --retry 5 -e "$REF" "http://ajax.googleapis.com/ajax/services/search/blogs?v=1.0&q=$STR&hl=zh-CN&key=$KEY" 2>/dev/null}

            ?

热点排行