Community discussions

MikroTik App
 
ludycro
just joined
Topic Author
Posts: 1
Joined: Wed Jan 10, 2024 12:37 pm

Creating a script for sending SMS

Wed Jan 10, 2024 1:08 pm

Greetings,
I just got my Chateau 5G and need help. Is there a good soul who could write me a script? I never used Mikrotik products and would greatly appreciate helping.
My ISP has a flat internet option, but I need to send an SMS to reactivate it after every 10GB of traffic.
Is there a way to send a SMS message after 9GB of traffic via a script?
I need to send a SMS containing a word "NASTAVI" (without quotes) to a phone number 13880 .
Can someone please write this script for me ?
I think the interface is lte1.
Thank you veeeery much in advance.
 
hafte
just joined
Posts: 11
Joined: Tue Jan 03, 2023 6:00 pm

Re: Creating a script for sending SMS

Sun Feb 25, 2024 8:08 am

I'm on the same both.
Can someone help us to mitigate this provider limitations of 10GB.

Somehow it is required to monitor data usage on the LTE interface and send sms when quota is reached.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12001
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Creating a script for sending SMS

Sun Feb 25, 2024 1:39 pm

Since SMS are free, send one sms each 12h... or you use more than 60GB every 12h?

https://telemach.hr/poslovni/mobilna-telefonija
Pravi je problem što 5G nije osmišljen kao rješenje za zamjenu optičkih vlakana,
to se tako prenosi, ali onda svaka firma na ovaj ili onaj način pokušava omesti one koji to iskorištavaju, kao što vas dvoje želite.
 
optio
Long time Member
Long time Member
Posts: 672
Joined: Mon Dec 26, 2022 2:57 pm

Re: Creating a script for sending SMS

Sun Feb 25, 2024 4:33 pm

@ludycro, @hafte You can use one of these two scripts in scheduler event (startup with some interval, eg. 5min).

Script with interface rx/tx bytes with persistence in interface comment
:local smsSendThresholdMiB 9216
:local smsNumber "13880"
:local smsMessage "NASTAVI"
:local wanIf "lte1"

/interface
:if ([get $wanIf disabled] || ![get $wanIf running]) do={ :return [:nothing] }
:local rx [get $wanIf rx-byte]
:local tx [get $wanIf tx-byte]
:if ($rx = 0 && $tx = 0) do={ :return [:nothing] }
reset-counters $wanIf
:local bts ($rx + $tx + [:tonum [get $wanIf comment]])

:if (($bts / 1048576) > $smsSendThresholdMiB) do={
    set $wanIf comment=0
    /tool/sms/send phone-number=$smsNumber message=$smsMessage
} else={
    set $wanIf comment=$bts
}

Script with interface rx/tx bytes with persistence in file
:local smsSendThresholdMiB 9216
:local smsNumber "13880"
:local smsMessage "NASTAVI"
:local wanIf "lte1"
:local bytesFile "flash/$wanIf_bytes.txt"

/interface
:if ([get $wanIf disabled] || ![get $wanIf running]) do={ :return [:nothing] }
:local rx [get $wanIf rx-byte]
:local tx [get $wanIf tx-byte]
:if ($rx = 0 && $tx = 0) do={ :return [:nothing] }
reset-counters $wanIf

:local lastBytes 0
/file
:if ([find name=$bytesFile] != "") do={
    :set lastBytes [:tonum [get $bytesFile contents]]
} else={
    add name=$bytesFile contents=0
}

:local bts ($rx + $tx + $lastBytes)

:if (($bts / 1048576) > $smsSendThresholdMiB) do={
    set $bytesFile contents=0
    /tool/sms/send phone-number=$smsNumber message=$smsMessage
} else={
    set $bytesFile contents=$bts
}
If you have USB disk connected to device you can change bytesFile to be in usb drive path instead flash.

P.S.
Script doesn't know how much traffic was before scheduler is initially created, so better reset interface counters (/interface/reset-counters lte1) and send manually sms to reset quota to be calibrated when scheduler is created. Generally to calibrate script if was already performed - just clear interface comment or delete bytes file (depends which script is used), reset interface counters and send sms manually assuming that provider will reset quota imidialty when sms is sent.
 
hafte
just joined
Posts: 11
Joined: Tue Jan 03, 2023 6:00 pm

Re: Creating a script for sending SMS

Mon Feb 26, 2024 8:30 am

@option thanks.
Implemented option 1 and seem working. Will monitor it in next days.
I have LTE RBLHGGR which does not support USB and using comment with bytes prevents reset if rooter is rebooted.

The other option is to send SMS every 4-6 hours not bothering for traffic :-)

Thanks all.
 
optio
Long time Member
Long time Member
Posts: 672
Joined: Mon Dec 26, 2022 2:57 pm

Re: Creating a script for sending SMS

Mon Feb 26, 2024 12:41 pm

Just be careful with that and don't and use short scheduler intervals since flash has limited sector writes, setting comment writes config on flash and also when using file on flash/* path. Using USB drive is alternative to avoid flash wear-out since USB drive is easier to replace when its wear-out (same reason why is for containers recommended to use USB drive).
 
hafte
just joined
Posts: 11
Joined: Tue Jan 03, 2023 6:00 pm

Re: Creating a script for sending SMS

Wed Feb 28, 2024 6:33 pm

Script basically works, but it is hard to sync and also after some time runs out of sync as ISP traffic counting is not the same as local.

It might be more useful when SMS for reached quota is received, script to send SMS to ISP

How to find particular word in SMS body and make do something?
For example if SMS contains "10 GB" + "1237" in message body is received to execute /tools/sms/send

My ISP is using Cyrillic for messages and most of the body is with ????. Not useful at all.

0 Vivacom-LTE 1237 2024-02-28 18:01:44+02:00 ??????? ????????? ???? 10 GB ?????? ???????????, ????? ?? ?????????
1 Vivacom-LTE 1237 2024-02-28 18:01:44+02:00 ? ?? ???????????? 10 GB ??????? ?? ???????? ?? ???? 1237.
 
optio
Long time Member
Long time Member
Posts: 672
Joined: Mon Dec 26, 2022 2:57 pm

Re: Creating a script for sending SMS

Wed Feb 28, 2024 6:49 pm

Are you sure that provider is not resetting quota after limit is reached and not immediately after SMS is sent which causes unsync? In such case you will need first to reach quota limit and then calibrate script, but it will still require small change in script to add diff from quota limit and threshold on bytes after reset.

Regarding failover to monitor SMS inbox and send SMS if such message is received it is possible to implement, but requires additional work on script to monitor inbox, currently I'm busiy to write this additional logic into script, in your case it is more complex because ROS is not handling non ascii encoding (UCS-2) from SMS PDU and to compare exact sms message text requires additional script to decode it properly, here is my script just for that to read SMS messages properly (it also concatenates messages with 160+ characters into single SMS message record which is also not handled by ROS SMS inbox) without using ROS tool /tool/sms/inbox:
sms-read.rsc
Edit:
Regarding proper SMS message text decoding, I think it is small chance that received SMS text will contain "10 GB" in body for other purpose so it will be possible just with regex to match such message without need for proper encoding using /tool/sms/inbox for SMS inbox monitoring in script implementation.
You do not have the required permissions to view the files attached to this post.
 
hafte
just joined
Posts: 11
Joined: Tue Jan 03, 2023 6:00 pm

Re: Creating a script for sending SMS

Wed Feb 28, 2024 8:57 pm

Hello @optio,

I appreciate and thanks for the script, but is too advanced for my knowledge.

I need something simple and straight forward. I made some progress what will help me and look simple, but i need some help getting things to work .

At least find is working and returns some positive result if sms contains 1237, which should be enough to have variable set to true.

:put [/tool sms inbox find where message ~"1237"]
*49;

This is what i stared to write as idea, but my knowledge is very basic.

