Skip to content

Instantly share code, notes, and snippets.

@CuberL
Created November 22, 2018 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CuberL/fa758c2361a8ee2d89b201b491bb0050 to your computer and use it in GitHub Desktop.
Save CuberL/fa758c2361a8ee2d89b201b491bb0050 to your computer and use it in GitHub Desktop.
sendsms
AccessKeyId LTAIL****N6gpzW6
SignatureVersion 1.0
SignatureMethod HMAC-SHA1
Format JSON
Action SendSms
Version 2017-05-25
RegionId cn-hangzhou
SignName ****
TemplateCode ****
#!/bin/bash
# Usage
# echo -e "PhoneNumber 1300000000\nTemplateParam {...}" | ./sendsms.sh
secretKey="z71BMf**********fB58aQdlSqxNZ7"
urlencode() {
local str=`echo ${@:1}`;
local length=${#str};
for (( i = 0; i < length; i++ )); do
local c="${str:i:1}"
case $c in
[a-zA-Z0-9.*\-_]) printf "$c" ;;
'\ ') printf "+" ;;
'%') printf "%%25" ;;
*) printf "$c" | xxd -p -u -c1 | while read x;do printf "%%%s" "$x";done
esac
done
}
special_urlencode() {
result=`urlencode $1 | sed 's/+/%20/g' | sed "s/\*/\%2A/g" | sed "s/\%7E/~/g"`
echo $result
}
getArgs() {
paras=`echo "${@:1}" | sort | awk '{printf("$(special_urlencode \"%s\")=$(special_urlencode \"%s\")&", $1, $2)}'`
cmd='echo "'${paras}'"'
args=$(eval $cmd)
echo ${args%?}
}
sign() {
stringToSign=`echo $(special_urlencode "${@:1}")`
sign=`echo -n "GET&%2F&${stringToSign}" | openssl dgst -binary -sha1 -hmac "${secretKey}&" | base64`
signEncoded=`echo $(special_urlencode "$sign")`
echo $signEncoded
}
send() {
input=`echo "${@:1}"`
uuid="SignatureNonce $(uuidgen)"
date="Timestamp $(TZ=GMT date +%Y-%m-%dT%H:%M:%SZ)"
args=`echo -e "$input\n$uuid\n$date"`
echo "$args"
allArgs=$(getArgs "$args")
signValue=$(sign "$allArgs")
data=`echo "${allArgs}&Signature=${signValue}"`
# echo "http://dysmsapi.aliyuncs.com?${data}"
curl "http://dysmsapi.aliyuncs.com?${data}"
}
send "`paste -s -d "\n" /dev/stdin ./config`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment