Syed Jahanzaib Personal Blog to Share Knowledge !

October 8, 2015

[For Reference] Quick Script for Mikrotik Daily Info via SMS or Email !

Filed under: Mikrotik Related — Tags: , , , , — Syed Jahanzaib / Pinochio~:) @ 1:37 PM

keep calm n coding

mikrotik status updated script via sms

Quick Note:

Scenario#1:

DUAL DSL wan links are connected with mikrotik [modems are in bridge mode], Configured as PCC along with the pppoe server. USB modem is attached with the Mikrotik via USB port. The OP need an script which can daily send sms or email specific information to admin cell, or whenever admin require on urgent basis via sending sms to mikrotik and it can return back the info to the admin cell.

 

Dirty Solution:

Following script will collect information from Mikrotik system such as active pppoe users, CPU load, dsl links status etc and send this information to admin via SMS or Email either via scheduler or as receiver command on mikrotik. [I am already using much more advance version of this script in other networks in linux system as BASH is the best ground to support scripting but since linux was not available at this spot, i had to use Mikrotik only with usb modem attached]  it can be set as receive command too so that admin can send sms to mikrotik and mikrotik will reply back the current info to the admin mobile. Sometimes its very useful for remote admins.

# SENDING SMS TO ADMINS FOR DAILY MORNING ALERT 
# Script Designed by SYED JAHANZAIB
# aacable at hotmail dot com / https://aacable.wordpress.com
# Morning Hours / 8th Oct, 2015

# Setting various variables / jz
:local date;
:local time;
:local PPP ([/ppp active print count-only])
:local UPTIME [/system resource get uptime]
:local CPU [/system resource get cpu-load]
:set date [/system clock get date];
:set time [/system clock get time];
:global FTPIP 192.168.0.50

# Following variables are configured via other scripts which scheduled to run after very 1 minutes to update internet/media sharing server status and set these variables 
# But you can run your own script here to make variable after successful or failed ping results
# Ref: https://aacable.wordpress.com/2014/06/12/mikrotik-wan-monitoring-script-with-multiple-host-check/

:local DSL1 [/system script environment get [/system script environment find name="DSL1netstatus"] value];
:local DSL2 [/system script environment get [/system script environment find name="DSL2netstatus"] value];

# Local FTP Sharing Server or any other remote host, should be set via netwatch or local script inside this script
#:local FTP [/tool netwatch get number=0 status]
# Using one liner code to get FTP ping status and store it in variable, you can use same for above dsl status as well by
# forcing routes via specific gateway
:global FTP;
:if ([/ping $FTPIP count=1] = 0) do {:put ":set FTP value=DOWN"} else={:set FTP value=UP}

# Admin SMS Number Config
:local cell1 "03333021909"

# Gmail Config
:global SYSID ([/system identity get name])
:global adminmail1 aacable@hotmail.com
:global gmailid GMAILID@gmail.com
:global gmailpwd GMAILPASSWORD
:global gmailip
:set gmailip [:resolve "smtp.gmail.com"];

# Print LOG
:log warning "INFO: Daily info for NETWORK @ $date $time\nActive PPPOE Users = $PPP\nUptime is $UPTIME\nCPU Load = $CPU\nDSL1 = $DSL1\nDSL2 = $DSL2\nFTP = $FTP\n\nPowered by J."
:log warning "Sending DAILY MORNING ALERT SMS on $cell1 ... by J."

# Sending SMS
/tool sms send port=usb3 phone-number=$cell1 message="INFO: Daily info for NETWORK @ $date $time\nActive PPPOE Users = $PPP\nUptime = $UPTIME\nCPU Load = $CPU\nDSL1 = $DSL1\nDSL2 = $DSL2\nFTP = $FTP\n\nPowered by J." channel=0

# Sending EMAIL - Use it if if required
#/tool e-mail send to=$adminmail1 password=$gmailpwd subject="INFO: Daily info for @ $date $time" body="Active PPPOE Users = $PPP\nUptime = $UPTIME\nCPU Load = $CPU\nDSL1 = $DSL1\nDSL2 = $DSL2\nFTP = $FTP\n\nPowered by J." from=$gmailid server=$gmailip start-tls=yes

 

Sample Image of SMS:

debug

sms


 

Enable Mikrotik to Receive SMS and run script

Use following command to enable SMS receiving on mikrotik.

#:delay 60
/tool sms set keep-max-sms=10 port=usb3 receive-enabled=yes secret=12345 channel 0
#:delay 3
#/tool sms set receive-enabled=no
#:delay 3
#/tool sms set receive-enabled=yes

From your mobile you can send following message to run script on remote mikrotik.

:cmd 12345 script test

 

Explanation:

  • :cmd to let mikrotik know that its command
  • 12345 is secret which you configured in /tool > sms,kind of password / authentication
  • script to let mikrotik know that its script related command  and
  • test is script name you want to run.

More references for mikrotik base sms are available at https://aacable.wordpress.com/tag/mikrotik-sms/

 


Another Beta Version:


# SENDING SMS TO ADMINS FOR DAILY MORNING ALERT ALERT

# Syed Jahanzaib

# Remove 'not required' items

# Your Network Name, change it here
:local NETWORK "ZzZzZz"

# Set DATE TIME
:local date;
:local time;
:set date [/system clock get date];
:set time [/system clock get time];

# Gather Data from Routerboard
:local PPP  ([/ppp active print count-only])
:local UPTIME [/system resource get uptime]
:local CPU [/system resource get cpu-load]
:local VOLT [/sys health get voltage]
:local TEMPR [/sys health get temperature]
:local CPUTEMP [/sys health get cpu-temperature]
:local PWRC [/sys health get power-consumption]
:local FAN1 [/sys health get fan1-speed]
:local FAN2 [/sys health get fan2-speed]
:local DSL1 [/system script environment get [/system script environment find name="DSL1netstatus"] value];
:local DSL2 [/system script environment get [/system script environment find name="DSL2netstatus"] value];

# Get Local Media sharing server status
:global FTP;
:if ([/ping 10.00.100 count=1] = 0) do {:put ":set FTP value=DOWN"} else={:set FTP value=UP}

# Modem Related
:local PORT usb3
:local CHANNEL 0

# Admin Mobile Number
:local cell1 "03333021909"

# Message to be send , which gather data from variables / zaib
:local MSG "INFO: Daily info for
$NETWORK  at $date $time
Active PPPOE Users  = $PPP
Uptime = $UPTIME
DSL1 = $DSL1
DSL2 = $DSL2
CPU Load = $CPU
FTP = $FTP
Voltage
$VOLT
CPU Temperature
$CPUTEMP
Power Consumption
$PWRC
Board Temperature
$TEMPR
FAN1 Speed
$FAN1
FAN2 Speed
$FAN2
Powered by J."

# Finally Send SMS with all gathered DATA for CCR_1036
/tool sms send port=$PORT channel=0 phone-number=$cell1 message="$MSG"

 

Regard’s
Syed Jahanzaib!

Advertisements

13 Comments »

  1. Hi syed, do we still have to subscribe?or can microtik actually send sms right away?

    Im thinking of integrating it with our NOC and get sms for critical alerts.

    Regards, Mon

    >

    Like

    Comment by Mon-Loi Perez — October 8, 2015 @ 1:45 PM

    • It will work right away. either via scheduler / netwatch / or admin send sms to mikrotik to execute any script. whatever action is required it will fulfill.

      Like

      Comment by Syed Jahanzaib / Pinochio~:) — October 8, 2015 @ 1:49 PM

  2. Hello,           I am one of those self studying people about Router OS and using mikrotik products. Is there an explanation why a script before was working fine in a previous version and in an updated version is not working anymore? Hope you can help me point it out or at least a guide thanks.   | Rolly B. Bacanto Jr. | | | |

    Like

    Comment by Rolly B. Bacanto Jr. — October 8, 2015 @ 8:03 PM

    • Dueto upgrade, sometimes syntax gets changed for better compatibility with the new system and added features. so sometimes a script which runs in 5.x may not run with 6.x version, as ver 6.x was re-written with lots of improvements and features.
      so use latest version [stable release] possible for your R&D.

      Like

      Comment by Syed Jahanzaib / Pinochio~:) — October 9, 2015 @ 11:58 AM

  3. It would be help full 🙂 very nice

    Like

    Comment by UmmarHussain — October 9, 2015 @ 1:34 AM

  4. Jahanzaib bahi thx for such a useful script Please add one this in this script current bandwidth / traffic of out going interface / Ethernet like 85 Mbps

    Like

    Comment by umer — October 12, 2015 @ 10:43 AM

  5. But sir please tell variable for hotspot active users like $PPP for PPPoE users

    Like

    Comment by UmmarHussain — October 12, 2015 @ 4:56 PM

  6. I think It is more likely to be /ip hotspot active print count-only But not sure.

    Like

    Comment by UmmarHussain — October 12, 2015 @ 4:57 PM

  7. as per TRAI rule we need http url logger server for mikrotik and radius manager……..Please sir help

    Like

    Comment by jatin — December 2, 2015 @ 9:41 AM

  8. hi syed !!! thanks for your posts it helps a lot …. i want a script for mikrotik to send sms based on recieved sms . for example if it recieves massage contains money (0.5,1$)and number to send mssge a user and password to number in the massage … ???

    Like

    Comment by mohamed ahmed ali — May 20, 2017 @ 3:25 PM

    • for receiving you need to setup PLAYSMS with incoming config as well using kannel.
      I wrote about it in details.
      search google for ‘aacable fun with playsms’

      Like

      Comment by Syed Jahanzaib / Pinochio~:) — May 26, 2017 @ 12:18 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

%d bloggers like this: