Community discussions

MikroTik App
 
matthysdt
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 55
Joined: Tue Jun 01, 2010 11:19 am

How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 1:00 pm

This works:
src-address = [:resolve mysite.com]

But I need scr-address to be !mysite.com

Tried these, all unsuccessful:
!src-address = [:resolve mysite.com]
src-address = [:resolve !mysite.com]
src-address = [:resolve ! mysite.com]
src-address = ![:resolve mysite.com]
src-address != [:resolve mysite.com]
src-address = "!".[:resolve mysite.com]

LOL, can't seem to guess the right way, and googling for "!" doesn't really go down that well.
 
User avatar
huigezi
newbie
Posts: 43
Joined: Sat Dec 24, 2011 4:39 am
Location: apple

Re: How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 1:03 pm

hi

to say ,what you're going to do
 
matthysdt
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 55
Joined: Tue Jun 01, 2010 11:19 am

Re: How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 1:08 pm

hi

to say ,what you're going to do

I want to change the src-address in a filter rule to the new dynamic IP Address of the given DNS.
Since RouterOS immediately resolve mysite.com when the rule is created, I need to periodically update the address.

Note: "mysite.com" is just a random DNS I use as an example, I'm actually using a dyndns.org account here, that's why it's a dynamic IP.
 
User avatar
huigezi
newbie
Posts: 43
Joined: Sat Dec 24, 2011 4:39 am
Location: apple

Re: How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 1:19 pm

hi
See your filter rules

Wait for me to help you write a script
 
User avatar
huigezi
newbie
Posts: 43
Joined: Sat Dec 24, 2011 4:39 am
Location: apple

Re: How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 1:29 pm

add a rule at [ip firewall filter] for example:
/ip firewall filter
add action=drop chain=forward comment=drop disabled=no src-address=10.10.10.10
the comment must be "drop" Of course you can modify their own

then copy the script to scheduler and set 1m-10m interval for exambple

the code is :
:do {
:local com "drop"
:local ip [:resolve mysite.com]
/ip firewall filter
:foreach a in=[find comment=$com] do={
:local cuip [get $a src-address];
:if ($cuip!=$ip) do={
set $a src-address=$ip
:log warning ("new ip is: ".$ip." src-address update is sucess")
}}};/;
Happy learning
 
matthysdt
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 55
Joined: Tue Jun 01, 2010 11:19 am

Re: How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 1:35 pm

add a rule at [ip firewall filter] for example:
/ip firewall filter
add action=drop chain=forward comment=drop disabled=no src-address=10.10.10.10
the comment must be "drop" Of course you can modify their own

then copy the script to scheduler and set 1m-10m interval for exambple

the code is :
:do {
:local com "drop"
:local ip [:resolve mysite.com]
/ip firewall filter
:foreach a in=[find comment=$com] do={
:local cuip [get $a src-address];
:if ($cuip!=$ip) do={
set $a src-address=$ip
:log warning ("new ip is: ".$ip." src-address update is sucess")
}}};/;
Happy learning
Thank you for helping.

How would I encorporate a "!" into this script?
Will this work:
set $a src-address=!$ip
 
User avatar
huigezi
newbie
Posts: 43
Joined: Sat Dec 24, 2011 4:39 am
Location: apple

Re: How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 1:39 pm

can not use the "!" so

My English is not good to express much

I'm so sorry
 
matthysdt
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 55
Joined: Tue Jun 01, 2010 11:19 am

Re: How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 1:50 pm

can not use the "!" so

My English is not good to express much

I'm so sorry

Ok, Thanks.

So how to use the "!" ?
 
User avatar
huigezi
newbie
Posts: 43
Joined: Sat Dec 24, 2011 4:39 am
Location: apple

Re: How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 2:01 pm

can not use the "!" so

My English is not good to express much

I'm so sorry

Ok, Thanks.

So how to use the "!" ?
/ip firewall filter
add action=drop chain=forward comment=drop disabled=no src-address=!10.10.10.10
With the use of script
 
matthysdt
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 55
Joined: Tue Jun 01, 2010 11:19 am

Re: How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 2:04 pm

/ip firewall filter
add action=drop chain=forward comment=drop disabled=no src-address=!10.10.10.10
That doesn't work! This "!" disappears when running the Resolve Script.
 
User avatar
huigezi
newbie
Posts: 43
Joined: Sat Dec 24, 2011 4:39 am
Location: apple

Re: How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 2:12 pm

use the code
:do {
:local com "drop"
:local ip [:resolve mysite.com]
/ip firewall filter
:foreach a in=[find comment=$com] do={
:local cuip [get $a src-address];
:set cuip [:pick $cuip ([:find $cuip "!"]+1) ([:len $cuip])]
:if ($cuip!=$ip) do={
set $a src-address=("!".$ip)
:log warning ("new ip is: ".$ip."src-address update is sucess")
}}};/;
 
matthysdt
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 55
Joined: Tue Jun 01, 2010 11:19 am

Re: How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 2:37 pm

use the code
:do {
:local com "drop"
:local ip [:resolve mysite.com]
/ip firewall filter
:foreach a in=[find comment=$com] do={
:local cuip [get $a src-address];
:set cuip [:pick $cuip ([:find $cuip "!"]+1) ([:len $cuip])]
:if ($cuip!=$ip) do={
set $a src-address=("!".$ip)
:log warning ("new ip is: ".$ip."src-address update is sucess")
}}};/;
Thanks, but this code seems like it only ignores the "!" in the comparison.
How do I SET the scr-address to have a "!" ??
 
User avatar
huigezi
newbie
Posts: 43
Joined: Sat Dec 24, 2011 4:39 am
Location: apple

Re: How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 2:46 pm

:do {
:local com "drop"
:local ip [:resolve mysite.com]
:set ip ("!".$ip)
/ip firewall filter
:foreach a in=[find comment=$com] do={
:local cuip [get $a src-address];
:if ($cuip!=$ip) do={
set $a src-address=$ip
:log warning ("new ip is: ".$ip."src-address update is sucess")
}}};/;
 
matthysdt
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 55
Joined: Tue Jun 01, 2010 11:19 am

Re: How to resolve and "NOT" (!) an address in 1 line

Fri Jan 20, 2012 2:57 pm

Thank you huigezi

Here is my working solution:
:local dnsname "mysite.dyndns.org"
:local dnsip [:resolve $dnsname]
:set dnsip "!$dnsip"
#:log info ("DNSIP : "."$dnsip" );
:local currentip [/ip firewall filter get [find comment="MyFilterMarkerComment"] src-address]
#:log info ("Current IP: "."$dnsip" );
:if ($currentip != $dnsip) do={ /ip firewall filter set [find comment="MyFilterMarkerComment"] src-address=$dnsip ;
:log info ("DNS RESOLVED " . "mysite.dyndns.org" . " new IP =  " . "$dnsip" );
}

}