SMS Server Tools 3
This site is hosted by Kekekasvi.com
 Menu
Basic information:
Additional information:
Support:
Get SMS Server Tools 3:
Additional Options

 Sponsored links

 Search
Custom Search

 Visitor locations
 
 SMS Server Tools 3 Community
Welcome, Guest. Please login or register. Sat Apr 20, 2024 01:15
SMSTools3 Community » Sample scripts / setups Bottom

example multiscript (eventhandler)

Login and Post Reply

Page:  1

Author Post
Member
Registered:
Jul 2011
Location: Warsaw, Poland
#!/bin/bash

# system log
sys_log()
  {
  logger -t smsd-handler -i "$1 from no.: $FROM"
  }

# write sms to smsd
send_sms()
  {
  FILETMP=$(mktemp /tmp/_tmp_handler_send_XXXXXX)
  # \n = new line
  /bin/echo -e "To: $FROM\n\n$1" > $FILETMP
  FILESMS=$(mktemp /var/spool/sms/outgoing/handler_send_XXXXXX)
  mv "$FILETMP" "$FILESMS"
  }

# expression for if
reg_expr()
  {
  /bin/echo -n "$TEXT" | grep -i "$1" >/dev/null
  }

if [ "$1" == "RECEIVED" ]; then
  # allowed numbers
  ALLOWED_NO=" 12345678900 12345678901 12345678902 "
  FROM=$(formail -zx From: < $2)
  key=" $FROM "

        if [[ $ALLOWED_NO == *$key* ]]; then
          TEXT=$(sed -e '1,/^$/ d' < "$2")

                # UPTIME check
                if reg_expr "uptime password"; then
                UPTIME=$(uptime)
                               
                sys_log "Authorized uptime"
                send_sms "$UPTIME"
                               
                # PING check
                elif reg_expr "ping password"; then

                 # ping function
                 my_ping()
                 {
                 /bin/ping -qc5 -s1 $1 | grep transmitted
                 }

                 status1=$(my_ping 192.168.0.2)
                 status2=$(my_ping 192.168.1.2)

                 set $status1
                 n1=$6
                 set $status2
                 n2=$6

                sys_log "Authorized ping"
                send_sms "Ping lost:\n# N1: $n1 # N2: $n2"
                               
                # REBOOT
                elif  reg_expr "reboot password"; then
                send_sms "Rebooting Server"

                sys_log "Authorized reboot"
                sleep 20; sudo /sbin/reboot
                else
                  # nothing
                  exit
                fi
        else
          sys_log "Unuthorized action"
          # or
          ##send_sms "You are not authorized"
        fi
fi
 
 
'bash' Syntax Highlight powered by GeSHi


Reboot perms for smsd (/etc/sudoers):


Small example for others

Member
Registered:
Jul 2011
Location: Warsaw, Poland
Topic owner
Errata:



Administrator
Registered:
May 2009
Location: Jyväskylä, Finland
Hi,

If sender is not in ALLOWED_NO list, perhaps "Unauthorized action" or "Unauthorized sender" is still okay? And if action is not recognized, "Unknown action" could be logged (and answered)?

Member
Registered:
Jul 2011
Location: Warsaw, Poland
Topic owner
i changed last time end of the script "not authorized" section is a crap ;)



if number isn't in ALLOWED_NO then sms is forwarded to my mail address.

Member
Registered:
Jul 2011
Location: Warsaw, Poland
Topic owner
eventhadler with ticket system, etc...

RECEIVED:

sms functions from $ALLOWED_NO ->
sms text: uptime password
answer "X:y:Z up X days, 20:22, X users, load average: X, Y, Z"

sms text: ip password
answer "IP: X.Y.Z.Z"

sms text: ping password
answer
"www.example1.com: 0% lost
192.168.0.2: 50% lost
172.19.64.2: 100% lost
user1.examplelan.net: 0% lost"

sms text: ping password 1.2.3.4 www.example2.com ftp.xyzexample.com
answer
"1.2.3.4: 0% lost
www.example2.com: 50% lost
ftp.xyzexample.com: 100% lost"

sms text: reboot password
answer "Reboot in progress" and execute reboot.

sms text: blah
no answering and log to syslog "Wrong command or password (blah)"

From other numbers ->
if number begins with "48" and hours are from 08:45 to 19:00 then
example SMS: blah
answer: "SMS Ticket no. 11/07/2 has been received and forwarded to specialists."
forwarded to $WHOS sms:
"Ticket no. 11/07/2
FROM_NUMBER: blah"
and forwarded to $MAILTO mail

if number begins with "48" and hours are from 19:01 to 08:44 then
example SMS: blah
answer: "SMS Ticket no. 11/07/2 has been received, will be checked as soon as posible."
forwarded to $WHOS sms at 8:50 (am) hour:
"Ticket no. 11/07/2 (11-07-03 22:32:39)
FROM_NUMBER: blah"
and forwarded to $MAILTO mail

if not begins with "48"
only sms to $MAILTO is send

--------------------

CALL:
if call is from number begins with "48" then
hangup and sending SMS: "Only text messages are accepted. One-time info"
number is saved to log, and will not be notified again

other calls: only hangup and log.

#!/bin/bash

sys_log()
  {
  logger -t smsd-handler -i "$1 from no.: $FROM"
  }

send_sms()
  {
  FILESMS=$(mktemp /var/spool/sms/outgoing/handler_send_XXXXXX)
  /bin/echo -e "To: $FROM\n\n$1" > $FILESMS
  }

send_sms_night()
  {
  FILESMS=$(mktemp  /var/spool/sms/myfiles/_ticket_night_send_XXXXXX)
  /bin/echo -e "To: $WHO\n\n$1" > $FILESMS
  }

  # /etc/crontab
  # after night: send at 8:50
  # 50 8    * * *  smsd    find /var/spool/sms/myfiles/ -name "_ticket_night_send*" -type f -exec mv {} /var/spool/sms/outgoing/ \; >/dev/null

  send_sms_day()
  {
  FILESMS=$(mktemp /var/spool/sms/outgoing/_ticket_day_send_XXXXXX)
  /bin/echo -e "To: $WHO\n\n$1" > $FILESMS
  }

reg_expr()
  {
  /bin/echo -n "$TEXT" | grep -i "$1" >/dev/null
  }

if [ "$1" == "RECEIVED" ]; then
  ALLOWED_NO=" 12345678900 12345678901 12345678901 "
  FROM=$(formail -zx From: < $2)
  key=" $FROM "

        if [[ $ALLOWED_NO == *$key* ]]; then
          TEXT=$(sed -e '1,/^$/ d' < "$2")

                if reg_expr "uptime password"; then
                 UPTIME=$(uptime)
                sys_log "Authorized uptime"
                send_sms "$UPTIME"

                elif reg_expr "ip password"; then
                 IPU="$(ifconfig eth0| sed -n '/^[A-Za-z0-9]/ {N;/dr:/{;s/.*dr://;s/ .*//;p;}}')"
                sys_log "Authorized ip check"
                send_sms "IP: $IPU"

                elif reg_expr "ping password"; then
                 TEXT=$(formail -I "" <$2|sed 's/ping password \?//');
                 if [ "$TEXT" == "" ]; then
                  TEXT="www.example1.com 192.168.0.2 172.19.64.2 user1.examplelan.net"
                 fi

                   for ip in $TEXT; do
                        ping="$ip: $(/bin/ping -qc5 -s1 $ip | grep transmitted | awk '{print $6}') lost\n";
                        pings+="$ping"
                   done

                 send_sms "$pings"
                sys_log "Authorized ping"

                elif  reg_expr "reboot password"; then
                 send_sms "Reboot in progress"
                sys_log "Authorized reboot"
                sleep 20; sudo /sbin/reboot
                #
                #/etc/sudoers
                # smsd ALL=NOPASSWD:/sbin/reboot

                else
                 sys_log "Wrong command or password ($TEXT)"
                fi

        else
         sys_log "New SMS"

# ^48 = my country code
FROM_TMP="$(echo $FROM | grep ^48)"
SMSTEXT="$(formail -I "" <$2)"

# admin mail
MAILTO="login1@example1.com"

 if [ -n "$FROM_TMP" ]; then

MYTIME=$(date +%H%M)
ISTIME=$(expr $MYTIME \> 0844)$(expr $MYTIME \< 1901)
TICKET_NO_DATE=$(date +%g/%m)
TICKET_FILE=/var/spool/sms/myfiles/tickets_no

# IT specialists numbers.
WHOS="12345678905 12345678906"

        if [ ! -f "$TICKET_FILE" ]; then
           echo 0 > "$TICKET_FILE"
        fi

TICKET_NO=$(($(cat $TICKET_FILE)+1))

  echo "$TICKET_NO" > "$TICKET_FILE"

  if [ "$ISTIME" = "11" ]; then

         /bin/echo -e "SMS Ticket no.: $TICKET_NO_DATE/$TICKET_NO\n$FROM: $SMSTEXT\n\n-- \nAutomatic info" | mail $MAILTO -s "SMS Ticket: $FROM"
         send_sms "SMS Ticket no. $TICKET_NO_DATE/$TICKET_NO has been received and forwarded to specialists."

                   for where in $WHOS; do
                   WHO="$where";
                   send_sms_day "Ticket $TICKET_NO_DATE/$TICKET_NO\n$FROM: $SMSTEXT"
                   done
  else

         /bin/echo -e "SMS Ticket no.: $TICKET_NO_DATE/$TICKET_NO\n$FROM: $SMSTEXT\n\n-- \nAutomatic info" | mail $MAILTO -s "Night SMS Ticket: $FROM"
         send_sms "SMS Ticket no. $TICKET_NO_DATE/$TICKET_NO has been received, will be checked as soon as posible."

                   SEND_TIME=$(formail -zx Sent: < $2)
                   for where in $WHOS; do
                   WHO="$where";
                   send_sms_night "Ticket $TICKET_NO_DATE/$TICKET_NO ($SEND_TIME)\n$FROM: $SMSTEXT"
                   done
   fi

  else
        /bin/echo -e "SMS from: $FROM\n$SMSTEXT\n\n-- \nAutomatic info" | mail $MAILTO -s "SMS: $FROM"

  fi
 fi
fi

if [ "$1" == "CALL" ]; then

# ^48 = my country code
 FROM=$(formail -zx From: < $2 | grep ^48)

 if [ -n "$FROM" ]; then

  LISTCALLS=/var/spool/sms/myfiles/listcalls

        if [ ! -f "$LISTCALLS" ]; then
           touch "$LISTCALLS"
        fi

  CALLGREP=$(grep "^$FROM$" "$LISTCALLS")

        if [ "$FROM" != "$CALLGREP" ]; then
           echo "$FROM" >> "$LISTCALLS"
           sys_log "Not listed CALL"
           send_sms "Only text messages are accepted. One-time info"
        else
           sys_log "Listed CALL"

        fi

  fi
fi
 
 
'bash' Syntax Highlight powered by GeSHi


Login and Post Reply

Page:  1

SMSTools3 Community » Sample scripts / setups Top

 
Time in this board is UTC.  

Privacy Policy   SMS Server Tools 3 Copyright © Keijo Kasvi.