Пример работы с SMS шлюзом для командной обочки Bash

Скрипт для оболочки Bash позволяет отправлять сообщения прямо с консоли пользователя, протестирован на версии 4.4.12.

Отправка СМС:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
set -u
 
LOGIN='your login'
PASSWORD='your password'
 
function sendSms {
    local phones
    phones=("${@:5}")
 
    local phonesXml
    for ix in ${!phones[*]}
    do
       phonesXml+="<number>${phones[$ix]}</number>"
    done
    local data="XML=\
        <sms>\
            <operations>\
                <operation>SEND</operation>\
            </operations>\
            <authentification>\
                <username>$1</username>\
                <password>$2</password>\
            </authentification>\
            <message>\
                <sender>$3</sender>\
                <text>$4</text>\
            </message>\
            <numbers>${phonesXml}</numbers>\
        </sms>"
    #echo -e "$data"
    curl --data "$data" $URL
}
 
function getPrice {
    local phones
    phones=("${@:5}")
 
    local phonesXml
    for ix in ${!phones[*]}
    do
       phonesXml+="<number>${phones[$ix]}</number>"
    done
    local data="XML=\
        <sms>\
            <operations>\
                <operation>GETPRICE</operation>\
            </operations>\
            <authentification>\
                <username>$1</username>\
                <password>$2</password>\
            </authentification>\
            <message>\
                <sender>$3</sender>\
                <text>$4</text>\
            </message>\
            <numbers>${phonesXml}</numbers>\
        </sms>"
    #echo -e "$data"
    curl --data "$data" $URL
}
 
function getStatus {
    local numbers
    numbers=("${@:3}")
    local statisticsXml
    for ix in ${!numbers[*]}
    do
       statisticsXml+="<messageid>${numbers[$ix]}</messageid>"
    done
    local data="XML=\
        <sms>\
            <operations>\
                <operation>GETSTATUS</operation>\
            </operations>\
            <authentification>\
                <username>$1</username>\
                <password>$2</password>\
            </authentification>\
            <statistics>\
                $statisticsXml\
            </statistics>\
        </sms>"
    #echo -e "$data"
    curl --data "$data" $URL
}
 
function getBalance {
    local data="XML=\
        <sms>\
            <operations>\
                <operation>BALANCE</operation>\
            </operations>\
            <authentification>\
                <username>$1</username>\
                <password>$2</password>\
            </authentification>\
        </sms>"
    #echo -e "$data"
    curl --data "$data" $URL
}
 
function getCreditPrice {
    local data="XML=\
        <sms>\
            <operations>\
                <operation>CREDITPRICE</operation>\
            </operations>\
            <authentification>\
                <username>$1</username>\
                <password>$2</password>\
            </authentification>\
        </sms>"
    #echo -e "$data"
    curl --data "$data" $URL
}
 
function logging {
    echo -en "$1";
}
 
args=("$@")
 
usage="$(basename "$0") [-h] [-a (send|price|status|credit_price|balance)] [-s n] [-t n] [-p n] [-n n] -- xml sms gateway for sending sms
 
where:
    -h  show this help text
    -a  \033[4m action_name \033[0m
        \033[1m send \033[0m send sms on phones
        \033[1m price \033[0m price sms on phones
        -s  sender name
        -t  text sms
        -p  phones (example: '380933630000 380933630001')
        \033[1m status \033[0m show status on phones by phone's id (special attributes)
        -n  phones's id
        \033[1m balance \033[0m show user banance
        \033[1m credit_price \033[0m show user price"
 
while getopts ":hs:a:s:t:p:" opt; do
    case $opt in
        a) ACTION="${OPTARG}" ;;
        s) SENDER="${OPTARG}" ;;
        t) TEXT="${OPTARG}" ;;
        n) NUMBERS="${OPTARG}" ;;
        p) STR_PHONES="${OPTARG}" ;;
        h) echo -en "$usage"
           printf "\n"
           exit
           ;;
        \?) printf "illegal option: -%s\n" "$OPTARG" >&2
            echo -en "$usage" >&2
            printf "\n"
            exit 1
            ;;
        :) logging "Option -${OPTARG} requires an argument." ; exit 252 ;;
    esac
done
shift $(($OPTIND - 1))
 
 
if [[ "${ACTION:-unset}" == "unset" ]]; then
    echo -e "Parameters ACTION not found. "
    exit 1
fi
 
case $ACTION in
    send)
        if [[ "${SENDER:-unset}" == "unset" ]]; then
            echo -e "Parameters SENDER not found. "
            exit 1
        fi
        if [[ "${TEXT:-unset}" == "unset" ]]; then
            echo -e "Parameters TEXT not found. "
            exit 1
        fi
        if [[ "${STR_PHONES:-unset}" == "unset" ]]; then
            echo -e "Parameters PHONES not found. "
            exit 1
        fi
        PHONES=()
        for phone in $STR_PHONES; do
            PHONES+=("$phone")
        done
        sendSms $LOGIN $PASSWORD "$SENDER" "$TEXT" ${PHONES[*]} ;;
    price)
        if [[ "${SENDER:-unset}" == "unset" ]]; then
            echo -e "Parameters SENDER not found. "
            exit 1
        fi
        if [[ "${TEXT:-unset}" == "unset" ]]; then
            echo -e "Parameters TEXT not found. "
            exit 1
        fi
        if [[ "${STR_PHONES:-unset}" == "unset" ]]; then
            echo -e "Parameters PHONES not found. "
            exit 1
        fi
        PHONES=()
        for phone in $STR_PHONES; do
            PHONES+=("$phone")
        done
        getPrice $LOGIN $PASSWORD "$SENDER" "$TEXT" ${PHONES[*]} ;;
    status)
        if [[ "${NUMBERS:-unset}" == "unset" ]]; then
            echo -e "Parameters NUMBERS not found. "
            exit 1
        fi
        PHONES=()
        for phone in $NUMBERS; do
            PHONES+=("$phone")
        done
        getStatus $LOGIN $PASSWORD ${PHONES[*]} ;;
    credit_price)
        getCreditPrice $LOGIN $PASSWORD ;;
    balance)
        getBalance $LOGIN $PASSWORD ;;
    :) logging "Option -${OPTARG} requires an argument." ; exit 252 ;;
esac
printf "\n"
vector
image

Нужна помощь?

Задайте вопрос специалисту технической поддержки