Shell 脚本 测试服务正常

中秋快乐。。。

昨天因为移动机房应用连接到电信机房不是很稳定,于是写了段shell进行自动探测。主要使用了JSON分析和截取。

 

#!/bin/bash

#file name : runTest.sh

#探测应用返回http状态码。

#set -e

fail=0      #初始化失败次数

tests=0   #初始化测试次数

succeed=0   #初始化成功次数

source JSON.sh     #引用JSON分析



while true    #无限循环

do

  let tests=$tests+1    #测试次数+1

  echo TEST: $tests 

  result=`curl -s "{URL}" |tokenize | parse | sed -e 's/"//g' | sed -n '2p' | awk ' { print $2 }'`   #curl命令 -s silent静音模式  | 调用 | 分词 | sed -e去掉"号 | sed -n 取得第二行内容 | 输入第二列 并把付值result

  if [ $result -eq 200 ] ;then     #http 200表示成功状态

	let succeed=$succeed+1

  else 

        let fail=$fail+1

  fi

  echo SUCCEED:$succeed    FAIL:$fail  

done

  URL 返回的是一串 JSON字符

     样式大概这样 :  

{"Request":"\/api\/1.0\/get","Status":"200","Msg":"OK","Data":{"Adid":"202-403","Lastupdate":"11111","Sessionid":"11111"}}

引用的JSON.sh方法 源自 https://github.com/dominictarr/JSON.sh/blob/master/JSON.sh


你可能感兴趣的:(shell)