Ajout du script d'envoi de sms via routeur Huawei

master
root 2 years ago
parent 7c8e7b2bd5
commit ce3251d183
  1. 150
      sms/huawei_sms
  2. 11
      sms/huawei_sms.zabbix_alert_script

@ -0,0 +1,150 @@
#!/bin/bash
function display_help() {
echo "${0} Usage :"
echo -e "\t-u --user\tSpecify the username"
echo -e "\t-p --password\tSpecify the password"
echo -e "\t-i --ip\tSpecify the IP Address of the Huawei router"
echo -e "\t-n --number\tSpecify Tel number to text"
echo -e "\t-h or --help\tThis help"
echo ""
echo "Examples :"
echo -e "\tSend \"My message\" to your mobile phone :"
echo -e "\t\t${0} -i 192.168.0.1 -u myuser -p mypassword -n 0612345678 My message"
echo -e "\tSend content from STDIN to your mobile phone :"
echo -e "\t\t${0} -i 192.168.0.1 -u myuser -p mypassword -n 0612345678 -"
}
GET_OPT=`getopt -o hu:p:i:n: --long help,user:,password:,ip:,number: -n "${0} parameters" -- "${@}"`
if [ ! "${?}" == "0" ] ; then
display_help
exit 1
fi
eval set -- "$GET_OPT"
while true ; do
case ${1} in
-u|--user)
NAME="${2}"
shift 2
;;
-p|--password)
PASS="${2}"
shift 2
;;
-i|--ip)
IP="${2}"
shift 2
;;
-n|--number)
NUMBER="${2}"
shift 2
;;
-h|--help)
display_help
shift
exit 0
;;
--)
MESSAGE="`echo ${@} | sed 's|^-- ||g'`"
shift
break
;;
*)
display_help >&2
exit 1
;;
esac
done
if [ -z "${NAME}" -o -z "${PASS}" -o -z "${IP}" -o -z "${NUMBER}" ] ; then
echo "You need to specify Username,Password,IP and Number" >&2
display_help >&2
exit 1
fi
if [ "${MESSAGE}" == "-" ] ; then
MESSAGE=$(cat -)
fi
if [ -z "${MESSAGE}" ] ; then
echo "You need to specify the message to send" >&2
display_help >&2
exit 1
fi
if ! which which > /dev/null 2>&1 ; then
echo "which is not installed"
exit 1
elif which curl > /dev/null 2>&1 ; then
echo using curl
BIN="curl --silent"
else
echo "curl is not installed" >&2
exit 1
fi
TMP_HEADER_FILE=`mktemp -t $(basename ${0}).XXXXXX`
ProcessRouterResponseHeader()
{
# Get token from header
NEWTOKEN=`cat ${TMP_HEADER_FILE} | grep "__RequestVerificationTokenone: " | awk -F' ' '{print $2}'`
if [ ! -z "${NEWTOKEN}" ]; then TOKEN=${NEWTOKEN}; fi
NEWTOKEN=`cat ${TMP_HEADER_FILE} | grep "__RequestVerificationToken: " | awk -F' ' '{print $2}'`
if [ ! -z "${NEWTOKEN}" ]; then TOKEN=${NEWTOKEN}; fi
NEWSESSIONID=`cat ${TMP_HEADER_FILE} | grep "Set-Cookie: SessionID=" | awk -F' ' '{print $2}' | cut -b 1-138`
if [ ! -z "${NEWSESSIONID}" ]; then SESSIONID=${NEWSESSIONID}; fi
echo ${NEWTOKEN}
}
GetRouterData() # Param1: Relative URL
{
#echo "GET on http://$ROUTER_IP$1"
RESPONSE=`${BIN} --request GET http://${IP}${1} \
--dump-header ${TMP_HEADER_FILE} \
-H "Cookie: ${SESSIONID}" -H "__RequestVerificationToken: ${TOKEN}" -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \
`
ProcessRouterResponseHeader
}
GetSessionToken() # No parameters
{
# Get SessionID and RequestVerificationToken
GetRouterData '/api/webserver/SesTokInfo'
SESSIONID="SessionID="`echo "${RESPONSE}"| grep -oPm1 "(?<=<SesInfo>)[^<]+"`
TOKEN=`echo "${RESPONSE}"| grep -oPm1 "(?<=<TokInfo>)[^<]+"`
}
PostRouterData() # Param1: RelativeUrl, Param2: Data, Param3:bAskNewToken
{
# Get new token if necessary
if [ ! -z $3 ]; then
GetSessionToken
fi
#echo "POST on http://$ROUTER_IP$1 :" $2
RESPONSE=`${BIN} --request POST http://${IP}${1} \
--dump-header ${TMP_HEADER_FILE} \
-H "Cookie: ${SESSIONID}" -H "__RequestVerificationToken: ${TOKEN}" -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \
--data "${2}"`
ProcessRouterResponseHeader
}
# Get initial SessionID and RequestVerificationToken
GetSessionToken
# Login
CREDENTIALS=`printf ${PASS} | sha256sum | head -c64 | base64 -w0`
CREDENTIALS=`printf "%s%s%s" ${NAME} ${CREDENTIALS} ${TOKEN} | sha256sum | head -c64 | base64 -w0`
DATA=`printf "<request><Username>%s</Username><Password>%s</Password><password_type>4</password_type></request>" ${NAME} ${CREDENTIALS}`
PostRouterData "/api/user/login" "${DATA}"
# Send SMS
DATA="<?xml version='1.0' encoding='UTF-8'?><request><Index>-1</Index><Phones><Phone>${NUMBER}</Phone></Phones><Sca></Sca><Content>${MESSAGE}</Content><Length>${#MESSAGE}</Length><Reserved>1</Reserved><Date>`date +'%F %T'`</Date></request>"
PostRouterData "/api/sms/send-sms" "${DATA}" 1
# Logout
PostRouterData "/api/user/logout" "<request><Logout>1</Logout></request>"
rm ${TMP_HEADER_FILE}

@ -0,0 +1,11 @@
#!/bin/bash
ROUTERIP=`echo ${1} | awk '{ print $1 }'`
USER=`echo ${1} | awk '{ print $2 }'`
PASS=`echo ${1} | awk '{ print $3 }'`
NUMBER=`echo ${1} | awk '{ print $4 }'`
body=${2}
cat <<EOF | /usr/bin/huawei_sms -i ${ROUTERIP} -u ${USER} -p ${PASS} -n ${NUMBER} -
${body}
EOF
Loading…
Cancel
Save