📜 ⬆️ ⬇️

We manage the server via SMS

It all started with the fact that I dug out a USB modem huaweiE1550, which I bought last summer to organize a backup Internet channel, in a shelf with glands of USB. He then worked for a short time and as unnecessary was removed to the "bins" until better times. The first thing I did was unlock it to work with MTS (historically, I prefer this particular operator). Initially, the idea of ​​sending SMS with warnings from Nagios, instead of mail, came to mind. Run through the Internet, stumbled upon the smsd daemon to send / receive SMS from the smstools package. After reading the documentation for this beast, I got the idea that you can receive messages from the right phones, with commands for the server. This is how the idea of ​​“Manage server via SMS” was born.

Configuring the modem


First you need to make friends with our modem and Linux (by the way, I use Centos 5.5). We stick the modem into one of the free usb ports. The first thing you have to face is that the modem is defined as a CD-ROM, and you don’t send SMS from a CD-ROM, and you don’t receive it. In order to fix this case, you just need to feed the modem this command: AT ^ U2DIAG = 0 (0 - only modem, 1 - modem + cd-rom, 255 - modem + cd-rom + cardreader, 256 - modem + cardreader ). If you have a computer at hand with Windows installed, then open HyperTerminal, connect to the modem, enter the command: AT ^ U2DIAG = 0 and skip the next step.

So, we force the modem to be a modem, and not some kind of CD-ROM under Linux. First you need to install the usb_modeswitch and minicom yum --enablerepo=rpmforge install usb_modeswitch minicom packages yum --enablerepo=rpmforge install usb_modeswitch minicom , then create / edit /etc/usb-modeswitch.conf :
DefaultVendor = 0x12d1
DefaultProduct = 0x1446
MessageEndPoint = "0x01"
MessageContent = "55534243000000000000000000000011060000000000000000000000000000"


And we preempt the modem to another port, it is necessary to wait 5-10 seconds (it is necessary that the modem is defined as a CD-ROM) and as root we run usb_modeswitch and see something like the following:
Looking for target devices ...
No devices in target mode or class found
Looking for default devices ...
Found default devices (1)
Accessing device 004 on bus 007 ...
Using endpoints 0x01 (out) and 0x81 (in)
Inquiring device details; driver will be detached ...
Looking for active driver ...
OK, driver found ("usb-storage")
OK, driver "usb-storage" detached

SCSI inquiry data (for identification)
-------------------------
Vendor String: HUAWEI
Model String: Mass Storage
Revision String: 2.31
-------------------------

USB description data (for identification)
-------------------------
Manufacturer: HUAWEI Technology
Product: HUAWEI Mobile
Serial No.: not provided
-------------------------
Setting up communication with interface 0 ...
Trying to send the message to endpoint 0x01 ...
OK, message successfully sent
Device is gone, skipping any further commands
-> Run lsusb to note any changes. Bye.


Should there be new ttyUSB devices
ls /dev | grep ttyUSB ls /dev | grep ttyUSB :
ttyUSB0
ttyUSB1
ttyUSB2


We start minicom –s configure a serial port for work with / dev / ttyUSB0,
Exit the settings, the terminal starts, then you need to send the command AT ^ U2DIAG = 0 and get ok in response

The procedure of turning the modem into a modem is over, we are going to install / configure smstools.
')

Smstools


Strangely enough, in huge rpmforge repositories there was no place for such a most useful package as smstools. But it does not matter, on the Internet and on the manufacturer's website it is enough. I found the package: smstools-3.0.10-4.el5.i386.rpm and “used” them rpm –i smstools-3.0.10-4.el5.i386.rpm. Configure smstools, file /etc/sms.conf :
devices = huaweiE1550
logfile = /var/log/smsd.log
loglevel = 2

[huaweiE1550]
device = /dev/ttyUSB0
baudrate = 115200
rtscts = no
init = at+cpms="sm","sm",""
incoming = yes
incoming = high


The settings are clear, we start the daemon, service smsd start and check this miracle: smssend 9128141111 'test message' (does not understand Cyrillic, needs to be converted to UCS-2BE , I’ll not consider this in this article) and wait for a sms on a mobile phone. If the coveted message did not come, put in the config loglevel = 7 and go for the tambourine. I got everything up the first time.

smsctrl daemon


So we can talk, you need to learn to listen!
If you send an SMS to the SIM card number in the modem, after a while smsd will create a file in /var/spool/sms/incoming/huaweiE1550.* of the following format:
From: 79128141111
From_TOA: 91 international, ISDN/telephone
From_SMSC: 79126313431
Sent: 11-03-02 08:05:46
Received: 11-03-02 08:08:09
Subject: huaweiE1550
IMSI: 2500XXXXXXXXXXX
Report: no
Alphabet: ISO
UDH: false

Test message

Accordingly, we will check these files for the presence of commands to control the server. There are two ways for this: the 1st small daemon on bash, the 2nd built-in event handler in smsd.

1st method

 #!/bin/sh # SMSCtrl # chkconfig: - 55 45 # description: Sms control, Egor N. Zuskin, 2011, http://www.it2k.ru/projects/smsctrl . /etc/rc.d/init.d/functions DAEMON=smsctrl REFRESH_TIME=15 COMMAND_CHAR="#" INCOMING_DIR=/var/spool/sms/incoming ALLOW_PHONES="79128141111 79128141112" SEND_BACK_REPORT=YES to_log(){ text=$1 export LANG=en_EN log_date=`date "+%b %d %H:%M:%S "` log_host=`hostname -s` echo "$log_date $log_host $DAEMON: $text" >> /var/log/$DAEMON.log } start() { echo -n "Starting $DAEMON: " $0 --daemon && success || failure RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/$DAEMON to_log "Starting ..." return $RETVAL } stop() { # Stop daemon. echo -n "Shutting down $DAEMON: " killproc $0 RETVAL=$? to_log "Stopping ..." echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/$DAEMON } run() { for File in $(ls $INCOMING_DIR); do Allow=0 for Phone in $ALLOW_PHONES; do cat $INCOMING_DIR/$File | grep "From: $Phone" > /dev/null 2>&1 [ $? -eq 0 ] && Allow=1 done; [ $Allow -eq 0 ] && continue cat $INCOMING_DIR/$File | grep "$COMMAND_CHAR" [ $? -ne 0 ] && continue FromPhone=`cat $INCOMING_DIR/$File | grep "From:" | cut -d " " -f2` command=`cat $INCOMING_DIR/$File | grep "$COMMAND_CHAR" | cut -d "$COMMAND_CHAR" -f2` to_log "Incoming command: $command from $FromPhone" out=`$command` if [ "$SEND_BACK_REPORT" = "YES" ]; then smssend $FromPhone "$out" to_log "Send sms to $FromPhone: $out" fi rm -f $INCOMING_DIR/$File to_log "Deleting file $INCOMING_DIR/$File" done } daemon() { exec >/dev/null exec 2>/dev/null ( trap "" TERM while [ true ]; do run sleep $REFRESH_TIME; done; )& } case "$1" in --daemon) daemon ;; run) run ;; start) start ;; stop) stop ;; restart) $0 stop $0 start exit $? ;; status) status $DAEMON echo ;; *) echo "Usage: $DAEMON {start|stop|restart|status|run}" exit 1 esac exit 0 


COMMAND_CHAR="#" –
INCOMING_DIR=/var/spool/sms/incoming – -
ALLOW_PHONES="79128141111 79128141112" –
SEND_BACK_REPORT=YES – -


In order not to bother with all sorts of passages phrases, etc. it was decided to accept commands only from certain numbers (did not check how SMS from substitution numbers would look) and make a check for the presence of a special character in front of the command in order to isolate from a random SMS.

Save the daemon in /etc/init.d/smsctrl , chkconfig --add smsctrl , service smsctrl start

2nd method


We add in / etc / smsd.conf :
eventhandler = /root/bin/sms_event.sh
create /root/bin/sms_events.sh
 #!/bin/bash COMMAND_CHAR="#" ALLOW_PHONES="79128141111 79128141112" SEND_BACK_REPORT=YES [ "$1" = "RECEIVED" ] || exit 0 to_log(){ text=$1 export LANG=en_EN log_date=`date "+%b %d %H:%M:%S "` log_host=`hostname -s` echo "$log_date $log_host $text" >> /var/log/smsctrl.log } File=$2 Allow=0 for Phone in $ALLOW_PHONES; do cat $File | grep "From: $Phone" > /dev/null 2>&1 [ $? -eq 0 ] && Allow=1 done; [ $Allow -eq 0 ] && exit 0 cat $INCOMING_DIR/$File | grep "$COMMAND_CHAR" [ $? -ne 0 ] && exit 0 FromPhone=`cat $File | grep "From:" | cut -d " " -f2` command=`cat $File | grep "$COMMAND_CHAR" | cut -d "$COMMAND_CHAR" -f2` to_log "Incoming command: $command from $FromPhone" out=`$command` if [ "$SEND_BACK_REPORT" = "YES" ]; then smssend $FromPhone "$out" to_log "Send sms to $FromPhone: $out" fi rm -f $File to_log "Deleting file $File" 


Check


Create a /root/bin/test.sh file with the following contents:
 #!/bin/bash ls –la /etc | grep $1 

Then we take the phone in our hands and send an SMS with the text #/root/bin/test.sh sms to the coveted number and look at the tail –f /var/log/smsctrl.log log, if all is well, we’ll #/root/bin/test.sh sms in response : smsd.conf

Conclusion


I have this solution, by SMS opens the ssh port for incoming connections. I think this is not the only application, just turn on a little imagination.

Useful links to customize huaweiE1550 and smsd
1: SMSTools 3 - gateway to send SMS
2: Need an article on SMS center with Huawei E1550 modem?
3: How to "tame" MTS-modem Huawei E1550

Thank you for your attention, looking forward to your comments.

UPD As noted by my good friend, I added the implementation of the processing of leading SMS messages by means of smsd.

Source: https://habr.com/ru/post/114912/


All Articles