#!/bin/bash # File: sendSMS.sh # Author: A.Lang # Purpose: Sendind the SMS thru FareaStone carrier # Created: 2014/01/13 # API_SYSID="ZM8WZ" API_SRCADD="019168000" API_URL="http://61.20.32.60:6600/mpushapi/smssubmit" Usage() { echo "Usage: $0 [-t ] [-n ]"; echo "For example: $0 -t \"Hello, SuperMan\" -n \"085713 457199\" " } while getopts ":t:n:" o; do case "$o" in t) t=$OPTARG ;; n) n=$OPTARG ;; \?) echo "Invalid option: -$OPTARG" Usage exit 1 ;; :) echo "Option -$OPTARG requires an argument !" Usage exit 1 ;; esac done if [ $OPTIND -ne 5 ]; then echo "Invalid options entered !" Usage exit 1 fi IN_TXT="$t" IN_PHONE="$n" function parseXML() { elemList=( $(echo $xmlFile | tr '\n' ' ' | XMLLINT_INDENT="" xmllint --format - | /bin/grep -e "$" | while read line; do \ echo $line | sed -e 's/^.*<\///' | cut -d '>' -f 1; \ done) ) totalNoOfTags=${#elemList[@]}; ((totalNoOfTags--)) suffix=$(echo ${elemList[$totalNoOfTags]} | tr -d '') suffix="${suffix}_" for (( i = 0 ; i < ${#elemList[@]} ; i++ )); do elem=${elemList[$i]} elemLine=$(echo $xmlFile | tr '\n' ' ' | XMLLINT_INDENT="" xmllint --format - | /bin/grep "") echo $elemLine | grep -e "^$" 1>/dev/null 2>&1 if [ "0" = "$?" ]; then continue fi elemVal=$(echo $elemLine | tr '\011' '\040'| sed -e 's/^[ ]*//' -e 's/^<.*>\([^<].*\)<.*>$/\1/' | sed -e 's/^[ ]*//' | sed -e 's/[ ]*$//') xmlElem="${suffix}$(echo $elem | sed 's/-/_/g')" eval ${xmlElem}=`echo -ne \""${elemVal}"\"` attrList=($(echo $xmlFile | tr '\n' ' ' | XMLLINT_INDENT="" xmllint --format - | /bin/grep "" | tr '\011' '\040' | sed -e 's/^[ ]*//' | cut -d '>' -f 1 | sed -e 's/^<[^ ]*//' | tr "'" '"' | tr '"' '\n' | tr '=' '\n' | sed -e 's/^[ ]*//' | sed '/^$/d' | tr '\011' '\040' | tr ' ' '>')) for (( j = 0 ; j < ${#attrList[@]} ; j++ )); do attr=${attrList[$j]} ((j++)) attrVal=$(echo ${attrList[$j]} | tr '>' ' ') attrName=`echo -ne ${xmlElem}_${attr}` eval ${attrName}=`echo -ne \""${attrVal}"\"` done done } function Process_PhoneNumbers(){ if [ -n "$@" ]; then API_DEST="" for no in $@ do API_DEST="${API_DEST}${no}" done fi } function PreCheck(){ NOT_OK=0 ## Check the command curl if ! which curl > /dev/null 2>&1; then NOT_OK=1 echo "The Command curl NOT Found!" fi ## Check the command base64 if ! which base64 > /dev/null 2>&1; then NOT_OK=1 echo "The Command base64 NOT Found !" fi ## Check the internet to FareaStone if ! curl -m 6 --silent --head $API_URL |egrep "200 OK" >/dev/null then NOT_OK=1 echo "It seems the internet is down !" fi if [ $NOT_OK -eq 1 ]; then return 0 else return 1 fi } function SendSMS(){ XML_STR='xml='${API_SYSID}''${API_SRCADD}''${API_DEST}''${API_TXT}'truefalse' RE=$(echo $XML_STR | curl -d @- $API_URL) } API_TXT=$(echo "$IN_TXT" | base64) if PreCheck; then echo "Aborted." exit 1 fi Process_PhoneNumbers "$IN_PHONE" SendSMS xmlFile=$RE parseXML echo "ResultCode: $SubmitRes_ResultCode" #Variables for each XML ELement echo "ResultText: $SubmitRes_ResultText" #Variables for each XML ELement echo ""