Just to share an old idea gathered in year 2008 …
Scenario:
Mikrotik is configured with User Manager.
playSMS with Kannel is already configured and working in separate box (Ubuntu)
Task:
- Renew expired users via sending Username and SCRATCH CODE to playSMS (Ubuntu Server)
- Renew expired users via sending SCRATCH CODE & verify the code with the user profile [later]
Requirements:
- 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/ - Linux OS with USB GSM modem (Like Ubuntu or other with playSMS & Kannel configured)
Follow this guide
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
Make 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/
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 ########################################## # Script to renew User Manager account via received SMS # Version 1.0b ######################################## # Strip user name and code because playsms will consider both variables as one, therefore we have to strip it in file echo $1 > /tmp/user-card USR=`cat /tmp/user-card | awk {' print $1 '}` CARD=`cat /tmp/user-card | awk {' print $2 '}` # Login to Mikrotik with SSH and look for validity of username validuser=`ssh admin@101.11.11.255 :put [/tool user-manager user get value-name=actual-profile number=$USR]` # If valid username found then proceed if [ -n "$validuser" ]; then echo -e "User name - $1 - found with profile reported by Mikrotik = $validuser" # Look for SCRATCH code, and if it found valid then proceed # You can also create other functions too like Remove the SCRATCH code once it is used , IMPORTANT ! grep -r1 $CARD /temp/*.txt -q if [ $? -eq 0 ]; then grep -q -rl $CARD /temp/*.txt | sed 's/\///g' | sed 's/temp//g' |sed 's/\.//g' | sed 's/txt//g' 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` echo "Scratch CODE found in list = $VAR1" # Login to mikrotik and re-activate or re-assign the profile to the user according to the package found in text files ssh admin@101.11.11.255 :put [/tool user-manager user create-and-activate-profile user=$USR customer=admin profile=$VA$ ssh admin@101.11.11.255 [:log warning Account_REFREH_DONE_ID_IS_$USR] # If card number is invalid, then issue error else /bin/echo "Invalid CARD Name!" fi else # If username is invalid, then issue error /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-12 15:04:32 PID5551d030b76d8 - L2 kannel__call # start load:/var/www/playsms/plugin/gateway/kannel/geturl.php 127.0.0.1 localhost 2015-05-12 15:04:32 PID5551d030b76d8 - L3 kannel__incoming # addr:127.0.0.1 host:localhost t:2015-05-12 10:04:40 q:+923333021909 a:Ren zaib 123 Q:13013 smsc:[] 127.0.0.1 localhost 2015-05-12 15:04:32 PID5551d030b76d8 - L3 recvsms # isrecvsmsd:1 dt:2015-05-12 10:04:40 sender:+923333021909 m:Ren zaib 123 receiver:13013 smsc: 127.0.0.1 localhost 2015-05-12 15:04:32 PID5551d030b76d8 - L2 kannel__call # end load geturl - - 2015-05-12 15:04:33 PID5551c430f1edc admin L3 recvsmsd # id:37 dt:2015-05-12 10:04:40 sender:+923333021909 m:Ren zaib 123 receiver:13013 smsc: - - 2015-05-12 15:04:33 PID5551c430f1edc admin L3 setsmsincomingaction # dt:2015-05-12 10:04:40 sender:+923333021909 m:Ren zaib 123 receiver:13013 smsc: - - 2015-05-12 15:04:33 PID5551c430f1edc admin L3 sms__command # command_exec:/var/lib/playsms/sms_command/1/test.sh 'zaib 123' - - 2015-05-12 15:04:33 PID5551c430f1edc admin L3 sms__command # command_output:User name - zaib 123 - found with profile reported by Mikrotik = 1mb Scratch CODE found in list = 1mb - - 2015-05-12 15:04:33 PID5551c430f1edc admin L2 sendsms # start uid:1 sender_id:[1234] smsc:[] - - 2015-05-12 15:04:33 PID5551c430f1edc admin L3 sendsms # maxlen:1676 footerlen:7 footer:[ @admin] msglen:102 message:[User name - zaib 123 - found with profile reported by Mikrotik = 1mb Scratch CODE found in list = 1mb] - - 2015-05-12 15:04:33 PID5551c430f1edc admin L2 sendsms_queue_create # saving queue_code:0d756b8e3ac750e4a513bc76f2398489 src:1234 scheduled:2015-05-12 15:04:33 - - 2015-05-12 15:04:33 PID5551c430f1edc admin L2 sendsms_queue_create # saved queue_code:0d756b8e3ac750e4a513bc76f2398489 id:30 - - 2015-05-12 15:04:33 PID5551c430f1edc admin L3 sendsms_manipulate_prefix # before prefix manipulation:[+923333021909] - - 2015-05-12 15:04:33 PID5551c430f1edc admin L3 sendsms_manipulate_prefix # after prefix manipulation:[+923333021909] - - 2015-05-12 15:04:33 PID5551c430f1edc admin L3 simplerate_hook_rate_getbyprefix # rate not found to:+923333021909 default_rate:1 - - 2015-05-12 15:04:33 PID5551c430f1edc admin L3 simplerate_hook_rate_getcharges # uid:1 u:admin len:109 unicode:0 to:+923333021909 enable_credit_unicode:0 count:1 rate:1 charge:1 - - 2015-05-12 15:04:33 PID5551c430f1edc admin L2 sendsms # dst:1 sms_count:1 total_charges:1 - - 2015-05-12 15:04:33 PID5551c430f1edc admin L2 sendsms_queue_push # saving queue_code:0d756b8e3ac750e4a513bc76f2398489 dst:+923333021909 - - 2015-05-12 15:04:33 PID5551c430f1edc admin L2 sendsms_queue_push # saved queue_code:0d756b8e3ac750e4a513bc76f2398489 smslog_id:30 - - 2015-05-12 15:04:33 PID5551c430f1edc admin L2 sendsms # end queue_code:0d756b8e3ac750e4a513bc76f2398489 queue_count:1 sms_count:1 failed_queue:0 failed_sms:0 - - 2015-05-12 15:04:33 PID5551c430f1edc admin L3 setsmsincomingaction # feature:sms_command datetime:2015-05-12 10:04:40 sender:+923333021909 receiver:13013 keyword:REN message:zaib 123 raw:Ren zaib 123 smsc: - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 sendsmsd # start processing queue_code:0d756b8e3ac750e4a513bc76f2398489 sms_count:1 scheduled:2015-05-12 15:04:33 uid:1 gpid:0 sender_id:1234 - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 sendsmsd # sending queue_code:0d756b8e3ac750e4a513bc76f2398489 smslog_id:30 to:+923333021909 sms_count:1 counter:1 - - 2015-05-12 15:04:34 PID5551d03283b74 - 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 found in list = 1mb] uid:[1] gpid:[0] sms_type:[text] unicode:[0] queue_code:[0d756b8e3ac750e4a513bc76f2398489] smsc:[] - - 2015-05-12 15:04:34 PID5551d03283b74 - L3 outgoing_hook_sendsms_intercept # no SMSC found uid:1 parent_uid:0 from:1234 to:+923333021909 - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 sendsms_process # start - - 2015-05-12 15:04:34 PID5551d03283b74 - L3 simplerate_hook_rate_getbyprefix # rate not found to:+923333021909 default_rate:1 - - 2015-05-12 15:04:34 PID5551d03283b74 - L3 simplerate_hook_rate_getcharges # uid:1 u:admin len:109 unicode:0 to:+923333021909 enable_credit_unicode:0 count:1 rate:1 charge:1 - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 simplerate__cansend # allowed user uid:1 sms_to:+923333021909 credit:111111091.000 count:1 rate:1 charge:1 balance:111111090 - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 sendsms # saving smslog_id:30 u:1 g:0 gw:kannel smsc:kannel s:1234 d:+923333021909 type:text unicode:0 status:0 - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 sendsms_process # saved smslog_id:30 id:30 - - 2015-05-12 15:04:34 PID5551d03283b74 - L3 sendsms # final smslog_id:30 gw:kannel smsc:kannel message:User name - zaib 123 - found with profile reported by Mikrotik = 1mb Scratch CODE found in list = 1mb @admin len:109 - - 2015-05-12 15:04:34 PID5551d03283b74 - L3 kannel_hook_sendsms # enter smsc:kannel smslog_id:30 uid:1 to:+923333021909 - - 2015-05-12 15:04:34 PID5551d03283b74 - 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%3D30%26uid%3D1&account=admin&text=User+name++-+zaib+123+-+found+with+profile+reported+by+Mikrotik+%3D+1mb%0AScratch+CODE+found+in+list+%3D+1mb+%40admin - - 2015-05-12 15:04:34 PID5551d03283b74 - L3 kannel__outgoing # smslog_id:30 response:0: Accepted for delivery - - 2015-05-12 15:04:34 PID5551d03283b74 - L3 dlr # isdlrd:1 smslog_id:30 p_status:0 uid:1 - - 2015-05-12 15:04:34 PID5551d03283b74 - L3 kannel__outgoing # end smslog_id:30 p_status:0 - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 simplerate__deduct # enter smslog_id:30 - - 2015-05-12 15:04:34 PID5551d03283b74 - L3 simplerate_hook_rate_getbyprefix # rate not found to:+923333021909 default_rate:1 - - 2015-05-12 15:04:34 PID5551d03283b74 - L3 simplerate_hook_rate_getcharges # uid:1 u:admin len:109 unicode:0 to:+923333021909 enable_credit_unicode:0 count:1 rate:1 charge:1 - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 credit_hook_rate_setusercredit # saving uid:1 balance:111111090 - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 credit_hook_rate_setusercredit # saved uid:1 balance:111111090 - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 simplerate__deduct # user uid:1 parent_uid: smslog_id:30 msglen:109 count:1 rate:1 charge:1 credit:111111091.000 balance:111111090 - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 simplebilling__post # saving smslog_id:30 rate:1 credit:111111091.000 count:1 charge:1 - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 simplebilling__post # saved smslog_id:30 id:30 - - 2015-05-12 15:04:34 PID5551d03283b74 - L3 simplerate__deduct # deduct successful uid:1 parent_uid: smslog_id:30 - - 2015-05-12 15:04:34 PID5551d03283b74 - L3 simplerate__deduct # credit_lowest_limit:0 balance:111111090 charge:1 - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 sendsms_process # end - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 sendsmsd # result queue_code:0d756b8e3ac750e4a513bc76f2398489 to:+923333021909 flag:1 smslog_id:30 - - 2015-05-12 15:04:34 PID5551d03283b74 - L2 sendsmsd # finish processing queue_code:0d756b8e3ac750e4a513bc76f2398489 uid:1 sender_id:1234 sms_count:1 - - 2015-05-12 15:04:35 PID5551c430ef414 - L3 dlrd # id:30 smslog_id:30 p_status:0 uid:1
& at Mikrotik LOG, You will see following
I w ill add more details and SMS Return reply screenshot later …
Regard’s
Syed Jahanzaib
Filed under: Mikrotik Related
