Community discussions

MikroTik App
 
User avatar
Sinan
Trainer
Trainer
Topic Author
Posts: 129
Joined: Tue Sep 01, 2015 7:00 am

Scripting Ideas ??

Thu Mar 10, 2016 6:13 am

Hi everybody
I want to measure my abilities with mikrotik scripting language (only mikrotik scripting no API)
so if anyone has an idea for script i ll try to solve it for him
 
jarda
Forum Guru
Forum Guru
Posts: 7756
Joined: Mon Oct 22, 2012 4:46 pm

Thu Mar 10, 2016 6:37 am

Very nice offer. Maybe twice a day someone requires a script here on the forum. You can just post an answer to those requests...
 
csalcedo
Frequent Visitor
Frequent Visitor
Posts: 80
Joined: Fri Jan 22, 2016 8:09 pm
Location: Santiago Chile

Re: Scripting Ideas ??

Thu Mar 10, 2016 10:12 pm

Here goes one...

I have 2 3g/4g Ethernet routers that i am using as Wan1 and Wan2 links.
What I am trying to do is the following:

all traffic should go out Wan1 until I hit my data limit. The Telco gives me full speed of 4-20G until I hit my limit then throttles me to 32K. So the switch mechanism could be use Wan1 until the speed drops (I think very difficult but does not rely on data counters) or check data counters then disable Wan1 and use Wan2 (default backup routes i think).
Reset data counters on interfaces on specific day of the month.

I would appreciate any help, hints ETC...


Thanks
 
User avatar
Sinan
Trainer
Trainer
Topic Author
Posts: 129
Joined: Tue Sep 01, 2015 7:00 am

Re: Scripting Ideas ??

Fri Mar 11, 2016 5:54 am

Here goes one...

I have 2 3g/4g Ethernet routers that i am using as Wan1 and Wan2 links.
What I am trying to do is the following:

all traffic should go out Wan1 until I hit my data limit. The Telco gives me full speed of 4-20G until I hit my limit then throttles me to 32K. So the switch mechanism could be use Wan1 until the speed drops (I think very difficult but does not rely on data counters) or check data counters then disable Wan1 and use Wan2 (default backup routes i think).
Reset data counters on interfaces on specific day of the month.

I would appreciate any help, hints ETC...


Thanks
what you want is exactly here , (the traffic monitor tool) :
http://wiki.mikrotik.com/wiki/Manual:To ... ic_Monitor
for resetting-counters , you need to put this in a scheduler that is repeated monthly ...
/interface ethernet reset-counters "name of your interface, example:ether1"
sometimes this not , so you need to put a scheduler to reboot the RB each month
/system reboot
 
Van9018
Long time Member
Long time Member
Posts: 558
Joined: Mon Jun 16, 2014 6:26 pm
Location: Canada - Abbotsford

Re: Scripting Ideas ??

Fri Mar 11, 2016 9:44 am

A script to maintain a list of static DNS entries based on host names found in DHCP leases. Allow the Mikrotik admin to specify the suffix, so a hostname of PC100 gets set as PC100.mycompany.local in the static DNS.
 
User avatar
Sinan
Trainer
Trainer
Topic Author
Posts: 129
Joined: Tue Sep 01, 2015 7:00 am

Re: Scripting Ideas ??

Sat Mar 12, 2016 6:22 am

A script to maintain a list of static DNS entries based on host names found in DHCP leases. Allow the Mikrotik admin to specify the suffix, so a hostname of PC100 gets set as PC100.mycompany.local in the static DNS.
I dont get the full idea, but i think this is what you are looking for : (just copy and past in the new terminal)
/system script add name=Host_Name_DNS owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive source="#By Sin\
an AlChalabi\r\
\n#12-3-2016\r\
\n#Static DNS enrty for DHCP leases \r\
\n#Edit the suffix below\r\
\n:local suffix \".mycompany.local\"\r\
\n:foreach i in [/ip dhcp-server lease find] do={\r\
\n:local hname [/ip dhcp-server lease get value-name=host-name \$i]\r\
\n:local hadd [ip dhcp-server lease get value-name=address \$i]\r\
\n:local dnsname (\"\$hname\".\"\$suffix\")\r\
\n/ip dns static add name=\$dnsname address=\$hadd ttl=1d\r\
\n}"
 
