Community discussions

MikroTik App
 
jcbhnz
just joined
Topic Author
Posts: 19
Joined: Tue Dec 14, 2021 1:32 pm

Modify? Script cuts the connection once a data limit is exceeded,

Thu Feb 02, 2023 7:38 am

With the script below, from https://pastebin.com/kVgugTrU, which cuts the connection once a data limit is exceeded, is it possible and if so would someone be kind enough, to change the script from working on interface "local interf "ether1-WAN1" to work on static local network address e.g. 192.168.88.45

Thanks
Jonnz

:global persistVar do={
:local varName $1;
:local varValue $2;
:local varID [/ip firewall layer7-protocol find name="$varName"];

:if ([:typeof $varValue] = "nothing") do={
:if ($varID != "") do={
:set $varValue [/ip firewall layer7-protocol get $varID value-name=regexp];
}
} else={
:if ($varID = "") do={
/ip firewall layer7-protocol add name="$varName" regexp="$varValue"
} else={
/ip firewall layer7-protocol set $varID regexp="$varValue"
}
}

return $varValue
}

:do {
####################################################################################################
# User config
#Set limit in MB
:local limit 350000;
#Set day to reset limit
:local dayresetlimit "01";
#Set interface
:local interf "ether1-WAN1";
#Set auto bring up interface
:local autointon true;
####################################################################################################
:local day ([:pick [/system clock get date] 4 6]);
:local newdata;
:local result;
:local ref;
:local status;
:local tx [/interface get $interf tx-byte];
:local rx [/interface get $interf rx-byte];

:set $newdata (($rx + $tx) / 1024 / 1024);

:if ([:len [$persistVar wan_counter]] < 1 ) do={
$persistVar wan_counter 0;
}

:if ([:len [$persistVar wan_rebootblock]] < 1 ) do={
$persistVar wan_rebootblock 1;
}

:if ([:len [$persistVar wan_offset]] < 1 ) do={
$persistVar wan_offset 0;
}

:if ([:len [$persistVar wan_refdata]] < 1 ) do={
$persistVar wan_refdata 0;
}

:local rebootblocker ([$persistVar wan_rebootblock]);

:if ( $day = $dayresetlimit and $rebootblocker = 0) do={
$persistVar wan_counter 0;
$persistVar wan_offset $newdata;
$persistVar wan_refdata 0;
/interface ethernet set $interf disabled=no
$persistVar wan_rebootblock 1;
:log info "Limit reset, int $interf bring up";
}

:if ( $day != $dayresetlimit ) do={
$persistVar wan_rebootblock 0;
}

:local before [$persistVar wan_counter];
:local offset [$persistVar wan_offset];

:if ( $newdata < $before ) do={
$persistVar wan_offset 0;
:set $ref [$persistVar wan_refdata];
:if ( $newdata < $ref ) do={
set $ref (0)
}
:set $result ($before + $newdata - $ref);
$persistVar wan_refdata $newdata;
$persistVar wan_counter $result;
} else={
:set $result ($newdata - $offset);
$persistVar wan_counter $result;
}

:local traffic ([$persistVar wan_counter]);
:local percent ($traffic*100 / $limit);
:log warning "Limit: $limit MB Traffic: $traffic MB Used: $percent%";

:if ($percent >= 100) do={
/interface ethernet set $interf disabled=yes
}

:if ($percent < 100 and $autointon = true) do={
:set $status [/interface get $interf value-name=disabled];
:if ($status = true) do={
/interface ethernet set $interf disabled=no
}
}
} on-error={ :log error "Error WAN1_Limiter "};
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Modify? Script cuts the connection once a data limit is exceeded,

Thu Feb 02, 2023 11:01 am

Contact direcly the author, is faster change own work than post script taken from somewhere and ask help on forum.
 
jcbhnz
just joined
Topic Author
Posts: 19
Joined: Tue Dec 14, 2021 1:32 pm

Re: Modify? Script cuts the connection once a data limit is exceeded,

Thu Feb 02, 2023 3:07 pm

Contact direcly the author, is faster change own work than post script taken from somewhere and ask help on forum.
I would if I could but no further clue to their identity seems available on Pastebin. They used the name "A GUEST" (Google has "About 2,000,000,000 results" for the name, so no luck there.

I would be content with just a few clues on how a change of format, to using an IP address would look, instead of the default ":local interf "ether1-WAN1";"

jcbhnz
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Modify? Script cuts the connection once a data limit is exceeded,

Thu Feb 02, 2023 3:13 pm

I don't want to do it to brag,
but it seems to me that you are missing the difference between simply seeing how much traffic a port has made (already available instantly)
and instead seeing how much traffic an IP has made (to be created from scratch, it doesn't exist of its own)...
 
jcbhnz
just joined
Topic Author
Posts: 19
Joined: Tue Dec 14, 2021 1:32 pm

Re: Modify? Script cuts the connection once a data limit is exceeded,

Thu Feb 02, 2023 3:38 pm

I don't want to do it to brag,
but it seems to me that you are missing the difference between simply seeing how much traffic a port has made (already available instantly)
and instead seeing how much traffic an IP has made (to be created from scratch, it doesn't exist of its own)...
So you state the obvious from my original post - practical assistance/directions would be of far more use.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Modify? Script cuts the connection once a data limit is exceeded,

Thu Feb 02, 2023 3:42 pm

Obvious? Ok...
You need to hire a consultant to teach you and write a script just for you.
(After an in-depth interview to actually understand what you want to achieve in the end,
without the consultant letting himself be influenced by what you think is necessary)
Or someone on the forum (not I) could do this JOB for you.

What you posted is not enough to make a little change here and there and make it work "with the IP"

Who is online

Users browsing this forum: No registered users and 9 guests