Syed Jahanzaib Personal Blog to Share Knowledge !

May 12, 2015

Sharing Ideas … Mikrotik with Kannel/playSMS

Filed under: Mikrotik Related — Syed Jahanzaib / Pinochio~:) @ 3:30 PM

UPDATED LINK is here:

https://aacable.wordpress.com/2015/05/25/sharing-ideas-renew-expired-user-account-via-sms-in-dmasoftlab-radius-manager/

Sharing an old idea , worked in Year 2008 …

mt - code found

Screenshot_2015-05-24-16-37-08

Post last updated: 20th May, 2015, 11:30AM

Changelog:
Added user friendly comments in the script section
Added Log entry in User Manager User comments section
Added Account Renewed Entry in the Return SMS Reply

24-05-2015   Added reboot script with protection code


 

Task:

  1. Renew expired users via sending Username and SCRATCH CODE to playSMS (Ubuntu Server)
  2. Reboot Linux or mikrotik server by sending sms with protection code (password)

 


Task # 1

  1. Renew expired users via sending Username and SCRATCH CODE to playSMS (Ubuntu Server)

Mikrotik is configured with User Manager.

playSMS with Kannel is already configured and working in separate box (Ubuntu)

 

Requirements:

  1. Mikrotik with User Manager . SSH must be enabled  (with public key to enable linux ssh connectivity to mikrotik without Password)
    Follow this guide
    https://aacable.wordpress.com/2011/11/25/howto-login-on-remote-mikrotik-linux-without-password-to-execute-commands/
  2. Linux OS with USB GSM modem [Teltonika usb modems are best, but you can use some cheaper modems like dlink dwm-156 used in this guide, Linux OS can be any flavor , I used Ubuntu in this example with playSMS & Kannel already configured)
    Follow this guide for kannel configuration
    https://aacable.wordpress.com/2015/05/08/fun-with-sms-management-system-playsms/

 

Remember, I took User Manager as an example and uses some low and un secure scripting method. The goal was to show you the capabilities and things we can do by combining various apps and cross platform OS to get our results. This is no way a perfect example, but I am sure it will give you some idea how things can be started : )

You can take this example , and you can generate you scratch card codes and store it in MYSQL DB and FREERADIUS, therefore you can then pull CODE from specific DB and act accordingly after receiving SMS. I am sharing just some thoughts : )

 

 


 

1- Renew expired users via sending SMS to playSMS (Ubuntu Server)