csalcedo
Frequent Visitor
Frequent Visitor
Posts: 80
Joined: Fri Jan 22, 2016 8:09 pm
Location: Santiago Chile

Re: Scripting Ideas ??

Mon Mar 14, 2016 10:16 pm

Hi Sinan, I looked at traffic monitor but I get the impresion that its for bandwith not for data passed through the interface.. Am I wrong????


Here goes one...

I have 2 3g/4g Ethernet routers that i am using as Wan1 and Wan2 links.
What I am trying to do is the following:

all traffic should go out Wan1 until I hit my data limit. The Telco gives me full speed of 4-20G until I hit my limit then throttles me to 32K. So the switch mechanism could be use Wan1 until the speed drops (I think very difficult but does not rely on data counters) or check data counters then disable Wan1 and use Wan2 (default backup routes i think).
Reset data counters on interfaces on specific day of the month.

I would appreciate any help, hints ETC...


Thanks
what you want is exactly here , (the traffic monitor tool) :
http://wiki.mikrotik.com/wiki/Manual:To ... ic_Monitor
for resetting-counters , you need to put this in a scheduler that is repeated monthly ...
/interface ethernet reset-counters "name of your interface, example:ether1"
sometimes this not , so you need to put a scheduler to reboot the RB each month
/system reboot
 
User avatar
Sinan
Trainer
Trainer
Topic Author
Posts: 129
Joined: Tue Sep 01, 2015 7:00 am

Re: Scripting Ideas ??

Tue Mar 15, 2016 5:55 am

it is for the rate of your data , so you can put the rate 32kbps if it is below it , then disable this interface .....
Hi Sinan, I looked at traffic monitor but I get the impresion that its for bandwith not for data passed through the interface.. Am I wrong????


Here goes one...

I have 2 3g/4g Ethernet routers that i am using as Wan1 and Wan2 links.
What I am trying to do is the following:

all traffic should go out Wan1 until I hit my data limit. The Telco gives me full speed of 4-20G until I hit my limit then throttles me to 32K. So the switch mechanism could be use Wan1 until the speed drops (I think very difficult but does not rely on data counters) or check data counters then disable Wan1 and use Wan2 (default backup routes i think).
Reset data counters on interfaces on specific day of the month.

I would appreciate any help, hints ETC...


Thanks
what you want is exactly here , (the traffic monitor tool) :
http://wiki.mikrotik.com/wiki/Manual:To ... ic_Monitor
for resetting-counters , you need to put this in a scheduler that is repeated monthly ...
/interface ethernet reset-counters "name of your interface, example:ether1"
sometimes this not , so you need to put a scheduler to reboot the RB each month
/system reboot
 
DMA
just joined
Posts: 4
Joined: Thu Mar 24, 2016 1:34 pm

Re: Scripting Ideas ??

Mon Mar 28, 2016 5:17 pm

Hey Sinan,
Here's one for you I've been struggling with. I'm wanting to loop through all wifi channels in the 2.4GHz with a set delay between the channel changes.

For example: Frequency 2412, wait 30 seconds, change to 2417, wait 30 seconds, change to 2422, wait 30 seconds, etc... and have it continually loop until powered off.

Help with this would be greatly appreciated. Thank you.

David
 
Van9018
Long time Member
Long time Member
Posts: 558
Joined: Mon Jun 16, 2014 6:26 pm
Location: Canada - Abbotsford

Re: Scripting Ideas ??

Thu Mar 31, 2016 8:40 am

Hey Sinan,
Here's one for you I've been struggling with. I'm wanting to loop through all wifi channels in the 2.4GHz with a set delay between the channel changes.

For example: Frequency 2412, wait 30 seconds, change to 2417, wait 30 seconds, change to 2422, wait 30 seconds, etc... and have it continually loop until powered off.

Help with this would be greatly appreciated. Thank you.

David
See below!

I'm curious though, what's the purpose of changing the frequency so frequently?

{
do {

:local WirelessLanName "wlan1";

:local wlan [/interface wireless get [find name=$WirelessLanName]];

:local freq ($wlan->"frequency");

:if ($freq = 2462) do={set $freq 2412} else={set $freq ($freq + 5)};

:set ($wlan->"frequency") $freq;

/interface wireless set [find id=($wlan->"id")] frequency=$freq

delay 30s;

} while=(0=0);

}
 
User avatar
Sinan
Trainer
Trainer
Topic Author
Posts: 129
Joined: Tue Sep 01, 2015 7:00 am

