Community discussions

MikroTik App
 
raymonvdm
Member Candidate
Member Candidate
Topic Author
Posts: 161
Joined: Mon Jan 31, 2005 7:47 pm

DHCP lease to DNS

Mon Dec 10, 2007 3:05 pm

Hi,

I have found a "Setting static DNS record for each DHCP lease" script on the Wiki BUT

I have serveral different DHCP Server with different IP ranges. But the script adds only one domain name to alle different leases. How do i make a difference betwean the different networks

dhcp.lan.local
dhcp.hotspot.local
dhcp.laptop1.local
dhcp.laptop2.local
:set topdomain "yourdomain.com" ;
/ip dhcp-server lease ;
:foreach i in=[find] \
do={  \
    /ip dhcp-server lease ;
        :if ([:len [get $i host-name]] > 0) do={ \
           :set hostname ([get $i host-name] . "." . $topdomain); \
           :set hostip [get $i address]; \
           :put ( $hostname . " : " . $hostip ) ; \
           /ip dns static ;
           :foreach di in [find] do = { :if ([get $di name] = $hostname) do {/ip dns static remove $di }}
           /ip dns static add name=$hostname address=$hostip
         }  \
};

 
User avatar
SeaburyNorton
Frequent Visitor
Frequent Visitor
Posts: 65
Joined: Tue Sep 28, 2010 9:39 pm

Re: DHCP lease to DNS

Sun Aug 19, 2012 10:07 pm

I would also love to know the answer to this one... I'm running two dhcp servers on 2 different interfaces with 2 different network tags... anyone?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: DHCP lease to DNS

Sun Aug 19, 2012 10:40 pm

Couldn't you just specify those "manually"? Like:
/ip dhcp-server network add address=192.168.0.100/32 dns-server=dns-for-this-IP
 
User avatar
SeaburyNorton
Frequent Visitor
Frequent Visitor
Posts: 65
Joined: Tue Sep 28, 2010 9:39 pm

Re: DHCP lease to DNS

Sun Aug 19, 2012 10:55 pm

Couldn't you just specify those "manually"? Like:
/ip dhcp-server network add address=192.168.0.100/32 dns-server=dns-for-this-IP

What the script does is create static entries in dns for all dhcp leases to that they may be resolved by name easily. For instance, if dhcp1 is giving out leases on interface eth1 with a network tag of "network.lan", you can have dns records created by script so that each lease gets resolved by hostname:

ex. mach1.network.lan = 192.168.2.100

The script works very well... the issue is that if your implementation is running more than one dhcp server. For instance, if dhcp2 is running off eth2 with a network tag of "second.lan" the script continually adds the "network.lan" tag to the addresses given out. I think what is needed is a script that can actually differentiate, if that is possible.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: DHCP lease to DNS

Sun Aug 19, 2012 11:41 pm

Ah... setting a DNS entry in the router for each lease. I get it now.

Well, the server from which the lease originated can be seen from the "server" property of the lease.

