We manage the server via SMS

    It all started with the fact that I dug out a huaweiE1550 USB modem, which I bought last summer to organize a backup Internet channel, in a shelf with glands. He did not work long then and, as unnecessary, was removed to the “bunker” until better times. The first thing I did was unlock it for working with MTS (it so happened historically that I prefer this particular operator). Initially, it came up with the idea of ​​sending SMS with warnings from Nagios, instead of mail. After a quick run through the Internet, I came across an smsd daemon to send / receive sms-ok from the smstools package. After reading the documentation on this beast, the idea came to my mind that you can receive messages from the necessary phones, with commands for the server. So the idea was born “Manage the server via SMS”, which can be used for anything: reboot the server, turn it off,

    We configure the modem


    First you need to make friends with our modem and Linux (by the way, I'm using Centos 5.5). We stick the modem into one of the free usb ports. The first thing that you have to deal with is that the modem is defined as a CD-ROM, but as you know, you won’t send SMS messages and you won’t receive them. In order to fix this matter for us, 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 with Windows installed on hand, 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 put the packagesusb_modeswitch and minicomyum --enablerepo=rpmforge install usb_modeswitch minicom , then create / edit /etc/usb-modeswitch.conf : And drag the modem to another port, wait 5-10 seconds (it is necessary for the modem to be detected as a CD-ROM) and run as root and see about the following : New ttyUSB devices should appear : We start, configure the serial port to work with / dev / ttyUSB0, exit the settings, start the terminal, then give the command AT ^ U2DIAG = 0 and get ok in response The procedure for turning the modem into a modem is finished, we are in the process of installation / configuring smstools.
    DefaultVendor = 0x12d1
    DefaultProduct = 0x1446
    MessageEndPoint = "0x01"
    MessageContent = "55534243000000000000000000000011060000000000000000000000000000"


    usb_modeswitch
    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.



    ls /dev | grep ttyUSB
    ttyUSB0
    ttyUSB1
    ttyUSB2


    minicom –s




    Smstools


    Oddly enough, in the huge rpmforge repositories there was no place for such a most useful package as smstools. But it doesn’t 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” it rpm –i smstools-3.0.10-4.el5.i386.rpm. We set up smstools, the file /etc/sms.conf : the settings are pretty clear, start the daemon, and check this miracle: (does not understand the Cyrillic alphabet, I need to convert it to UCS-2BE , I won’t consider this article) and wait for SMS messages on the mobile phone . If the coveted message did not come, put loglevel = 7 in the config and go for the tambourine. Everything rose from me the first time.
    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


    service smsd startsmssend 9128141111 ‘test message’

    smsctrl daemon


    So we can talk, we must 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.* with the following contents: Accordingly, we will check these files for the presence of commands for managing the server. There are two ways to do this: 1st small daemon on bash, 2nd built-in event handler in smsd.
    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



    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=$1export 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 $DAEMONecho
              ;;
        *)
              echo"Usage: $DAEMON {start|stop|restart|status|run}"exit 1
      esacexit 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 pass phrases, etc. it was decided to accept commands only from certain numbers (I did not check how the SMS would look from the replacement numbers) and check for the presence of special characters in front of the team in order to fence off a random SMS.

    Save demon /etc/init.d/smsctrl , chkconfig --add smsctrl,service smsctrl start

    2nd method


    Add to /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=$1export 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 the file /root/bin/test.sh with the following contents:
    #!/bin/bash
    
    ls –la /etc | grep $1

    Then we pick up the phone and send an SMS with the text #/root/bin/test.sh smsto the coveted number and look at the log tail –f /var/log/smsctrl.log, if everything is fine, an SMS of the form will arrive in response:smsd.conf

    Conclusion


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

    Useful links for setting up huawei E1550 and smsd
    1: SMSTools 3 - gateway for sending SMS
    2: Need an article about SMS center with a Huawei E1550 modem?
    3: How to “tame” the MTS-modem Huawei E1550

    Thank you for your attention, I look forward to your comments.

    UPD According to the remark of my good friend, I added the implementation of processing SMS messages using smsd.

    Also popular now: