Syed Jahanzaib – Personal Blog to Share Knowledge !

May 22, 2015

Give your hardware some freedom of Speech ;)

Filed under: Linux Related, Mikrotik Related — Tags: — Syed Jahanzaib / Pinochio~:) @ 11:53 AM

userinf-2

 

MT-REPORT-SMS

playSMS_logo_full

kick


 CHANGE LOG:

  • 23-05-2015 – Added few functions in mt.sh to avoid SSH delay bug with mikrotik and to avoid false result
  • 08/06-2015 – Added individual dealer’s online users details according to VLAN, and user current online status with quota limits showing like data assigned, used and remained.

 

Recently I was working on a SMS reporting system to somehow receive some specific information for the Mikrotik health & Radius server information via sending an SMS and it should reply back the required information in some user friendly format. In the beginning it seemed fairly an easy task to accomplish by simply ssh (pass-wordless) to mikrotik and get the required info, BUT … executing the script from the terminal was working flawless, but executing this script from the playSMS was returning command_output EMPTY for ssh $variables. It took almost 15-16 hours wall banging efforts and finally I took another long route in order to achieve my task. You may find many other short routes to achieve this task, but I managed to get it working this way. Share your thoughts please.

TASK:

Send an sms with specific keyword to playSMS server, and it should then SSH to Mikrotik, and get some information (as described in the script) and send back that info (after making it user friendly) to the sender. so in short it’s a very good and handy way to get the server’s health report via sms Quickly 😉

Requirements:

This task is for people with some firm knowledge on Mikrotik and specially with Linux BASH scripting. But even if you are  a simple beginner, you can learn a lot from it. This is how you begin your journey by seeing far objects and then eventually reach to them by finding ways 🙂 and that’s the exact way I started my astronomy journey as well 😀

jupiter-first-modified-pic-24-3-2015

My First Picture of ‘Jupiter’ planet taken with 10″ manual Dobsonian with AFOCAL method, It’s an amazing experience when you see the planet from your Eyes Live !

 

 

1- Mikrotik with SSH/FTP services enabled (I changed the ports for security reasons)
2- Linux base System, I used my existing radius server for this purpose. It already have Kannel+playSMS configured with Dlink USB DWM-156 Modem. It can also ssh to mikrotik without password using per-configured public keys.

So you should read following guides before proceeding further.


 

Linux Section:

Here is the main script which will actually perform all the required actions and will gather information from mirkotik and radius itself.


#root@radius:/var/lib/playsms/sms_command/1# cat mt.sh

#!/bin/bash
# Script by Syed Jahanzaib / aacable@hotmail.com
# https://aacable.wordpress.com
# This script can inquire some specific Information from mikrotik and other server
# and send it back to sender via SMS on demand
# Version 2.0 -

MIKROTIK="192.168.1.2"
MTPORT="10001"
SERVER4="10.0.0.200"
SERVICE="radiusd"
DATE=`date`

# Check if Mikrotik is accessible or not, if not then EXIT immediately with error / zaib
if [[ $(ping -q -c 1 $MIKROTIK) == @(*100% packet loss*) ]]; then
echo "ALERT ..... MIKROTIK $MIKROTIK  is DOWN"
exit
else
echo "Galaxy Mikrotik Information SMS @ $DATE ..."
fi

# Execute Internetstatus script on mikrotik which will set the envrionment variable
ssh -q -p $MTPORT admin@$MIKROTIK /sys script run smsnetstatus > /tmp/pingresult

# Print PPP Active user in a file at Mikrotik
ssh -q -p $MTPORT admin@$MIKROTIK /ppp active print file=pppactive

# Print Internet Status Environment variables that we will use later
ssh admin@$MIKROTIK -p $MTPORT /sys script environment print file=netstatus

# Print Mikrotik Systems Resources in a file
ssh admin@$MIKROTIK -p $MTPORT /sys resource print file=resources

# Download pppactive file from mikrotik to local system in /tmp
scp -q -P $MTPORT admin@$MIKROTIK:pppactive.txt /tmp/pppactive.txt

# Verify if file is downloaded from mikrotik or not, if not dueo to ssh delay bug, then print error and exit 🙂 Security Check by zaib
{
if [ ! -f /tmp/pppactive.txt ]; then
echo -e "ERROR: Mikrotik is live but it's SSH not accessible. Its a Mikrotik level BUG, You MUST upgrade with latest version. zaib"
exit 0
fi
}