###################################
:global smsMessageRequired
:if [/tool sms inbox find where message ~"1237"] do={
:set $smsMessageRequired=True
:if $smsMessageRequired=true do={
/tool/sms/send Vivacom-LTE phone-number=1237 type=class-1 status-report-request=no message=traffic
:set $smsMessageRequired=false
/tool sms inbox remove [ find where message ~"1237"];
}
:log warning ("Sent SMS 10GB Trafic Reset");
##################################
 
optio
Long time Member
Long time Member
Posts: 672
Joined: Mon Dec 26, 2022 2:57 pm

Re: Creating a script for sending SMS

Wed Feb 28, 2024 10:12 pm

If that's works for you ok, if WAN network is not blocked in between scheduler interval (provider gives some grace bandwidth or time until SMS is sent when quota is reached)... or you want to put this function in logic of an existing script?
If you want better message cleanup match with "10 GB" because it seems that this is single split message and it will delete both records, unless you can receive other messages with "10 GB" in text.
For sms send command, if you want reduced script size, status-report-request=no (it is not even documented) and type=class-1 are not needed because these are default values afaik.
 
hafte
just joined
Posts: 11
Joined: Tue Jan 03, 2023 6:00 pm

Re: Creating a script for sending SMS

Thu Feb 29, 2024 10:16 am

WAN network is not blocked , but limited to 64kb, where mainly ping is working.

I do not have script, but the above example is my starting point.
I need somehow to refine it and make it working, but with my knolage it will be true or false fight :-)
 
optio
Long time Member
Long time Member
Posts: 672
Joined: Mon Dec 26, 2022 2:57 pm

Re: Creating a script for sending SMS

Thu Feb 29, 2024 3:55 pm

I meant if you will using this SMS check along with logic from my past script which you tried and which goes out of sync for some reason... But if you want just to check SMS inbox, from first look your script maybe needs some corrections but something like that can be put in scheduler event. Maybe over weekend I will have time to review it, you can try meanwhile to check by yourself if is working and correct errors by studying scripting manual and my suggestions from previous post.
 
hafte
just joined
Posts: 11
Joined: Tue Jan 03, 2023 6:00 pm

Re: Creating a script for sending SMS

Sat Mar 02, 2024 12:37 pm

With some True or False, below seems to be working:
:global smsMessageRequired
:if [/tool sms inbox find where message ~"1237"] do={
:set $smsMessageRequired value=true} else={
:set $smsMessageRequired value=false}
:if ($smsMessageRequired=false) do={:return [:nothing]}
:if ($smsMessageRequired=true) do={
/tool/sms/send Vivacom-LTE phone-number=1237 status-report-request=no message=traffic
:set $smsMessageRequired value=false
:log info "Send SMS for Traffic reset"
/tool sms inbox remove [find];
}
The only issue is that, not all sms are removed from the inbox, to be ready for next cycle.
It gives error: Vivacom-LTE, removing SMS, AT prefered (#=7) failed: +CMS ERROR: unknown error (6) and remove only one of the SMS

Probably this is as the SMS are on cyrilic, and are splited in three separate SMS.

But how to force delete them. Shuld i set removal command to be executed for example 10 times?
/tool sms inbox remove [find];
/tool sms inbox remove [find];
/tool sms inbox remove [find];
/tool sms inbox remove [find];
/tool sms inbox remove [find];
 
optio
Long time Member
Long time Member
Posts: 672
Joined: Mon Dec 26, 2022 2:57 pm

Re: Creating a script for sending SMS

Sat Mar 02, 2024 3:07 pm

Can't tell where is the issue regarding removing SMS, try to upgrade ROS to 7.14, there is some fix in 7.14 regarding SMS removal:
*) modem - fixed SMS removal (introduced in v7.13);
here is script that retries removing SMS in recursion until no error with max retry count 10, controlled by maxRetry variable:
:local removeAllSMS do={
  :do {
    /tool/sms/inbox/remove [find]
  } on-error={
    :if ($retryCnt < $maxRetry) do={
      $removeAllSMS removeAllSMS=$removeAllSMS retryCnt=($retryCnt + 1) maxRetry=$maxRetry
    }
  }
}

:global sendingSMS
:if (!$sendingSMS && [/tool sms inbox find where message ~"1237"]) do={
  :set sendingSMS true
  :do {
    /tool/sms/send Vivacom-LTE phone-number="1237" status-report-request=no message=traffic
  } on-error={
    :set sendingSMS false
    :local errMsg "Send SMS for Traffic reset failed"
    :log error $errMsg
    :error $errMsg
  }
  :log info "Send SMS for Traffic reset"
  $removeAllSMS removeAllSMS=$removeAllSMS retryCnt=1 maxRetry=10
  :set sendingSMS false
}
 
hafte
just joined
Posts: 11
Joined: Tue Jan 03, 2023 6:00 pm

Re: Creating a script for sending SMS

Sat Mar 02, 2024 6:08 pm

Thank you @optio,

I'm good to go. All is set and will monitor the behaviour in next days.

Thanks again for all the hints and help.
 
Dezoteck55
just joined
Posts: 10
Joined: Mon Mar 04, 2024 9:00 pm

Re: Creating a script for sending SMS

Fri Apr 12, 2024 5:36 am

Hello

After quite a rough beginnings, broken webfig, unresponsive modem, finally started to get a hang of this. I'm also totally new to Mikrotik, and my first experience with RouterOS was with ATL LTE18 Kit.

Long story short, turns out that the updated version of RouterOS comes with a pretty decent default config that produces a working state after just entering the SIM PIN code. I've since added several more customizations.

Now, I did my homework good, kept reading the online manuals, hugely dug into the whole LTE mobile technology topic, reading all about how cell towers work, cellmapper.net, bands, etc, etc.
Then the Remote Management wouldn't work, some folks familiar explained it's actually due to CGNAT and the ISP had to be involved, and that there's a low chance the ISP would provide an exception, disabling or forwarding.
The ISP first offered to enable static IP, which I didn't thought of as I didn't knew it was a thing for mobile, I'm also used to static IPs at home, it was all another long story there with the Carrier-Grade NAT and the negotiation back and forth, but it turned out that the way this ISP/Operator has things set up, that it just disables CGNAT completely when Static IP is enabled, so the whole issue was resolved quickly. Also CGNAT isn't well known within the ISP either, nor the fact that it just gets disabled with Static IP.


I came back here to figure out what I can do regarding one major setback, a requirement to send SMS messages to the ISP to confirm continuing the internet session, or rather to say to reset the quota counters.

The operator Telemach in Slovenia has a 30GB limit daily, after which the speed is reduced to minimal 64/64kbps, but the counters will reset after 24 hours or a reconnect of the session automatically.
For the users, turning off and on the antenna ungracefully is probably a bad idea, they would have to call me everytime something could go wrong with the ATL.
And the users having to login to the management and fiddle with the administration to send SMS via WinBox or SSH is also ... last resort I guess.

What I had in mind, now that I'm getting familiar with the SMS, is the ability to trigger SMS send to the ISP from the ATL, by sending a SMS to the ATL from my phone with the run script feature that requires the "Secret", I think that would make for a much more simpler script, it's not full auto ofcourse, but that would be pretty easy to do for the users if I give them the instructions to send a SMS from their phone.

Auto daily reset is not a guarantee apparently the ISP said, and with all the testing and daily driving I was getting over the limit 3 times in these couple of weeks without knowing.
There's probably a way in RouterOS to set some kind of a warning when SMS is received, right? I bet there's a logging rule I could have set up.
Later, when I learn more, a possibility of a script to trigger a SMS send to one of my phones, after any kind of SMS is received to the modem from a particular number such as the ISP.

Also, it's good that SMS from the ISP is still received even tho "Receive" isn't setup with the default configuration and I can see no SMS of my test sends from my phone.

In the meantime I need to finish the initial setup so the antenna can get installed, 30GB limit probably won't be an issue for basic operations, but there is a plan for TV and Security System stuff and that's going to all add up, no doubt it'll be an issue.

I'll report back later, maybe I'll manage to figure out a script myself, but this is the last thing I'll tackle.

PS: The SMS Inbox timestamps isn't verbose enough, so it displays messages that were sent later as if they arrived earlier. The ISP sends long messages about the quotas, which are split in 2 or 3, and they all have the same exact timestamps. Possibly the timestamps aren't any more precise in the whole cell network and SMS tech/standard, in which case nothing Mikrotik can do?

Who is online

Users browsing this forum: No registered users and 13 guests