Re: Scripting Ideas ??

Fri Apr 01, 2016 6:56 am

Hey Sinan,
Here's one for you I've been struggling with. I'm wanting to loop through all wifi channels in the 2.4GHz with a set delay between the channel changes.

For example: Frequency 2412, wait 30 seconds, change to 2417, wait 30 seconds, change to 2422, wait 30 seconds, etc... and have it continually loop until powered off.

Help with this would be greatly appreciated. Thank you.

David
See below!

I'm curious though, what's the purpose of changing the frequency so frequently?

{
do {

:local WirelessLanName "wlan1";

:local wlan [/interface wireless get [find name=$WirelessLanName]];

:local freq ($wlan->"frequency");

:if ($freq = 2462) do={set $freq 2412} else={set $freq ($freq + 5)};

:set ($wlan->"frequency") $freq;

/interface wireless set [find id=($wlan->"id")] frequency=$freq

delay 30s;

} while=(0=0);

}
thanx for answering instead of me.
if wanted to change the interval i suggest to put it in scheduler with the desired interval instead of using the delay function that will be easier to edit
 
DMA
just joined
Posts: 4
Joined: Thu Mar 24, 2016 1:34 pm

Re: Scripting Ideas ??

Wed Apr 06, 2016 6:47 pm

Hey Sinan,
Here's one for you I've been struggling with. I'm wanting to loop through all wifi channels in the 2.4GHz with a set delay between the channel changes.

For example: Frequency 2412, wait 30 seconds, change to 2417, wait 30 seconds, change to 2422, wait 30 seconds, etc... and have it continually loop until powered off.

Help with this would be greatly appreciated. Thank you.

David
See below!

I'm curious though, what's the purpose of changing the frequency so frequently?

{
do {

:local WirelessLanName "wlan1";

:local wlan [/interface wireless get [find name=$WirelessLanName]];

:local freq ($wlan->"frequency");

:if ($freq = 2462) do={set $freq 2412} else={set $freq ($freq + 5)};

:set ($wlan->"frequency") $freq;

/interface wireless set [find id=($wlan->"id")] frequency=$freq

delay 30s;

} while=(0=0);

}
Looks like it will work, thank you.
I just wanted something automated to hop through the channels for signal strengths at each channel. Primarily to see if there were any significant advantages for one channel versus another other than just looking at an app of my phone. Thanks again.
 
irghost
Member
Member
Posts: 308
Joined: Sun Feb 21, 2016 1:49 pm

Re: Scripting Ideas ??

Fri Apr 08, 2016 1:10 pm

hi i need script for ( hotspot >user profile > script > on login & on logout)
that add nat masquerade (ip firewall nat add action=masquerade chain=srcnat src-address=userIP)
and remote it after user logout
i use 2 ip pool for hotspot first dhcp assign ip to user when he connect to wlan , second hotspot assign ip to logged in users form another ip pool
i need nat for second ip pool
 
User avatar
amt
Long time Member
Long time Member
Posts: 529
Joined: Fri Jan 16, 2015 2:05 pm

Re: Scripting Ideas ??

Thu Jun 30, 2016 1:11 pm

Hi everybody
I want to measure my abilities with mikrotik scripting language (only mikrotik scripting no API)
so if anyone has an idea for script i ll try to solve it for him

Hi Sinan,

can you help me for some scripting ?
Last edited by amt on Thu Jun 30, 2016 4:32 pm, edited 1 time in total.
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 3016
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: Scripting Ideas ??

Thu Jun 30, 2016 1:20 pm

To forum jest w języku angielskim zatem proszę go używać :-)

This forum is in English language so please use it. :-)
 
User avatar
amt
Long time Member
Long time Member
Posts: 529
Joined: Fri Jan 16, 2015 2:05 pm

Re: Scripting Ideas ??

Thu Jun 30, 2016 4:33 pm

To forum jest w języku angielskim zatem proszę go używać :-)

This forum is in English language so please use it. :-)

Sory , I changed it
 
User avatar
Deantwo
Member
Member
Posts: 332
Joined: Tue Sep 30, 2014 4:07 pm

Re: Scripting Ideas ??

Thu Jun 30, 2016 4:48 pm

can you help me for some scripting ?
What are you looking for?
 
User avatar
amt
Long time Member
Long time Member
Posts: 529
Joined: Fri Jan 16, 2015 2:05 pm

Re: Scripting Ideas ??

Fri Jul 01, 2016 10:55 am

can you help me for some scripting ?
What are you looking for?
some script will take ddosed ip's from /ip firewall address-list and will add this to black hole like this /ip route add bgp-communities=1111:333 comment=ddosed distance=1 dst-address="$id/32" type=blackhole i would like to run this every 5min and it will check again if ip removed from ddosed it will automatically remove it from black hole also.

Thanks.
 
User avatar
Deantwo
Member
Member
Posts: 332
Joined: Tue Sep 30, 2014 4:07 pm

Re: Scripting Ideas ??

Fri Jul 01, 2016 11:08 am

some script will take ddosed ip's from /ip firewall address-list and will add this to black hole like this /ip route add bgp-communities=1111:333 comment=ddosed distance=1 dst-address="$id/32" type=blackhole i would like to run this every 5min and it will check again if ip removed from ddosed it will automatically remove it from black hole also.
So you have the address-list already? Then why don't you just make a firewall rule to drop all packets from that address-list? Why does it have to be a route?
/ip firewall filter
add action=drop chain=forward src-address-list=test comment=ddosed 
add action=drop chain=input src-address-list=test comment=ddosed 
 
User avatar
amt
Long time Member
Long time Member
Posts: 529
Joined: Fri Jan 16, 2015 2:05 pm

Re: Scripting Ideas ??

Fri Jul 01, 2016 11:33 am

some script will take ddosed ip's from /ip firewall address-list and will add this to black hole like this /ip route add bgp-communities=1111:333 comment=ddosed distance=1 dst-address="$id/32" type=blackhole i would like to run this every 5min and it will check again if ip removed from ddosed it will automatically remove it from black hole also.
So you have the address-list already? Then why don't you just make a firewall rule to drop all packets from that address-list? Why does it have to be a route?
/ip firewall filter
add action=drop chain=forward src-address-list=test comment=ddosed 
add action=drop chain=input src-address-list=test comment=ddosed 
but I would like to sent them to upstream provider blackhole :)
 
User avatar
Deantwo
Member
Member
Posts: 332
Joined: Tue Sep 30, 2014 4:07 pm

Re: Scripting Ideas ??

Fri Jul 01, 2016 1:50 pm

but I would like to sent them to upstream provider blackhole :)
Weird way of doing it, but ok.

Try this:
{
    :local ddosedList [/ip firewall address-list find list=ddosed]
    :foreach ddosedEntry in=$ddosedList do={
        :local ddosedIp [/ip firewall address-list get $ddosedEntry address]
        :if ([:len [/ip route find comment=ddosed dst-address="$ddosedIp/32"]] = 0) do={
            /ip route add bgp-communities=1111:333 comment=ddosed distance=1 dst-address="$ddosedIp/32" type=blackhole
        }
    }
}
{
    :local blackholeList [/ip route find comment=ddosed]
    :foreach blackholeEntry in=$blackholeList do={
        :local blackholeIp [/ip route get $blackholeEntry dst-address]
        :set blackholeIp [:pick $blackholeIp 0 [:find $blackholeIp "/" -1]]
        :if ([:len [/ip firewall address-list find list=ddosed address=$blackholeIp]] = 0) do={
            /ip route remove $blackholeEntry
        }
    }
}
It worked in my little test at least.
I couldn't think of a better way to do it than check each list one at a time.
 
User avatar
amt
Long time Member
Long time Member
Posts: 529
Joined: Fri Jan 16, 2015 2:05 pm

Re: Scripting Ideas ??

Fri Jul 01, 2016 2:35 pm

but I would like to sent them to upstream provider blackhole :)
Weird way of doing it, but ok.

Try this:
{
    :local ddosedList [/ip firewall address-list find list=ddosed]
    :foreach ddosedEntry in=$ddosedList do={
        :local ddosedIp [/ip firewall address-list get $ddosedEntry address]
        :if ([:len [/ip route find comment=ddosed dst-address="$ddosedIp/32"]] = 0) do={
            /ip route add bgp-communities=1111:333 comment=ddosed distance=1 dst-address="$ddosedIp/32" type=blackhole
        }
    }
}
{
    :local blackholeList [/ip route find comment=ddosed]
    :foreach blackholeEntry in=$blackholeList do={
        :local blackholeIp [/ip route get $blackholeEntry dst-address]
        :set blackholeIp [:pick $blackholeIp 0 [:find $blackholeIp "/" -1]]
        :if ([:len [/ip firewall address-list find list=ddosed address=$blackholeIp]] = 0) do={
            /ip route remove $blackholeEntry
        }
    }
}
It worked in my little test at least.
I couldn't think of a better way to do it than check each list one at a time.

Thank you very much... thanks a lot... thats wonderfull.. thats what i want :)

Thanks Thanks Thanks
 
Herbie
just joined
Posts: 7
Joined: Mon Nov 08, 2010 8:17 pm

Re: Scripting Ideas ??

Mon Aug 29, 2016 11:24 am

Hi Sinan,
i need help with this script:

:local phone "android";
:local telefon value=[/ip dhcp-server lease find dynamic=yes host-name~$phone];
:foreach looping in=$telefon do={
:local mac value=[/ip dhcp-server lease get $looping value-name=active-mac-address];
/ip firewall filter add chain=forward src-mac-address=$mac action=reject comment=szkriptdroid disabled=no
/ip dhcp-server lease remove [/ip dhcp-server lease find dynamic=yes active-mac-address=$mac];
log info $mac;
};

it works as i want but it fills the firewall with same mac addresses, so i need to filter out already added addresses. i tried if statement but im bad in programing and didnt succeded. please help
 
User avatar
Deantwo
Member
Member
Posts: 332
Joined: Tue Sep 30, 2014 4:07 pm

Re: Scripting Ideas ??

Mon Aug 29, 2016 12:49 pm

Hi Sinan,
i need help with this script:
:local phone "android";
:local telefon value=[/ip dhcp-server lease find dynamic=yes host-name~$phone]; 
:foreach looping in=$telefon do={
    :local mac value=[/ip dhcp-server lease get $looping value-name=active-mac-address]; 
    /ip firewall filter add chain=forward src-mac-address=$mac action=reject comment=szkriptdroid disabled=no
    /ip dhcp-server lease remove [/ip dhcp-server lease find dynamic=yes active-mac-address=$mac];
    log info $mac;
};
it works as i want but it fills the firewall with same mac addresses, so i need to filter out already added addresses. i tried if statement but im bad in programing and didnt succeded. please help
.
:local phone "android"
:local phoneLeases [/ip dhcp-server lease find dynamic=yes host-name~$phone]
:foreach looping in=$phoneLeases do={
    :local mac [/ip dhcp-server lease get $looping active-mac-address]
    :if ([:len [/ip firewall filter find src-mac-address=$mac]] = 0) do={
        /ip firewall filter add chain=forward src-mac-address=$mac action=reject comment="szkriptdroid" disabled=no
        /ip dhcp-server lease remove $looping
        :log info "Mac address \"$mac\" blocked for having \"$phone\" in the name."
    }
}
I didn't test it, but I think this should work better.
Last edited by Deantwo on Tue Feb 25, 2020 5:13 pm, edited 8 times in total.
 
Herbie
just joined
Posts: 7
Joined: Mon Nov 08, 2010 8:17 pm

Re: Scripting Ideas ??

Fri Sep 02, 2016 12:24 pm

Hi Sinan,
i need help with this script:

...

it works as i want but it fills the firewall with same mac addresses, so i need to filter out already added addresses. i tried if statement but im bad in programing and didnt succeded. please help
:local phone "android"
:local phoneLeases [/ip dhcp-server lease find dynamic=yes host-name~$phone]
:foreach looping in=$phoneLeases do={
    :local mac [/ip dhcp-server lease get $looping active-mac-address]
    :if ([:len [/ip firewall filter find src-mac-address=$mac]] = 0) do={
        /ip firewall filter add chain=forward src-mac-address=$mac action=reject comment="szkriptdroid" disabled=no
        /ip dhcp-server lease remove $looping
        :log info "Mac address \"$mac\" blocked for having \"$phone\" in the name."
    }
}
I didn't test it, but I think this should work better.
Thank you very much. It works as I wanted
 
User avatar
fengyuclub
Member Candidate
Member Candidate
Posts: 109
Joined: Mon Dec 09, 2013 8:50 am

Re: Scripting Ideas ??

Tue Dec 27, 2016 11:48 am

Who is online

Users browsing this forum: No registered users and 18 guests