Community discussions

MikroTik App
 
pablo
newbie
Topic Author
Posts: 46
Joined: Sun Apr 18, 2010 8:18 am

:resolve function and multiple ip addresses

Tue Jan 03, 2012 11:34 am

so :resolve returns on ip address at a time. It seems like multiple calls to resolve will return different ip addresses if more than one is assigned to a serverand then it'll cycle through them... so for example:
[admin@MikroTik] > :put [:resolve  talkr.l.google.com]
72.14.203.126
[admin@MikroTik] > :put [:resolve  talkr.l.google.com]
209.85.225.126
[admin@MikroTik] > :put [:resolve  talkr.l.google.com]
74.125.113.126
[admin@MikroTik] > :put [:resolve  talkr.l.google.com]
74.125.93.126
[admin@MikroTik] > :put [:resolve  talkr.l.google.com]
209.85.229.126
[admin@MikroTik] > :put [:resolve  talkr.l.google.com]
74.125.159.126
[admin@MikroTik] > :put [:resolve  talkr.l.google.com]
74.125.65.126
[admin@MikroTik] > :put [:resolve  talkr.l.google.com]
74.125.157.126
[admin@MikroTik] > :put [:resolve  talkr.l.google.com]
209.85.145.126
[admin@MikroTik] > :put [:resolve  talkr.l.google.com]
74.125.127.126
[admin@MikroTik] > :put [:resolve  talkr.l.google.com]
74.125.45.126
[admin@MikroTik] > :put [:resolve  talkr.l.google.com]
209.85.225.126
[admin@MikroTik] > 
When I try this out with: pop.gmail.com or plus.pop.mail.yahoo.com (which only have 2 ip addresses each) :resolve alternates predictably between them. I'm trying to figure out exactly how :resolve works so I can script around this to make sure I've picked up all of the ip addresses associated with a server and not missed any.

I've dug through the forums and documentation but haven't found anything so any help would be appreciated.
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: :resolve function and multiple ip addresses

Tue Jan 03, 2012 12:55 pm

on request one address is given as first record and the rest (if given at all) are give as secondary options. It is called RR load ballancing, to reduce load on one host, several hosts can resolve same name to different addresses. as result several separate servers/clusters/whatever can respond to manu more requests coming in when there is no difference what is serving your request. :resolve just gives you one option out of many - desired behaviour of service provider. Oser way service provide rwhen setting up DNS names can set up priority what host to give out and what not.
 
pablo
newbie
Topic Author
Posts: 46
Joined: Sun Apr 18, 2010 8:18 am

Re: :resolve function and multiple ip addresses

Tue Jan 03, 2012 11:55 pm

Thanks for the quick response. I definitely understand the behaviour but I'm used to working with something like nslookup. Like to figure out if there is a way of doing something similar and getting all of the ip addresses. I'm creating address-lists for firewall rules/mangle so I don't just want one that'll work but I want all of them at that point in time.
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: :resolve function and multiple ip addresses

Wed Jan 04, 2012 9:19 am

it is more like - i have this domain name, give me an working address.
 
pablo
newbie
Topic Author
Posts: 46
Joined: Sun Apr 18, 2010 8:18 am

Re: :resolve function and multiple ip addresses

Wed Jan 04, 2012 11:54 am

Thank you. In case it helps someone else (and if anyone wants to give me some feedback) here is what I ended up doing:

#delete old address lists
:foreach a in=[/ip firewall address-list find list=safe_pop_servers] do={
  /ip firewall address-list remove $a;
}

:local popServers {"plus.pop.mail.yahoo.com";"pop.gmail.com"};

:foreach popServer in=$popServers do={
  :resolve $popServer;

#get any A records and add them directly
  :foreach aRecord in=[/ip dns cache all find where (name=$popServer && type="A")] do={
    /ip firewall address-list add list=safe_pop_servers address=[/ip dns cache all get $aRecord data] comment=$popServer;
  }

#Check for CNAME
  :local cname;
  :local nextCname
  :set cname [/ip dns cache all find where (name=$popServer && type="CNAME")];
  :set nextCname [/ip dns cache all find where (name=[/ip dns cache all get $cname data] && type="CNAME")];

  :while ($nextCname != "") do={
        :set cname $nextCname;
        :set nextCname [/ip dns cache all find where (name=[/ip dns cache all get $cname data] && type="CNAME")];
  }
  
  :foreach aRecord in=[/ip dns cache all find where (name=[/ip dns cache all get $cname data] && type="A")] do={
    /ip firewall address-list add list=safe_pop_servers address=[/ip dns cache all get $aRecord data] comment=$popServer;
  }
}
 
pablo
newbie
Topic Author
Posts: 46
Joined: Sun Apr 18, 2010 8:18 am

Re: :resolve function and multiple ip addresses

Thu Jan 05, 2012 11:27 am

And here's a slightly more sophisticated version of the script. First you need to set three globals before calling...
:global Servers {"apple.com";"pop.gmail.com";"pop.plus.mail.yahoo.com";"scs.msg.yahoo.com";"scsa.msg.yahoo.com";"scsc.msg.yahoo.com"}
:global Done true
:global ListName test
then you can go ahead and call it anyway you want. And here's the script. Again.. hope it helps... not fully tested and error handling is of course an issue but feedback is welcome as I hope to learn from this effort:
:global ListName
:global Servers
:global Done

#make sure previous runs have finished
while (!$Done) do={
  :nothing;
}

#delete old address lists
:foreach aListItem in=[/ip firewall address-list find list=$ListName] do={
  /ip firewall address-list remove $aListItem;
}

:foreach aServer in=$Servers do={
#force the dns entries to be cached
  :resolve $aServer;

  :foreach dnsRecord in=[/ip dns cache all find where (name=$aServer)] do={
#if it's an A records add it directly
    :if ([/ip dns cache all get $dnsRecord type]="A") do={
       /ip firewall address-list add list=$ListName address=[/ip dns cache all get $dnsRecord data] comment=$aServer;
    }
  

#if it's a CNAME follow it until we get A records
    :if ([/ip dns cache all get $dnsRecord type]="CNAME") do={
      :local cname;
      :local nextCname
      :set cname [/ip dns cache all find where (name=$aServer && type="CNAME")];
      :set nextCname [/ip dns cache all find where (name=[/ip dns cache all get $cname data] && type="CNAME")];

      :while ($nextCname != "") do={
          :set cname $nextCname;
          :set nextCname [/ip dns cache all find where (name=[/ip dns cache all get $cname data] && type="CNAME")];
        }
  
#add the a records we found
    :foreach aRecord in=[/ip dns cache all find where (name=[/ip dns cache all get $cname data] && type="A")] do={
      /ip firewall address-list add list=$ListName address=[/ip dns cache all get $aRecord data] comment=$aServer;
      }
    }
  }
}
#allow other scripts to call this
:set Done true
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: :resolve function and multiple ip addresses

Thu Jan 05, 2012 12:14 pm

looks good, after some time you could set up wiki page with this script so it is easier to find.
 
pablo
newbie
Topic Author
Posts: 46
Joined: Sun Apr 18, 2010 8:18 am

Re: :resolve function and multiple ip addresses

Fri Jan 06, 2012 1:30 am

I don't have perm for private messages... how do I register to post on the wiki?
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: :resolve function and multiple ip addresses

Fri Jan 06, 2012 8:42 am

write to support for wiki account.
 
User avatar
huigezi
newbie
Posts: 43
Joined: Sat Dec 24, 2011 4:39 am
Location: apple

Re: :resolve function and multiple ip addresses

Sun Jan 08, 2012 10:15 am

find where

why to use the where? what mean?
 
pablo
newbie
Topic Author
Posts: 46
Joined: Sun Apr 18, 2010 8:18 am

Re: :resolve function and multiple ip addresses

Mon Jan 09, 2012 2:56 am

Well each one serves a different purpose...

1. "/ip dns cache all find where (name=$aServer)"
Find everything added to the dns cache for the server we are trying to add to the ip address list

2. "/ip dns cache all find where (name=$aServer && type="CNAME")"
Keep in mind that this done prior to a while do and the value will change. The goal is to follow the cname entries until we get to A records

3. '/ip dns cache all find where (name=[/ip dns cache all get $cname data] && type="A")"
Finally we're trying to get the a records that we found. The data of the cname record will be the A record

P
 
User avatar
indnti
Frequent Visitor
Frequent Visitor
Posts: 86
Joined: Thu Nov 09, 2006 11:53 am

Re: :resolve function and multiple ip addresses

Tue Nov 06, 2018 5:45 pm

Works great for amazon cloud addresses - thanks a lot

Who is online

Users browsing this forum: No registered users and 44 guests