Last active
March 30, 2018 00:34
-
-
Save gotnix/4326286 to your computer and use it in GitHub Desktop.
使用curl POST数据,调用Http接口发送短信。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Zabbix alert script via E-mas. | |
#curl -v -i -H "Accept: application/json" -X POST --data-urlencode 'jsondata={"cmd":"1001","username":"UserName","userpassword":"PassWord","key":"EMASKEYS","timestamp":"","channel_id":"5","mobiles":"'$tz'","sendtime":"","smscontent":"Order:T-343651581-Status: Curl-Shipped","srccharset":"GBK","smsid":"201211022014"}' http://10.0.0.1/EMAS/sms_server.jsp | |
CURL='/usr/bin/curl -w %{http_code} -s -H "Accept: application/json" -H "Content Type: application/json" -X POST --data-urlencode' | |
EMAS_URL="http://10.0.0.1/EMAS/sms_server.jsp" | |
CHID_CM=5 | |
CHID_CU=4 | |
CHID_CN=7 | |
EMAS_USER="UserName" | |
EMAS_PW="PassWord" | |
TIME_STAMP=$(date +%Y%m%d%H%M%S) | |
EMAS_CH=0 | |
CELL_PHON=${1} | |
MESS_ORIG=${3} | |
MESS=$(echo ${3} | iconv -f UTF8 -t GBK) | |
SMSID=$(date +%s) | |
################################################################ | |
# 检查要发送的短信中是否包含URL需要转义的特殊字符, | |
# 如果有% , 替换成ASCII 表示, 并把短信的字符集从UTF8 转换成 GBK. | |
# | |
# 以下代码把这个功能实现得很土,把curl 的参数 -d 换成 | |
# --data-urlencode 完美解决URL 特殊字符需要转义的问题. | |
#if [[ ${MESS_ORIG} =~ .*%.* ]] ; then | |
# HANDLE=$(eval "echo ${MESS_ORIG} | sed s/%/%25/g") | |
# MESS=$(echo ${HANDLE} | iconv -f UTF8 -t GBK) | |
#else | |
# MESS=$(echo ${MESS_ORIG} | iconv -f UTF8 -t GBK) | |
#fi | |
################################################################ | |
function send_sms() { | |
# Parameter1 is E-mas channel ID. | |
local ch_id=$1 | |
local phone_num=$2 | |
local emas_json='jsondata={"cmd":"1001","username":"'"${EMAS_USER}"'","userpassword":"'"${EMAS_PW}"'","key":"EMASKEYS","timestamp":"'"${TIME_STAMP}"'","channel_id":"'"${ch_id}"'","mobiles":"'"${CELL_PHON}"'","sendtime":"","smscontent":"'"${MESS}"'","srccharset":"GBK","smsid":"'"${SMSID}"'"}' | |
local send="${CURL} '${emas_json}' ${EMAS_URL} | grep 0" | |
local result=$(eval ${send}) | |
logger -s -t zabbix_sms "${CELL_PHON} send status is ${result}" | |
# ToDo: Confirm the message send status; | |
# Confirm curl http code. | |
} | |
# 检查手机号码位数 | |
if [ ${#CELL_PHON} -ne 11 ] ; then | |
logger -s -t zabbix_sms "Please check you cell phone number." | |
exit 44 | |
fi | |
case ${CELL_PHON:0:3} in # 截取手机号前3 位判断运营商号段 | |
13[4-9]|15[0-2]|15[7-9]|18[23]|18[78]) | |
# 中国移动号段 | |
EMAS_CH="${CHID_CM}" | |
send_sms ${EMAS_CH} | |
;; | |
1[35]3|18[01]|189) | |
# 中国电信号段 | |
EMAS_CH="${CHID_CN}" | |
send_sms ${EMAS_CH} | |
;; | |
13[0-2]|15[56]|18[56]) | |
# 中国联通手机发送通道 | |
EMAS_CH="${CHID_CN}" | |
send_sms ${EMAS_CH} | |
;; | |
*) | |
echo "Chechk your cell phone number." | |
;; | |
esac | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment