CropID=' ' Secret=' ' GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" #get acccess_token token=$(/usr/bin/curl -s -G $GURL| awk -F\" '{print $4}') PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$token" # function body() { local int AppID=1000004 #企业号中的应用id local UserID="touser" #部门成员id,zabbix中定义的微信接收者 local PartyID=1 #部门id,定义了范围,组内成员均可接收到消息 local Msg=$(echo "$@" | cut -d" " -f3-) #过滤出zabbix传递的第三个参数 printf '{\n' printf '\t"touser": "'"$UserID"\"",\n" printf '\t"toparty": "'"$PartyID"\"",\n" printf '\t"msgtype": "text",\n' printf '\t"agentid": "'" $AppID "\"",\n" printf '\t"text": {\n' printf '\t\t"content": "'"$Msg"\""\n" printf '\t},\n' printf '\t"safe":"0"\n' printf '}\n' } /usr/bin/curl --data-ascii "$(body $! $2 $3)" $PURL
上面的demo是在度娘上常见的,正常调用微信api会返回: shell
那么问题出在哪里呢? // 请从新查看上面demo 配置完成CropID 以及 Secret以后咱们的GURL是正常的 能够打印下查看api
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" echo $GURL #get acccess_token token=$(/usr/bin/curl -s -G $GURL| awk -F\" '{print $4}')
打印结果 获取到GURL以后脚本输入服务器
curl -v 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwc******bf4&corpsecret=xEhgjy******5tQ'
在这里咱们能够看到curl请求URL HTTP返回200请求是成功的,access_token也是可以正常获取到。 前方高能:坑来了, 根据上方的demo,咱们在输出下一句微信
token=$(/usr/bin/curl -s -G $GURL| awk -F\" '{print $4}') echo $token
返回: 上图咱们能够看到CURL请求后,shell awk正则匹配筛选后的值并非先前咱们所获取到的access_tokencurl
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" #get acccess_token token=$(/usr/bin/curl -s -G $GURL| awk -F\" '{print $4}') echo $token PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$token"
相信看到这里你们已经明确了,curl是能够正常请求,问题就出在awk正则上, shell awk正则具体用法你们能够自行百度。 这里提供出正确的正则匹配。url
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" #get acccess_token Gtoken=$(/usr/bin/curl -s -G $GURL) a=`echo $Gtoken |awk -F ':"' '{print $3}'` token=`echo $a |awk -F '".' '{print $1}'` PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$token"
将上面代码替换到开头的demo中,你们也能够自行echo查看输出内容。 正则是一次次筛选的,掌握后能够进行一次正则匹配。 替换好的代码咱们再来调用:
能够看到微信已经能够正常接受到服务器发送的信息。code
但愿能够帮助到你们再用demo的同时掌握其中奥妙而不是一味盲用。 高手勿喷。token