# Download pppactive file from mikrotik to local system in /tmp
scp -q -P $MTPORT admin@$MIKROTIK:netstatus.txt /tmp/netstatus.txt

# Verify if file is downloaded from mikrotik or not, if not dueo to ssh delay bug, then print error and exit 🙂 Security Check by zaib
{
if [ ! -f /tmp/netstatus.txt ]; then
echo -e "ERROR: Mikrotik is live but it's SSH not accessible. Its a Mikrotik level BUG, You MUST upgrade with latest version. zaib"
exit 0
fi
}

# Download Mikrotik System Resources file from mikrotik to local system in /tmp
scp -q -P $MTPORT admin@$MIKROTIK:resources.txt /tmp/resources.txt

# Verify if file is downloaded from mikrotik or not, if not dueo to ssh delay bug, then print error and exit 🙂 Security Check by zaib
{
if [ ! -f /tmp/resources.txt ]; then
echo -e "ERROR: Mikrotik is live but it's SSH not accessible. Its a Mikrotik level BUG, You MUST upgrade with latest version. zaib"
exit 0
fi
}

# Print/Extract Uptime & CPU load only
cat /tmp/resources.txt  | awk 'NR==4' | sed -e 's/^[ \t]*//'
cat /tmp/resources.txt  | awk 'NR==11' | sed -e 's/^[ \t]*//'

# Extract Netstaus value from the csript we ran earlier on mt.
NETSTATUSVALUE=`grep "smsInternetStatus" /tmp/netstatus.txt | awk '{print $4}'`
echo "NET STATUS = $NETSTATUSVALUE"

# Print/Extract PPP Active Number of users
ACTIVE=`cat /tmp/pppactive.txt | wc -l`
echo "Active PPP users = $ACTIVE"

# Check RADIUS or whatever service PID status
pid=`pidof $SERVICE`
if [ "$pid" == "" ]; then
echo -e "$SERVICE service is NOT running, trying to start it ..."
service $SERVICE start
else
echo "$SERVICE = OK"
fi

# Check status of sharing media server
if [[ $(ping -q -c 1 $SERVER4) == @(*100% packet loss*) ]]; then
echo "ALERT ..... SERVER4 $SERVER4 is DOWN"
exit
else
echo "SERVER4 = OK"
fi
echo "Powered by Syed.Jahanzaib ..."

# Delete Files on Mikrotik so that false reproting may not occur / zaib
ssh -q -p $MTPORT admin@$MIKROTIK /file remove pppactive.txt
ssh -q -p $MTPORT admin@$MIKROTIK /file remove netstatus.txt
ssh -q -p $MTPORT admin@$MIKROTIK /file remove resources.txt

# delete files download from the local tmp folder in linux
rm -fr /tmp/netstatus.txt
rm -fr /tmp/pppactive.txt
rm -fr /tmp/resources.txt

exit 0

You should save this file mt.sh (or whatever name you decide) in following location

/var/lib/playsms/sms_command/1

 


Mikrotik Section

At mikrotik you should create and script which will actually check the internet status and update environment variable which we will fetch and use later via cat/awk.

Mikrotik Script name: smsnetstatus


# Modified few contents to suite local requirements and added descriptions
# Regard's / Syed Jahanzaib / https://aacable.wordpress.com

# Script Starts here...
# Internet Host to be checked You can modify them as per required, JZ
# We are using DOUBLE hosts to avoid FALSE REPORTING : ) / zaib

:local host1   "8.8.8.8"
:local host2   "192.0.78.13"

# Do not modify data below without proper understanding.
:local i 0;
:local F 0;
:local date;
:local time;
:global smsInternetStatus;
:global smsInternetLastChange;

# PING each host 5 times
:for i from=1 to=5 do={
if ([/ping $host1 count=1]=0) do={:set F ($F + 1)}
if ([/ping $host2 count=1]=0) do={:set F ($F + 1)}
:delay 1;
};

