shell调用webservice

这篇博客介绍了如何利用shell脚本调用webservice,通过curl和xpath工具获取返回信息,强调了shell和Linux在自动化任务中的强大能力。

shell调用webservice
今日为了向同事讲解shell,特地用其调用webservice获得返回信息,展现shell的妙用。此次本人再次领略到shell和linux的强大。shell的关键之处是curl、xpath的使用。webservice使用公共的地址:http://ws.webxml.com.cn/webservices/DomesticAirline.asmx/getDomesticAirlinesTime,代码如下:

// shell
#!/usr/bin/env bash
#程序目的:通过bash调用webservice获取航空信息
#程序版本:bash5.0
#程序编写:枫叶
#编写时间:2020年3月15日
#平台:Fedora31
str="----------航空信息查询----------"
echo -e "${str}\n"
function get_schedule()
{
    flag=0
    req_time=$(date "+%Y-%m-%d %H:%M:%S")
    printf "${req_time} request...\n"
    #####定义变量方式获得信息#####
    start_city="桂林"
    arrive_city="上海"
    query_date="2020-3-18"
    res=`curl -d "startCity=${start_city}&lastCity=${arrive_city}&theDate=${query_date}&userID=" http://ws.webxml.com.cn/webservices/DomesticAirline.asmx/getDomesticAirlinesTime`
    #####定义变量方式获得信息#####

    #####读取文件方式获得信息#####
    #param=`cat airline.txt`
    #res=`curl -d "${param}" http://ws.webxml.com.cn/webservices/DomesticAirline.asmx/getDomesticAirlinesTime`
    #####读取文件方式获得信息#####
    res_time=$(date "+%Y-%m-%d %H:%M:%S")
    printf "${res_time} response...\n"
    printf "${res}\n"
    #重定向到文件
    echo -e "----------将查询结果保存到xml文件----------\n"
    file_head="airline"
    file_time=$(date "+%Y%m%d%H%M%S")
    file_suffix=".xml"
    file_name=${file_head}${file_time}${file_suffix}
    printf "${res}" > ${file_name}
    echo -e "-----------已保存为${file_name}-----------\n"
    #解析xml,人性化输出信息
    #获取返回的总条数
    counter=`grep -o "<AirlinesTime" ${file_name} | wc -l`
    #定义id前缀
    air_id="AirlinesTime"
    #循环进行人性化输出
    for ((i=1;i<`expr ${counter} + 1`;i++))
    do
        echo -e "序号:`expr ${flag} + 1`"
        echo -e "航空公司:"`xpath -e "//AirlinesTime[@diffgr:id='${air_id}${i}']/Company/text()" ${file_name}`
        echo -e "航空编码:"`xpath -e "//AirlinesTime[@diffgr:id='${air_id}${i}']/AirlineCode/text()" ${file_name}`
        echo -e "起飞机场:"`xpath -e "//AirlinesTime[@diffgr:id='${air_id}${i}']/StartDrome/text()" ${file_name}`
        echo -e "抵达机场:"`xpath -e "//AirlinesTime[@diffgr:id='${air_id}${i}']/ArriveDrome/text()" ${file_name}`
        echo -e "起飞时间:"`xpath -e "//AirlinesTime[@diffgr:id='${air_id}${i}']/StartTime/text()" ${file_name}`
        echo -e "抵达时间:"`xpath -e "//AirlinesTime[@diffgr:id='${air_id}${i}']/ArriveTime/text()" ${file_name}`
        echo -e "机型:"`xpath -e "//AirlinesTime[@diffgr:id='${air_id}${i}']/Mode/text()" ${file_name}`
        echo -e "经停:"`xpath -e "//AirlinesTime[@diffgr:id='${air_id}${i}']/AirlineStop/text()" ${file_name}`
        echo -e "操作平台:Bash"
        echo -e "飞行周期:"`xpath -e "//AirlinesTime[@diffgr:id='${air_id}${i}']/Week/text()" ${file_name}`
        echo -e "起飞城市:${start_city}"
        echo -e "抵达城市:${arrive_city}"
        echo -e "查询日期:${query_date}"
        echo -e "----------------------------------------------------------------------"
    done
}
#invoke function
get_schedule
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值