So... if I'm reading the script correctly, instead of having $topdomain defined at the top, it needs to be conditionally defined within the
:if ([:len [get $i host-name]] > 0) do={ \
part.

Something like:
/ip dhcp-server lease ;
:foreach i in=[find] \
do={  \
    /ip dhcp-server lease ;
    :if ([:len [get $i host-name]] > 0) do={ \
        :if ("server1" == [get $i server]) do={ \
            :set hostname ([get $i host-name] . ".dhcp.lan.local"); \
        } else { \
            :if ("server2" == [get $i server]) do={ \
                :set hostname ([get $i host-name] . ".dhcp.hotspot.local"); \
            }
        } \
        :set hostip [get $i address]; \
        :put ( $hostname . " : " . $hostip ) ; \
        /ip dns static ;
        :foreach di in [find] do = { :if ([get $di name] = $hostname) do {/ip dns static remove $di }}
        /ip dns static add name=$hostname address=$hostip
     }  \
};
 
User avatar
SeaburyNorton
Frequent Visitor
Frequent Visitor
Posts: 65
Joined: Tue Sep 28, 2010 9:39 pm

Re: DHCP lease to DNS

Sun Aug 19, 2012 11:52 pm

indeed... that's where we are at now... :?
 
User avatar
SeaburyNorton
Frequent Visitor
Frequent Visitor
Posts: 65
Joined: Tue Sep 28, 2010 9:39 pm

Re: DHCP lease to DNS

Mon Aug 20, 2012 7:57 pm

Ah... setting a DNS entry in the router for each lease. I get it now.

Well, the server from which the lease originated can be seen from the "server" property of the lease.

So... if I'm reading the script correctly, instead of having $topdomain defined at the top, it needs to be conditionally defined within the
:if ([:len [get $i host-name]] > 0) do={ \
part.

Something like:
/ip dhcp-server lease ;
:foreach i in=[find] \
do={  \
    /ip dhcp-server lease ;
    :if ([:len [get $i host-name]] > 0) do={ \
        :if ("server1" == [get $i server]) do={ \
            :set hostname ([get $i host-name] . ".dhcp.lan.local"); \
        } else { \
            :if ("server2" == [get $i server]) do={ \
                :set hostname ([get $i host-name] . ".dhcp.hotspot.local"); \
            }
        } \
        :set hostip [get $i address]; \
        :put ( $hostname . " : " . $hostip ) ; \
        /ip dns static ;
        :foreach di in [find] do = { :if ([get $di name] = $hostname) do {/ip dns static remove $di }}
        /ip dns static add name=$hostname address=$hostip
     }  \
};

I will test this and let you know! I assume you need to change the "server1" and "server2" parameters to what they are on your specific device AND the ".dhcp.lan.local" and ".dhcp.hotspot.local" as well... (Thank you in advance!)
 
User avatar
SeaburyNorton
Frequent Visitor
Frequent Visitor
Posts: 65
Joined: Tue Sep 28, 2010 9:39 pm

Re: DHCP lease to DNS

Mon Aug 20, 2012 8:12 pm

Ah... setting a DNS entry in the router for each lease. I get it now.

Well, the server from which the lease originated can be seen from the "server" property of the lease.

So... if I'm reading the script correctly, instead of having $topdomain defined at the top, it needs to be conditionally defined within the
:if ([:len [get $i host-name]] > 0) do={ \
part.

Something like:
/ip dhcp-server lease ;
:foreach i in=[find] \
do={  \
    /ip dhcp-server lease ;
    :if ([:len [get $i host-name]] > 0) do={ \
        :if ("server1" == [get $i server]) do={ \
            :set hostname ([get $i host-name] . ".dhcp.lan.local"); \
        } else { \
            :if ("server2" == [get $i server]) do={ \
                :set hostname ([get $i host-name] . ".dhcp.hotspot.local"); \
            }
        } \
        :set hostip [get $i address]; \
        :put ( $hostname . " : " . $hostip ) ; \
        /ip dns static ;
        :foreach di in [find] do = { :if ([get $di name] = $hostname) do {/ip dns static remove $di }}
        /ip dns static add name=$hostname address=$hostip
     }  \
};


Just to play Devil's Advocate, the second script on the op's page above (I'll paste it below) allows the usage of TTL to differentiate between statically added and dhcp added dns entries. In my travels I find this script is most commonly used over the first one as that ttl protects manually added entries from being overwritten via dhcp. Would you think it possible to add your script to this below?
# Domain to be added to your DHCP-clients hostname
:local topdomain;
:set topdomain "lan";

# Use ttl to distinguish dynamic added DNS records
:local ttl;
:set ttl "00:59:59";

# Set variables to use
:local hostname;
:local hostip;
:local free;

# Remove all dynamic records
/ip dns static;
:foreach a in=[find] do={
  :if ([get $a ttl] = $ttl) do={
    :put ("Removing: " . [get $a name] . " : " . [get $a address]);
    remove $a;
  }
}

/ip dhcp-server lease ;
:foreach i in=[find] do={
  /ip dhcp-server lease ;
  :if ([:len [get $i host-name]] > 0) do={
    :set free "true";
    :set hostname ([get $i host-name] . "." . $topdomain);
    :set hostip [get $i address];
    /ip dns static ;
# Check if entry already exist
    :foreach di in [find] do={
      :if ([get $di name] = $hostname) do={
        :set free "false";
        :put ("Not adding already existing entry: " . $hostname);
      }
    }
    :if ($free = true) do={
      :put ("Adding: " . $hostname . " : " . $hostip ) ;
      /ip dns static add name=$hostname address=$hostip ttl=$ttl;
    }
  }
}
 
User avatar
SeaburyNorton
Frequent Visitor
Frequent Visitor
Posts: 65
Joined: Tue Sep 28, 2010 9:39 pm

Re: DHCP lease to DNS

Wed Aug 22, 2012 5:18 am

Ah... setting a DNS entry in the router for each lease. I get it now.

Well, the server from which the lease originated can be seen from the "server" property of the lease.

So... if I'm reading the script correctly, instead of having $topdomain defined at the top, it needs to be conditionally defined within the
:if ([:len [get $i host-name]] > 0) do={ \
part.

Something like:
/ip dhcp-server lease ;
:foreach i in=[find] \
do={  \
    /ip dhcp-server lease ;
    :if ([:len [get $i host-name]] > 0) do={ \
        :if ("server1" == [get $i server]) do={ \
            :set hostname ([get $i host-name] . ".dhcp.lan.local"); \
        } else { \
            :if ("server2" == [get $i server]) do={ \
                :set hostname ([get $i host-name] . ".dhcp.hotspot.local"); \
            }
        } \
        :set hostip [get $i address]; \
        :put ( $hostname . " : " . $hostip ) ; \
        /ip dns static ;
        :foreach di in [find] do = { :if ([get $di name] = $hostname) do {/ip dns static remove $di }}
        /ip dns static add name=$hostname address=$hostip
     }  \
};

I will test this and let you know! I assume you need to change the "server1" and "server2" parameters to what they are on your specific device AND the ".dhcp.lan.local" and ".dhcp.hotspot.local" as well... (Thank you in advance!)

Haven't had much success... the script won't even run... just does nothing.

Who is online

Users browsing this forum: triss and 44 guests