# If both links are down and all replies are timedout, then link is considered down
:if (($F=10)) do={
:if (($smsInternetStatus="UP")) do={
:log error "smsRESULT:  - WARNING : The INTERNET link seems to be DOWN. Please Check";
:set smsInternetStatus "DOWN";

:set date [/system clock get date];
:set time [/system clock get time];
:set smsInternetLastChange ($time . " " . $date);
} else={:set smsInternetStatus "DOWN";}
} else={

##      If reply is received , then consider the Link is UP
:if (($smsInternetStatus="DOWN")) do={
:log warning "smsRESULT: WARNING :The INTERNET link have been restored";
:set smsInternetStatus "UP";

##      MAKE SURE TO CHANGE PPPO-OUT1 TO MATCH YOUR WAN INTERFACE
:local currentIP
:local externalInterface "pppoe-out1"

# get the current IP address from the external interface
:set currentIP [/ip address get [find interface="$externalInterface"] address]
# Strip netmask
:for i from=( [:len $currentIP] - 1) to=0 step=-1 do={
:if ( [:pick $currentIP $i] = "/") do={
:set currentIP [:pick $currentIP 0 $i]

:log warning "smsRESULT: PTCL LINK RE - CONNECTED with new WAN IP = $currentIP, Please check and confirm / zaib"

}
}

:set date [/system clock get date];
:set time [/system clock get time];
:set smsInternetLastChange ($time . " " . $date);
} else={:set smsInternetStatus "UP";}
}

# Script Ends Here.
# Thank you

Done!


Now you should create an SMS COMMAND in playSMS as showed below …

sms-command-fro-mt-report


 

TESTING …

Now test by sending an sms with word “mikrotik” to the playsms server mobile number. and you will shortly receive the reply 🙂 Allah Shuker

MT-REPORT-SMS


 

playSMS LOG

here is the playsms log when it will receive the SMS with word mikrotik

127.0.0.1 localhost 2015-05-22 11:40:59 PID555ecf7b2f24c - L2 kannel__call # start load:/var/www/playsms/plugin/gateway/kannel/geturl.php
127.0.0.1 localhost 2015-05-22 11:40:59 PID555ecf7b2f24c - L3 kannel__incoming # addr:127.0.0.1 host:localhost t:2015-05-22 06:42:03 q:+923333021909 a:Mikrotik Q:13013 smsc:[]
127.0.0.1 localhost 2015-05-22 11:40:59 PID555ecf7b2f24c - L3 recvsms # isrecvsmsd:1 dt:2015-05-22 06:42:03 sender:+923333021909 m:Mikrotik receiver:13013 smsc:
127.0.0.1 localhost 2015-05-22 11:40:59 PID555ecf7b2f24c - L2 kannel__call # end load geturl
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L3 recvsmsd # id:195 dt:2015-05-22 06:42:03 sender:+923333021909 m:Mikrotik receiver:13013 smsc:
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L3 setsmsincomingaction # dt:2015-05-22 06:42:03 sender:+923333021909 m:Mikrotik receiver:13013 smsc:
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L3 sms__command # command_exec:/var/lib/playsms/sms_command/1/mt.sh
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L3 sms__command # command_output:Galaxy Mikrotik Information SMS @ Fri May 22 11:41:00 PKT 2015 ... uptime: 17h54m55s cpu-load: 13% NET STATUS = UP Active PPP users = 295 radiusd = OK SERVER4 = OK Powered by Syed.Jahanzaib ...
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L2 sendsms # start uid:1 sender_id:[1234] smsc:[]
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L3 sendsms # maxlen:15140 footerlen:7 footer:[ @admin] msglen:193 message:[Galaxy Mikrotik Information SMS @ Fri May 22 11:41:00 PKT 2015 ... uptime: 17h54m55s cpu-load: 13% NET STATUS = UP Active PPP users = 295 radiusd = OK SERVER4 = OK Powered by Syed.Jahanzaib ...]
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L2 sendsms_queue_create # saving queue_code:60c45bad522ae25332bf3b37c33ea19f src:1234 scheduled:2015-05-22 11:41:00
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L2 sendsms_queue_create # saved queue_code:60c45bad522ae25332bf3b37c33ea19f id:163
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L3 sendsms_manipulate_prefix # before prefix manipulation:[+923333021909]
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L3 sendsms_manipulate_prefix # after prefix manipulation:[+923333021909]
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L3 simplerate_hook_rate_getbyprefix # rate not found to:+923333021909 default_rate:1
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L3 simplerate_hook_rate_getcharges # uid:1 u:admin len:200 unicode:0 to:+923333021909 enable_credit_unicode:0 count:2 rate:1 charge:2
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L2 sendsms # dst:1 sms_count:2 total_charges:2
- - 2015-05-22 11:41:00 PID555ebd2ae9681 admin L2 sendsms_queue_push # saving queue_code:60c45bad522ae25332bf3b37c33ea19f dst:+923333021909
- - 2015-05-22 11:41:01 PID555ebd2ae9681 admin L2 sendsms_queue_push # saved queue_code:60c45bad522ae25332bf3b37c33ea19f smslog_id:157
- - 2015-05-22 11:41:01 PID555ebd2ae9681 admin L2 sendsms # end queue_code:60c45bad522ae25332bf3b37c33ea19f queue_count:1 sms_count:2 failed_queue:0 failed_sms:0
- - 2015-05-22 11:41:01 PID555ebd2ae9681 admin L3 setsmsincomingaction # feature:sms_command datetime:2015-05-22 06:42:03 sender:+923333021909 receiver:13013 keyword:MIKROTIK message: raw:Mikrotik smsc:
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L2 sendsmsd # start processing queue_code:60c45bad522ae25332bf3b37c33ea19f sms_count:2 scheduled:2015-05-22 11:41:00 uid:1 gpid:0 sender_id:1234
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L2 sendsmsd # sending queue_code:60c45bad522ae25332bf3b37c33ea19f smslog_id:157 to:+923333021909 sms_count:2 counter:1
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L3 sendsms_intercept # msgtemplate modified sms_sender:[1234] sms_footer:[@admin] sms_to:[+923333021909] sms_msg:[Galaxy Mikrotik Information SMS @ Fri May 22 11:41:00 PKT 2015 ... uptime: 17h54m55s cpu-load: 13% NET STATUS = UP Active PPP users = 295 radiusd = OK SERVER4 = OK Powered by Syed.Jahanzaib ...] uid:[1] gpid:[0] sms_type:[text] unicode:[0] queue_code:[60c45bad522ae25332bf3b37c33ea19f] smsc:[]
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L3 outgoing_hook_sendsms_intercept # no SMSC found uid:1 parent_uid:0 from:1234 to:+923333021909
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L2 sendsms_process # start
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L3 simplerate_hook_rate_getbyprefix # rate not found to:+923333021909 default_rate:1
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L3 simplerate_hook_rate_getcharges # uid:1 u:admin len:200 unicode:0 to:+923333021909 enable_credit_unicode:0 count:2 rate:1 charge:2
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L2 simplerate__cansend # allowed user uid:1 sms_to:+923333021909 credit:55324.000 count:2 rate:1 charge:2 balance:55322
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L2 sendsms # saving smslog_id:157 u:1 g:0 gw:kannel smsc:kannel s:1234 d:+923333021909 type:text unicode:0 status:0
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L2 sendsms_process # saved smslog_id:157 id:157
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L3 sendsms # final smslog_id:157 gw:kannel smsc:kannel message:Galaxy Mikrotik Information SMS @ Fri May 22 11:41:00 PKT 2015 ... uptime: 17h54m55s cpu-load: 13% NET STATUS = UP Active PPP users = 295 radiusd = OK SERVER4 = OK Powered by Syed.Jahanzaib ... @admin len:200
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L3 kannel_hook_sendsms # enter smsc:kannel smslog_id:157 uid:1 to:+923333021909
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L3 kannel_hook_sendsms # URL: http://localhost:13013/cgi-bin/sendsms?username=kannel&password=KANNELPASSWORD&from=1234&to=%2B923333021909&dlr-mask=31&dlr-url=http%3A%2F%2Fclick.onmypc.net%3A1235%2Fplaysms%2Findex.php%3Fapp%3Dcall%26cat%3Dgateway%26plugin%3Dkannel%26access%3Ddlr%26type%3D%25d%26smslog_id%3D157%26uid%3D1&account=admin&text=Galaxy+Mikrotik+Information+SMS+%40+Fri+May+22+11%3A41%3A00+PKT+2015+...%0Auptime%3A+17h54m55s%0Acpu-load%3A+13%25%0ANET+STATUS+%3D+UP%0AActive+PPP+users+%3D+295%0Aradiusd+%3D+OK%0ASERVER4+%3D+OK%0APowered+by+Syed.Jahanzaib+...+%40admin
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L3 kannel__outgoing # smslog_id:157 response:0: Accepted for delivery
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L3 dlr # isdlrd:1 smslog_id:157 p_status:0 uid:1
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L3 kannel__outgoing # end smslog_id:157 p_status:0
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L2 simplerate__deduct # enter smslog_id:157
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L3 simplerate_hook_rate_getbyprefix # rate not found to:+923333021909 default_rate:1
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L3 simplerate_hook_rate_getcharges # uid:1 u:admin len:200 unicode:0 to:+923333021909 enable_credit_unicode:0 count:2 rate:1 charge:2
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L2 credit_hook_rate_setusercredit # saving uid:1 balance:55322
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L2 credit_hook_rate_setusercredit # saved uid:1 balance:55322
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L2 simplerate__deduct # user uid:1 parent_uid: smslog_id:157 msglen:200 count:2 rate:1 charge:2 credit:55324.000 balance:55322
- - 2015-05-22 11:41:01 PID555ecf7da2f5f - L2 simplebilling__post # saving smslog_id:157 rate:1 credit:55324.000 count:2 charge:2
- - 2015-05-22 11:41:02 PID555ecf7da2f5f - L2 simplebilling__post # saved smslog_id:157 id:156
- - 2015-05-22 11:41:02 PID555ecf7da2f5f - L3 simplerate__deduct # deduct successful uid:1 parent_uid: smslog_id:157
- - 2015-05-22 11:41:02 PID555ecf7da2f5f - L3 simplerate__deduct # credit_lowest_limit:0 balance:55322 charge:2
- - 2015-05-22 11:41:02 PID555ecf7da2f5f - L2 sendsms_process # end
- - 2015-05-22 11:41:02 PID555ecf7da2f5f - L2 sendsmsd # result queue_code:60c45bad522ae25332bf3b37c33ea19f to:+923333021909 flag:1 smslog_id:157
- - 2015-05-22 11:41:02 PID555ecf7da2f5f - L2 sendsmsd # finish processing queue_code:60c45bad522ae25332bf3b37c33ea19f uid:1 sender_id:1234 sms_count:2
- - 2015-05-22 11:41:02 PID555ebd2ae3e02 - L3 dlrd # id:157 smslog_id:157 p_status:0 uid:1


Another version of Mikrotik Info Script with Dealers online users details [in VLAN]

Another version which have some extended details like each dealer online users count which is helpful for dealers who can send sms to query there online users. script can be modified with security code or individual dealer by adding directives.


#!/bin/bash
MIKROTIK="192.168.1.1"
MTPORT="50001"
SERVER4="192.168.1.10" # Any other Server you may want to monitor in the report
SERVICE="radiusd" # Service you want to monitor like radius
DATE=`date`

# Check if Mikrotik is accessibel or not, if not then EXIT immediately with error / zaib
if [[ $(ping -q -c 1 $MIKROTIK) == @(*100% packet loss*) ]]; then
echo "ALERT ..... MIKROTIK $MIKROTIK  is DOWN"
exit
else
echo "Galaxy Mikrotik Information SMS @ $DATE ..."
fi

# Execute Internetstatus script on mikrotik which will set the envrionment variable
ssh -q -p $MTPORT admin@$MIKROTIK /sys script run smsnetstatus > /tmp/pingresult

# Print PPP Active user in a file at Mikrotik
ssh -q -p $MTPORT admin@$MIKROTIK /ppp active print file=pppactive

# Print Internet Status Environment variables that we will use later
ssh admin@$MIKROTIK -p $MTPORT /sys script environment print file=netstatus

# Print Mikrotik Systems Resources in a file
ssh admin@$MIKROTIK -p $MTPORT /sys resource print file=resources

# Download pppactive file from mikrotik to local system in /tmp
scp -q -P $MTPORT admin@$MIKROTIK:pppactive.txt /tmp/pppactive.txt

# Verify if file is downloaded from mikrotik or not, if not dueo to ssh delay bug, then print error and exit 🙂 Security Check by zaib
{
if [ ! -f /tmp/pppactive.txt ]; then
echo -e "ERROR: Mikrotik is live but it's SSH not accessible. Its a Mikrotik level BUG, You MUST upgrade with latest version. zaib"
exit 0
fi
}

# Download pppactive file from mikrotik to local system in /tmp
scp -q -P $MTPORT admin@$MIKROTIK:netstatus.txt /tmp/netstatus.txt

# Verify if file is downloaded from mikrotik or not, if not dueo to ssh delay bug, then print error and exit 🙂 Security Check by zaib
{
if [ ! -f /tmp/netstatus.txt ]; then
echo -e "ERROR: Mikrotik is live but it's SSH not accessible. Its a Mikrotik level BUG, You MUST upgrade with latest version. zaib"
exit 0
fi
}

# Download Mikrotik System Resources file from mikrotik to local system in /tmp
scp -q -P $MTPORT admin@$MIKROTIK:resources.txt /tmp/resources.txt

# Verify if file is downloaded from mikrotik or not, if not dueo to ssh delay bug, then print error and exit 🙂 Security Check by zaib
{
if [ ! -f /tmp/resources.txt ]; then
echo -e "ERROR: Mikrotik is live but it's SSH not accessible. Its a Mikrotik level BUG, You MUST upgrade with latest version. zaib"
exit 0
fi
}

# Print/Extract Uptime & CPU load only
cat /tmp/resources.txt  | awk 'NR==4' | sed -e 's/^[ \t]*//'
cat /tmp/resources.txt  | awk 'NR==11' | sed -e 's/^[ \t]*//'

# Extract Netstaus value from the csript we ran earlier on mt.
NETSTATUSVALUE=`grep "smsInternetStatus" /tmp/netstatus.txt | awk '{print $4}'`
echo "NET STATUS = $NETSTATUSVALUE"

# Extract PPP Active Number of users ACCORDING TO vlan
# These are pools assigned to pppoe users for different vlan's
VLAN9=`grep 10.0.9  /tmp/pppactive.txt | wc -l`
VLAN10=`grep 10.0.10  /tmp/pppactive.txt | wc -l`
VLAN13=`grep 10.0.13  /tmp/pppactive.txt | wc -l`
VLAN14=`grep 10.0.14  /tmp/pppactive.txt | wc -l`
VLAN15=`grep 10.0.15  /tmp/pppactive.txt | wc -l`
VLAN16=`grep 10.0.16  /tmp/pppactive.txt | wc -l`
VLAN17=`grep 10.0.17  /tmp/pppactive.txt | wc -l`
VLAN18=`grep 10.0.18  /tmp/pppactive.txt | wc -l`
VLAN19=`grep 10.0.19 /tmp/pppactive.txt | wc -l`

# Trim excess lines inlcuding license detgails and un-necessary stuff
TOTACTIVE=`cat /tmp/pppactive.txt |sed '1,5d' | wc -l`

# Print total and each vlan users according to above
echo "Total Active Users = $TOTACTIVE"
echo "Dealer = No. Of Online Users"
echo "DEALER-1 = $VLAN9"
echo "DEALER-2 = $VLAN10"
echo "DEALER-3 = $VLAN13"
echo "DEALER-4 = $VLAN14"
echo "DEALER-5 = $VLAN15"
echo "DEALER-6 = $VLAN16"
echo "DEALER-7 = $VLAN17"
echo "DEALER-8 = $VLAN18"
echo "DEALER-9 = $VLAN19"

# Check RADIUS or whatever service PID status
pid=`pidof $SERVICE`
if [ "$pid" == "" ]; then
echo -e "$SERVICE service is NOT running, trying to start it ..."
service $SERVICE start
else
echo "$SERVICE = OK"
fi

# Check status of sharing media server
if [[ $(ping -q -c 1 $SERVER4) == @(*100% packet loss*) ]]; then
echo "ALERT ..... SERVER4 $SERVER4 is DOWN"
exit
else
echo "SERVER4 = OK"
fi
echo "Powered by Syed.Jahanzaib ..."

# Delete Files on Mikrotik so that false reporting may not occur / zaib
ssh -q -p $MTPORT admin@$MIKROTIK /file remove pppactive.txt
ssh -q -p $MTPORT admin@$MIKROTIK /file remove netstatus.txt
ssh -q -p $MTPORT admin@$MIKROTIK /file remove resources.txt

# delete files download from the local tmp folder in linux
rm -fr /tmp/netstatus.txt
rm -fr /tmp/pppactive.txt
rm -fr /tmp/resources.txt

exit 0

Results of above script can be seen here


Galaxy Mikrotik Information SMS @ Mon Jun  8 12:55:49 PKT 2015 ...
uptime: 1d15h38m41s
cpu-load: 24%
NET STATUS = UP
Total Active Users = 414
Dealer = No. Of Online Users
DEALER-1 = 46
DEALER-2 = 42
DEALER-3 = 117
DEALER-4 = 6
DEALER-5 = 22
DEALER-6 = 17
DEALER-7 = 11
DEALER-8 = 53
DEALER-9 = 96
radiusd = OK
SERVER4 = OK
Powered by Syed.Jahanzaib ...

 


 

Regard’s
Syed Jahanzaib