This one is a very basic task. It does not involve any code to profile match check.
In this method user send sms with his Username and the scratch CODE. Once the server will receive the sms, it will first check the USERNAME at mikrotik, if it found valid username, it will then check the CODE in local text files (in this example I created 3 files, 512k.txt 1mb.txt and 4mb.txt , script will check all of three files (infact *.txt files) and if it found the CODE, it will then ssh to mikrotik and re-activate the user account with the given code profile. if the code belongs to 512k, it will re-activate user with 512k profile, and if the code is from 1mb pacakge, it will re-activate the account with 1mb profile.

SMS with following format

REN zaib 123

Explanation:

REN is a keyword which SMS server will catch and consider it as renew command.
zaib is user id
123 is password


 

 

Pseudo Code:

Server receive SMS with username and code

Server will SSH into mikrotik and look for username

If valid username found then it proceed further, otherwise issue error INVALID USERNAME and EXIT

If valid username found, then script will pick the second variable (code) and will look for this variable in /temp/*.txt , if it found the CODE then proceed further, otherwise issue error INVALID CARD NUMBER

If valid CODE is found in text files, then script will SSH to mikrotik and will re-activate the user account according to the package (which was fetched from the text file) and also print LOG in the window. You can also create other functions too like Remove the SCRATCH code once used, email admin or user, etc etc

Here is the example:

First create text files with the Scratch codes

example

cat /temp/512k.txt

123

cat /temp/1mb.txt

555

cat /temp/4mb.txt

999

Add Command on playSMS Server

As showed in the image below … or read my previous guide on howto make custom commands in playSMS

https://aacable.wordpress.com/2015/05/08/fun-with-sms-management-system-playsms/

playsms_command


 

 

Create SCRIPT on SMS Server which will be executed after receiving SMS

 

Create following SCRIPT on SMS Server.

cat /temp/test.sh

#!/bin/bash
#set -x
# Syed Jahanzaib
# aacable.wordpress.com
# aacable@hotmail.com
# Version 1.3a
# Mikrotik IP Address
MT="101.11.11.255"
date=`date`

# Strip user name and card number separate oterhwise playsms will treat both variables as one
echo $1 > /tmp/user-card
USR=`cat /tmp/user-card | awk {' print $1 '}`
CARD=`cat /tmp/user-card | awk {' print $2 '}`

# Login to Mikrotik and look for User naem validity
validuser=`ssh admin@$MT :put [/tool user-manager user get value-name=actual-profile number=$USR]`
if [ -n "$validuser" ]; then

# If found, then print it with profile name, if account is expired profile name will not be printed
# Send return sms with username and its profile (if valid)
echo -e "User name - $USR - found with profile reported by Mikrotik = $validuser"

# Now Find EXACT match of card number
grep -Fx -r1 $CARD /temp/*.txt -q
              if [ $? -eq 0 ]; then
#If valid card number is found then record it in tmp file
grep -q -rl  $CARD /temp/*.txt  | sed 's/\///g' | sed 's/temp//g' |sed 's/\.//g' | sed 's/txt//g'

# TEST ZAIB
grep -rl  $CARD /temp/*.txt > /tmp/pkg
grep -rl $CARD /temp/*.txt | sed 's/\///g' | sed 's/temp//g' |sed 's/\.//g' | sed 's/txt//g' > /tmp/crd

VAR1=`cat /tmp/crd`
# Send return sms with Scratc card package
echo "Scratch CODE is valid & found in $VAR1 Package. "

# ssh to Mikrotik and activate the profile matched with the card number
ssh admin@$MT :put [/tool user-manager user create-and-activate-profile user=$USR customer=admin profile=$VAR1]

# Add comments in User COMMENTS section in user man
ssh admin@$MT :put [/tool user-manager user set comment="$USR_RENWED_BY_CARD_NUMBER_$VAR1" customer=admin numbers=zaib]

# Print Log entry
ssh admin@$MT /log warning \"$USR account have been renewed by scratch code $CARD at $date \"

# Also send following msg in return SMS to user
echo "Your Account have been Renewed on $date"
PKG=`cat /tmp/pkg`
sed -i "s/\<$CARD\>/This card is used by user id - $USR - and card number is USED_$CARD on $date/ig" /$PKG
#sed -i "s/\<$CARD\>/User $USR have used this card number_$CARD/ig" /$PKG

#Otherwise print errors if user name or card name not found
               else
                        /bin/echo "Invalid CARD Name!"
                fi
        else
                /bin/echo "Invalid User Name!"
        fi

 


 

TESTING ….

Send an sms to the server with following code

ren zaib 123

As soon the server will receive the SMS with the word REN, it will start act as following …

playSMS LOG [focus on sms__command # command_output:]

127.0.0.1 localhost 2015-05-20 01:01:14 PIDx- L2 kannel__call # start load:/var/www/playsms/plugin/gateway/kannel/geturl.php
127.0.0.1 localhost 2015-05-20 01:01:14 PIDx- L3 kannel__incoming # addr:127.0.0.1 host:localhost t:2015-05-20 05:01:11 q:+923333021909 a:ren zaib 123 Q:13013 smsc:[]
127.0.0.1 localhost 2015-05-20 01:01:14 PIDx- L3 recvsms # isrecvsmsd:1 dt:2015-05-20 05:01:11 sender:+923333021909 m:ren zaib 123 receiver:13013 smsc:
127.0.0.1 localhost 2015-05-20 01:01:14 PIDx- L2 kannel__call # end load geturl
- - 2015-05-20 01:01:14 PIDxxxadmin L3 recvsmsd # id:48 dt:2015-05-20 05:01:11 sender:+923333021909 m:ren zaib 123 receiver:13013 smsc:
- - 2015-05-20 01:01:14 PIDxxxadmin L3 setsmsincomingaction # dt:2015-05-20 05:01:11 sender:+923333021909 m:ren zaib 123 receiver:13013 smsc:
- - 2015-05-20 01:01:14 PIDxxxadmin L3 sms__command # command_exec:/var/lib/playsms/sms_command/1/test.sh 'zaib 123'
- - 2015-05-20 01:01:15 PIDxxxadmin L3 sms__command # command_output:User name - zaib 123 - found with profile reported by Mikrotik = 1mb  Scratch CODE is valid & found in 1mb Package.        Your Account have been Renewed
- - 2015-05-20 01:01:15 PIDxxxadmin L2 sendsms # start uid:1 sender_id:[1234] smsc:[]
- - 2015-05-20 01:01:15 PIDxxxadmin L3 sendsms # maxlen:1676 footerlen:7 footer:[ @admin] msglen:149 message:[User name - zaib 123 - found with profile reported by Mikrotik = 1mb Scratch CODE is valid & found in 1mb Package.     Your Account have been Renewed]
- - 2015-05-20 01:01:15 PIDxxxadmin L2 sendsms_queue_create # saving queue_code:fcedd9f9cb128b1baed4b015d660a35b src:1234 scheduled:2015-05-20 01:01:15
- - 2015-05-20 01:01:15 PIDxxxadmin L2 sendsms_queue_create # saved queue_code:fcedd9f9cb128b1baed4b015d660a35b id:40
- - 2015-05-20 01:01:15 PIDxxxadmin L3 sendsms_manipulate_prefix # before prefix manipulation:[+923333021909]
- - 2015-05-20 01:01:15 PIDxxxadmin L3 sendsms_manipulate_prefix # after prefix manipulation:[+923333021909]
- - 2015-05-20 01:01:15 PIDxxxadmin L3 simplerate_hook_rate_getbyprefix # rate not found to:+923333021909 default_rate:1
- - 2015-05-20 01:01:15 PIDxxxadmin L3 simplerate_hook_rate_getcharges # uid:1 u:admin len:156 unicode:0 to:+923333021909 enable_credit_unicode:0 count:1 rate:1 charge:1
- - 2015-05-20 01:01:15 PIDxxxadmin L2 sendsms # dst:1 sms_count:1 total_charges:1
- - 2015-05-20 01:01:15 PIDxxxadmin L2 sendsms_queue_push # saving queue_code:fcedd9f9cb128b1baed4b015d660a35b dst:+923333021909
- - 2015-05-20 01:01:15 PIDxxxadmin L2 sendsms_queue_push # saved queue_code:fcedd9f9cb128b1baed4b015d660a35b smslog_id:40
- - 2015-05-20 01:01:15 PIDxxxadmin L2 sendsms # end queue_code:fcedd9f9cb128b1baed4b015d660a35b queue_count:1 sms_count:1 failed_queue:0 failed_sms:0
- - 2015-05-20 01:01:15 PIDxxxadmin L3 setsmsincomingaction # feature:sms_command datetime:2015-05-20 05:01:11 sender:+923333021909 receiver:13013 keyword:REN message:zaib 123 raw:ren zaib 123 smsc:
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 sendsmsd # start processing queue_code:fcedd9f9cb128b1baed4b015d660a35b sms_count:1 scheduled:2015-05-20 01:01:15 uid:1 gpid:0 sender_id:1234
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 sendsmsd # sending queue_code:fcedd9f9cb128b1baed4b015d660a35b smslog_id:40 to:+923333021909 sms_count:1 counter:1
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 sendsms_intercept # msgtemplate modified sms_sender:[1234] sms_footer:[@admin] sms_to:[+923333021909] sms_msg:[User name - zaib 123 - found with profile reported by Mikrotik = 1mb Scratch CODE is valid & found in 1mb Package.     Your Account have been Renewed] uid:[1] gpid:[0] sms_type:[text] unicode:[0] queue_code:[fcedd9f9cb128b1baed4b015d660a35b] smsc:[]
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 outgoing_hook_sendsms_intercept # no SMSC found uid:1 parent_uid:0 from:1234 to:+923333021909
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 sendsms_process # start
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 simplerate_hook_rate_getbyprefix # rate not found to:+923333021909 default_rate:1
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 simplerate_hook_rate_getcharges # uid:1 u:admin len:156 unicode:0 to:+923333021909 enable_credit_unicode:0 count:1 rate:1 charge:1
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 simplerate__cansend # allowed user uid:1 sms_to:+923333021909 credit:111111083.000 count:1 rate:1 charge:1 balance:111111082
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 sendsms # saving smslog_id:40 u:1 g:0 gw:kannel smsc:kannel s:1234 d:+923333021909 type:text unicode:0 status:0
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 sendsms_process # saved smslog_id:40 id:40
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 sendsms # final smslog_id:40 gw:kannel smsc:kannel message:User name - zaib 123 - found with profile reported by Mikrotik = 1mb Scratch CODE is valid & found in 1mb Package.     Your Account have been Renewed @admin len:156
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 kannel_hook_sendsms # enter smsc:kannel smslog_id:40 uid:1 to:+923333021909
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 kannel_hook_sendsms # URL: http://localhost:13013/cgi-bin/sendsms?username=kannel&password=kannel&from=1234&to=%2B923333021909&dlr-mask=31&dlr-url=http%3A%2F%2F101.11.11.241%2Fplaysms%2Findex.php%3Fapp%3Dcall%26cat%3Dgateway%26plugin%3Dkannel%26access%3Ddlr%26type%3D%25d%26smslog_id%3D40%26uid%3D1&account=admin&text=User+name+-+zaib+123+-+found+with+profile+reported+by+Mikrotik+%3D+1mb%0AScratch+CODE+is+valid+%26+found+in+1mb+Package.+%0A%0A%0A%0AYour+Account+have+been+Renewed+%40admin
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 kannel__outgoing # smslog_id:40 response:0: Accepted for delivery
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 dlr # isdlrd:1 smslog_id:40 p_status:0 uid:1
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 kannel__outgoing # end smslog_id:40 p_status:0
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 simplerate__deduct # enter smslog_id:40
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 simplerate_hook_rate_getbyprefix # rate not found to:+923333021909 default_rate:1
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 simplerate_hook_rate_getcharges # uid:1 u:admin len:156 unicode:0 to:+923333021909 enable_credit_unicode:0 count:1 rate:1 charge:1
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 credit_hook_rate_setusercredit # saving uid:1 balance:111111082
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 credit_hook_rate_setusercredit # saved uid:1 balance:111111082
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 simplerate__deduct # user uid:1 parent_uid: smslog_id:40 msglen:156 count:1 rate:1 charge:1 credit:111111083.000 balance:111111082
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 simplebilling__post # saving smslog_id:40 rate:1 credit:111111083.000 count:1 charge:1
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 simplebilling__post # saved smslog_id:40 id:40
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 simplerate__deduct # deduct successful uid:1 parent_uid: smslog_id:40
- - 2015-05-20 01:01:16 PID555c151c1efaa - L3 simplerate__deduct # credit_lowest_limit:0 balance:111111082 charge:1
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 sendsms_process # end
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 sendsmsd # result queue_code:fcedd9f9cb128b1baed4b015d660a35b to:+923333021909 flag:1 smslog_id:40
- - 2015-05-20 01:01:16 PID555c151c1efaa - L2 sendsmsd # finish processing queue_code:fcedd9f9cb128b1baed4b015d660a35b uid:1 sender_id:1234 sms_count:1
- - 2015-05-20 01:01:17 PID555c0676a6be6 - L3 dlrd # id:40 smslog_id:40 p_status:0 uid:1
101.11.11.241 101.11.11.241 2015-05-20 01:01:22 PID555c15225bf5f - L2 kannel__call # start load:/var/www/playsms/plugin/gateway/kannel/dlr.php
101.11.11.241 101.11.11.241 2015-05-20 01:01:22 PID555c15225bf5f - L2 kannel__dlr # exit remote_addr:101.11.11.241 remote_host:101.11.11.241 bearerbox_host:localhost


Mikrotik LOG

& at Mikrotik LOG, You will see following

 

mtlog


 

 

SMS Reply Sent To The User …

and user will be replied with successful message
As showed in the image below …

mt - code found


 

Task # 2

 

  • 2- Reboot Linux or mikrotik server by sending sms with protection code (password)

 

 

Following script will reboot Linux server upon receiving SMS with protection code (password) adn this password must match with the /temp/password.txt content.


root@zaib:/var/lib/playsms/sms_command/1# cat radreboot.sh

#!/bin/bash
# Script to reboot with password protection matching with local
# By Syed Jahanzaib / aacable @ hotmail.com
# https://aacable.wordpress.com

# Local File which contain reboot password / CODE to be matched with sms parameter
LOCALPASS="localpass.txt"

# If no reboot code is supplied then Echo error and exit
if [ $1 = ]
   then
echo "No reboot code is supplied. Quitting Now !"
else

# Search for Password in /temp/password.txt and match it with supplied code from the SMS
grep -Fx -r1 $1 /temp/$LOCALPASS -q
              if [ $? -eq 0 ]; then

echo -e "Reboot code OK. This machine will reboot after 1 minute."

# REBOOT THIS MACHINE AFTER 1 Minute so that playSMS will get enough time to reply back the Ok response to the sender
echo /var/lib/playsms/sms_command/1/reboot1min.sh | at now + 1 min
exit

# Else print INVALID CODE if not matched
else
echo -e "ERROR: Invalid code for reboot!"
fi
fi

NOTE:

Don’t forget to create /temp/localpass.txt as showed below

root@zaib:/var/lib/playsms/sms_command/1# cat /temp/local pass.txt

12345

ALSO make sure you create following script as well which will be executed after one minute which will restart this server
root@zaib:/var/lib/playsms/sms_command/1# cat reboot1min.sh

shutdown -r now

Now create COMMAND from the playSMS menu.

As showed in the image below …

radreboot

Now send sms as following

reboot 12345

 As showed in the image below …

Screenshot_2015-05-24-16-37-08


 

I will add more details  later …

Regard’s
Syed Jahanzaib

UPDATED LINK is here:

https://aacable.wordpress.com/2015/05/25/sharing-ideas-renew-expired-user-account-via-sms-in-dmasoftlab-radius-manager/

8 Comments »

  1. As Salamo Alaikum
    Zaib bhai! Can we use this script in Radiusmanager.

    Comment by Nadeem Ahmad — May 20, 2015 @ 9:46 PM

  2. Alsalam Alikom

    Dear Syed

    Can you give us some exampels using thise senareo on radius manager

    Thank you

    Comment by ministaralex — May 25, 2015 @ 12:41 PM

  3. Alsalam Alikom

    Dear syed
    Can you give us some exampels using radius manager .

    Comment by ministaralex — May 25, 2015 @ 12:44 PM


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: