I came across a situation where I needed to add a routing rule for a domain name that’s bound to a dynamic IP address. So I wrote a very short, easy script and thought I’d share. Hope this saves you time/work if you’re in the need for something similar.
# Routing rules for dynamic IP addresses
# This script resolves a hostname and checks if its IP address is already in the routing rules
# If it isn't it creates a routing rule that looksup only in the specified table with it.
#
# Networker
# Change names here:
:local ip1 [:resolve host1.someonesdomain.com]
:local table1 "regulartraffic"
:local ip2 [:resolve host2.someonesdomain.com]
:local table2 "specialtraffic"
#--------------------------
:if ([:len [/ip route rule find where dst-address=$ip1 . "/32"]] = 0) do={
[/ip route rule add dst-address=$ip1 action=lookup-only-in-table table=$table1]
:log info "New IP added to table $table1"
}
:if ([:len [/ip route rule find where dst-address=$ip2 . "/32"]] = 0) do={
[/ip route rule add dst-address=$ip2 action=lookup-only-in-table table=$table2]
:log info "New IP added to table $table2"
}
Yeah, but what I don’t understand is why is such a basic and important feature not included in RouterOS? Dynamic IP addresses are VERY common for many internet-connected hosts!
You cannot put hostnames in “/routing rule add dst-address=” commands. Such a glaring and evident lack of functionality should be implemented by RouterOS. Why do we have to resort to scripts for such an IMPORTANT and ABSOLUTELY BASIC feature?!?
I don’t know about your particular zone or region, but all the people I know around me who buy Mikrotik routers do of course know what a route is!
And the reason I search for scripts already written instead of writing my own scripts is because Mikrotik’s documentation is extremely poor and dispersed through different websites and forums. Really chaotic and very difficult to follow.
In a way, Mikrotik documentation reminds me of Linux (which is also extremely poorly documented, and you have to search in many forums to find anything of value)
How I wish their docs were as good as FreeBSD or OpenBSD documentation. THAT is really how ALL documentation should be. Of high quality!
Sure, in a scale 1 to 10 it can be graded 2 or 3, while users post are on average 1 or 2, not a very good result.
Honestly, if it was not for you, Amm0, erlinden and another handful of people that have a good knowledge of the script syntax and are sharing it on the forum (guys, thanks to all of you, the ones I named and the ones I didn't ) it would be impossible to write even the most basic script on the basis of the official documentation only.
Particularly the - sometimes subtle - changes between v6 and v7, but even within v7 are often not documented at all.
In fact syntax highlight will show you exactly where the error is and gives you good enough hint where to look.
And the last telling that documentation is bad because it does not have exact script for you your specific needs is lain ignorant. That is not the purpose of the documentation. There are a lot of scripting examples that explains bits by bits how to do, for example, resolve of the DNS, how to execute commands etc. You just need to adapt all the given information for your specific needs.
@mrz
Where is this fantastic repository of scripting examples?
As a lazy, ignorant, clueless user I wasn’t able to find it, and always have to get bits scattered all around the forum (and provided by advanced forum members, not by Mikrotik).
Ok, so a quick & dirty “upgrade” of NetWorker’s script to v7 could be this?
:local ip1 [:resolve host1.someonesdomain.com]
:local table1 "regulartraffic"
:local ip2 [:resolve host2.someonesdomain.com]
:local table2 "specialtraffic"
:if ([:len [/routing rule find where dst-address="$ip1/32"]] = 0) do={
[/routing rule add dst-address="$ip1/32" action=lookup-only-in-table table=$table1]
:log info "New IP added to table $table1"
}
:if ([:len [/routing rule find where dst-address="$ip2/32"]] = 0) do={
[/routing rule add dst-address="$ip2/32" action=lookup-only-in-table table=$table2]
:log info "New IP added to table $table2"
}
Perhaps the only missing bit is how to “clean up” the rules mentioning old IP addresses that the host used in the past?? I’ll try to investigate a bit more and see what can be done about that.
EDIT: mmm, there seems to be a bug in the original script where the second IF statement for host2 creates problem for the entire script. Argh, that needs fixing too!
I use address list, create a list (i.e. WAN-IP) and add the address (blahblah.sn.mynetname.net).
Then you can refer to the address list (with its name).
Yes, that would be the logical way to do it. But Mikrotik, for some unknown reason, doesn't allow to use address lists in the "/routing rule" commands.
Ok, here’s another attempt at trying to modify NetWorker’s original script. I haven’t found an “orthodox” way to cleanup obsolete rules that point to old IP addresses used by hosts in the past. But I think that comments can be recruited to fulfill this function.
:local ip1 [:resolve host1.someonesdomain.com]
:local table1 "regulartraffic"
:local ip2 [:resolve host2.someonesdomain.com]
:local table2 "specialtraffic"
:if ([:len [/routing rule find where dst-address="$ip1/32"]] = 0) do={
[/routing rule remove [find where comment="uglyrule1"]];
[/routing rule add dst-address="$ip1/32" action=lookup-only-in-table table=$table1 comment="uglyrule1"]
:log info "New IP added to table $table1"
}
:if ([:len [/routing rule find where dst-address="$ip2/32"]] = 0) do={
[/routing rule remove [find where comment="uglyrule2"]];
[/routing rule add dst-address="$ip2/32" action=lookup-only-in-table table=$table2 comment="uglyrule2"]
:log info "New IP added to table $table2"
}