julmajanne.com

Use kannel SMS-Gateway with wget

You can wget commandline program to send sms messages via Kannel SMS-gateway. Here is a quite simple bash-program that I'm using to check if one of my VPN-connections is still alive.

Here is the check_vpn script:

#!/bin/bash
#################################################################################
#                                                                               #
# Check if selected network interface is present. If not                        #
# then send text-message to selected gsm-numbers.                               #
#                                                                               #
#    Author: Janne Mikkonen                                                     #
# Copyright: 2007 - Janne Mikkonen                                              #
#            2007 - Mediamaisteri Group                                         #
#   License: http://www.opensource.org/licenses/mit-license.php MIT License     #
#                                                                               #
# Copyright (c) 2007 Janne Mikkonen http://julmajanne.com                       #
#               2007 Mediamaisteri Group http://www.mediamaisteri.com           #
#                                                                               #
# Permission is hereby granted, free of charge, to any person obtaining a copy  #
# of this software and associated documentation files (the "Software"), to deal #
# in the Software without restriction, including without limitation the rights  #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     #
# copies of the Software, and to permit persons to whom the Software is         #
# furnished to do so, subject to the following conditions:                      #
#                                                                               #
# The above copyright notice and this permission notice shall be included in    #
# all copies or substantial portions of the Software.                           #
#                                                                               #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN     #
# THE SOFTWARE.                                                                 #
#################################################################################
#
# Define variables
#

PATH=/bin:/sbin:/usr/bin:/usr/sbin
export $PATH

NUMBERS="+35850125784"              #Recipients separated with spaces
GWHOST="http://123.123.123.123"     #SMS-gw address
GWPORT=14000                        #SMS-gw port
GWPATH="/cgi-bin/sendsms"           #SMS-gw command path
GWUSER="youruser"                   #SMS-gw username
GWPASS="yourpass"                   #SMS-gw password

# Check if user gave an interface to search for?
if [ $# -lt 1 ]; then
    FIND='ppp'
else
    FIND=$1
fi

#
# Messages to send
#
MESSAGE="Could not find given interface! Connection lost!"
ERRMSG="Could not create new connection!"
#
# Check if needed interface is present
#

CONN=$(/sbin/ifconfig | grep -E $FIND | cut -d' ' -f1)
if [ -z $CONN ]; then
    wget --spider -q -O /dev/null \
        "${GWHOST}:${GWPORT}${GWPATH}?username=${GWUSER}&password=${GWPASS}&text=$MESSAGE&to=${NUMBERS}&charset=UTF-8"
    /path/to/start_vpn # Path to start_vpn script.
    RETVAL=$?
    if [ $RETVAL -eq 1 ]; then
        wget --spider -q -O /dev/null \
            "${GWHOST}:${GWPORT}${GWPATH}?username=${GWUSER}&password=${GWPASS}&text=${ERRMSG}&to=${NUMBERS}"
    fi
fi

start_vpn -script:

#!/bin/bash
PATH=/bin:/sbin:/usr/sbin:/usr/bin
export PATH

TOUT=10
COUNT=0

pppd pty 'pptp 231.231.231.231 --nolaunchpppd' call xxx
RETVAL=$?

if [ $RETVAL -eq 0 ]; then
    while [ $COUNT -lt $TOUT ]; do
        COUNT=$(($COUNT+1))
        TEST=$(ifconfig | grep -E ppp | cut -d' ' -f1)
        if [ -z $TEST ]; then
            sleep 1
        else
            route add -host 111.111.111.111 dev $TEST
            exit 0
        fi
    done
    exit 1
fi

The firs IP-address is the address that should accept VPN-connections and the second one
is the IP-address which is reponsible for the service that we want to use.