Giter Site home page Giter Site logo

Comments (10)

johnmmcgee avatar johnmmcgee commented on May 18, 2024

Actually, since the comment from the access list is already being brought over to the DHCP Leases, perhaps things could simplified and just have an option to use the comment from DHCP.

from routeros-scripts.

eworm-de avatar eworm-de commented on May 18, 2024

Comments from Telegram Chat, in case it has some more important details:

DHCP comments for dns names would be a nice addition, as it would be in sync with using access list comments for dhcp entries and it would also remove issues with multiple entries and some items reporting information wrong, such as using blocky as a dns proxy. If i have multiple entries, when i look at the statistics it will show one or the other. I am open to discussion about any reason this should not be a good feature request.
also it would remove some of the inconsistencies i have come across. for example, at times phone entries will go in as mac address naming into DNS. etc
I'd be willing to donate a case of beer for the cause :D
or perhaps i could attempt to write it myself and contribute.

from routeros-scripts.

eworm-de avatar eworm-de commented on May 18, 2024

Well, all the DHCP leases and DNS static records stuff is kind of tricky. I am happy that it kind of works currently. 😜

There are some issues with this:

  • Currently the order is: dhcp-to-dns -> collect-wireless-mac -> dhcp-lease-comment... This make sense as I have the DNS name in place to add it into the notification from collect-wireless-mac. To get the info into dhcp-to-dns I would have to switch order - which causes other troubles - or try to get the info from access list directly. The latter would require to duplicate the script for local wireless and caps-man.
  • The comment in access list is not limited in what it is allowed to contain. Actually it is intended to contain human readable information. Squeezing that into valid DNS names could be troublesome. 😝
  • Probably more I do not recognize now.

However I do agree that it is inconsistent to have the mac address for some and host name (if available) for other records. So I am preparing some changes now. The plan is to always have an A type records with the mac address, and a CNAME type records pointing to the A record if the host name is available. (To test this install dhcp-to-dns from next branch.)

So wondering if you could just create another CNAME type record that suits your needs. It can always point to the A type record with the mac address then, and it resolves correctly even if the ip address changes.

from routeros-scripts.

eworm-de avatar eworm-de commented on May 18, 2024

Also could you give some more detail on the issue with blocky dns proxy and the statistics thing that shows one or the other? Or is that solved by using CNAME type records?

from routeros-scripts.

johnmmcgee avatar johnmmcgee commented on May 18, 2024

However I do agree that it is inconsistent to have the mac address for some and host name (if available) for other records. So I am preparing some changes now. The plan is to always have an A type records with the mac address, and a CNAME type records pointing to the A record if the host name is available. (To test this install dhcp-to-dns from next branch.)

Could an option be created that would do a CNAME record that is pointing to the A record if the hostname OR a comment is available? I would imagine you could make an option switch in the global-config that would let you ask a preference of which one.

Either hostname OR comment, or prefer comment over hostname or something along those lines.

Also could you give some more detail on the issue with blocky dns proxy and the statistics thing that shows one or the other? Or is that solved by using CNAME type records?

Not certain why this does this. I would have to look further into way blocky operates.

from routeros-scripts.

eworm-de avatar eworm-de commented on May 18, 2024

The issues from above would still apply, no?
Thinking about it, but as soon as the comment from access list comes into the game things become a lot more complex.

from routeros-scripts.

johnmmcgee avatar johnmmcgee commented on May 18, 2024

I suppose it isn't that complicated from my perspective as my access list comments are pretty simple. I set the comment to the hostname or purpose. For example, my kids laptop is named link, so i just comment it as "link" (big zelda fans here :D). My doorbell is simply "camera-doorbell". My backdoor camera is simply "camera-backdoor", cellphone is simply "dads-cellphone". This carries over to the dhcp server comments. The option would be nice to allow that comment to be placed in as the dns entry as well.

With that said, based on MY personal use case, my suggestion would be to have the MAC address as the A record, a CNAME with the hostname if available, but allow an override to allow the DHCP comment as the A record or CNAME, if that option is defined in config and it is available. I personally do not like the idea of MAC address as a dns record, but i can see the desire to have SOMETHING that is consistent and that is one thing that would be consistent.

I will make an attempt to mock something up and submit a patch to see if you find it usable for your scripts.

from routeros-scripts.

eworm-de avatar eworm-de commented on May 18, 2024

You should not spread the discussion over at least three places... Let's keep it here.

I gave it some more thought and came up with 7d5c839... It does not use plain comment, but would require you to set hostname=link for your kid's laptop.

Also note that this requires a second execution of the script with the default order of lease scripts if the comment comes from access list.

from routeros-scripts.

johnmmcgee avatar johnmmcgee commented on May 18, 2024

No worries @eworm-de. My intention of messaging you over the Telegram chat was not to fragment the conversation. I had some questions about scripting under routeros, and I assumed that would be a quicker way to chat. My intention was to share my work here.

I rather like your solution. I can just put `hostname=link" in the access list and it would eventually carry over, it looks like.

Here is what I came up with today. You can use the modified dhcp-to-dns.rsc below by setting the following in global-config-overlay

UseDHCPConfig true;

How this will work is it will first use a comment from the dhcp server, if available. If not it will use the hostname and lastly if neither is available it will create an A record with the mac address.

Setting that will also forego the creation of the CNAME.

This is just a quick hack to demonstrate the functionality that I had in mind.

dns-to-dhcp;

#!rsc by RouterOS
# RouterOS script: dhcp-to-dns
# Copyright (c) 2013-2023 Christian Hesse <[email protected]>
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
#
# provides: lease-script, order=20
#
# check DHCP leases and add/remove/update DNS entries
# https://git.eworm.de/cgit/routeros-scripts/about/doc/dhcp-to-dns.md

:local 0 "dhcp-to-dns";
:global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }

:global Domain;
:global HostNameInZone;
:global Identity;
:global PrefixInZone;
:global ServerNameInZone;
:global UseDHCPComment;

:global CharacterReplace;
:global IfThenElse;
:global LogPrintExit2;
:global ScriptLock;

$ScriptLock $0 false 10;

:local Zone \
  ([ $IfThenElse ($PrefixInZone = true) "dhcp." ] . \
   [ $IfThenElse ($HostNameInZone = true) ($Identity . ".") ] . $Domain);
:local Ttl 5m;
:local CommentPrefix ("managed by " . $0 . " for ");
:local CommentString ("--- " . $0 . " above ---");

:if ([ :len [ /ip/dns/static/find where (name=$CommentString or (comment=$CommentString and name=-)) type=NXDOMAIN disabled ] ] = 0) do={
  /ip/dns/static/add name=$CommentString type=NXDOMAIN disabled=yes;
  $LogPrintExit2 warning $0 ("Added disabled static dns record with name '" . $CommentString . "'.") false;
}
:local PlaceBefore ([ /ip/dns/static/find where (name=$CommentString or (comment=$CommentString and name=-)) type=NXDOMAIN disabled ]->0);

:foreach DnsRecord in=[ /ip/dns/static/find where comment~("^" . $CommentPrefix) !(type=CNAME) ] do={
  :local DnsRecordVal [ /ip/dns/static/get $DnsRecord ];
  :local MacAddress [ $CharacterReplace ($DnsRecordVal->"comment") $CommentPrefix "" ];
  :if ([ :len [ /ip/dhcp-server/lease/find where mac-address=$MacAddress address=($DnsRecordVal->"address") status=bound ] ] > 0) do={
    $LogPrintExit2 debug $0 ("Lease for " . $MacAddress . " (" . $DnsRecordVal->"name" . ") still exists. Not deleting DNS entry.") false;
  } else={
    :local Found false;
    $LogPrintExit2 info $0 ("Lease expired for " . $MacAddress . " (" . $DnsRecordVal->"name" . "), deleting DNS entry.") false;
    /ip/dns/static/remove $DnsRecord;
    /ip/dns/static/remove [ find where type=CNAME cname=($DnsRecordVal->"name") comment=($DnsRecordVal->"comment") ];
  }
}

:foreach Lease in=[ /ip/dhcp-server/lease/find where status=bound ] do={
  :local LeaseVal;
  :do {
    :set LeaseVal [ /ip/dhcp-server/lease/get $Lease ];
  } on-error={
    $LogPrintExit2 debug $0 ("A lease just vanished, ignoring.") false;
  }

  :if ([ :len ($LeaseVal->"address") ] > 0) do={
    :local Comment ($CommentPrefix . $LeaseVal->"mac-address");
    :local HostnameSet [ $CharacterReplace ($LeaseVal->"mac-address") ":" "-" ];
    :local HostName [ $CharacterReplace ($LeaseVal->"host-name") " " "" ];
    :local Domain ([ $IfThenElse ($ServerNameInZone = true) ($LeaseVal->"server" . ".") ] . $Zone);
  
    :if ($UseDHCPComment = true && [ :len ($LeaseVal->"comment") ] > 0) do={ :set HostnameSet ($LeaseVal->"comment"); } 
    :if ($UseDHCPComment = true && [ :len ($LeaseVal->"comment") ] = 0 && [ :len $HostName ] > 0) do={ :set HostnameSet ($HostName); }

    :local DnsRecord [ /ip/dns/static/find where name=($HostnameSet . "." . $Domain) ];
    :if ([ :len $DnsRecord ] > 0) do={
      :local DnsIp [ /ip/dns/static/get $DnsRecord address ];

      :local DupMacLeases [ /ip/dhcp-server/lease/find where mac-address=($LeaseVal->"mac-address") status=bound ];
      :if ([ :len $DupMacLeases ] > 1) do={
        $LogPrintExit2 debug $0 ("Multiple bound leases found for mac-address " . ($LeaseVal->"mac-address") . ", using ip address of last one.") false;
        :set ($LeaseVal->"address") [ /ip/dhcp-server/lease/get ($DupMacLeases->([ :len $DupMacLeases ] - 1)) address ];
      }

      :if ($DnsIp = $LeaseVal->"address") do={
        $LogPrintExit2 debug $0 ("DNS entry for " . ($HostnameSet . "." . $Domain) . " does not need updating.") false;
      } else={
        $LogPrintExit2 info $0 ("Replacing DNS entry for " . ($HostnameSet . "." . $Domain) . ", new address is " . $LeaseVal->"address" . ".") false;
        /ip/dns/static/set address=($LeaseVal->"address") $DnsRecord;
      }

      :local Cname [ /ip/dns/static/find where type=CNAME cname=($HostnameSet . "." . $Domain) comment=$Comment ];
      :if ([ :len $Cname ] > 0 && [ /ip/dns/static/get $Cname name ] != ($HostName . "." . $Domain)) do={
        $LogPrintExit2 info $0 ("Host name changed, updating CNAME (pointing to " . ($HostnameSet . "." . $Domain) . ") to " . ($HostName . "." . $Domain) . ".") false;
        /ip/dns/static/set name=($HostName . "." . $Domain) $Cname;
      }
    } else={
      $LogPrintExit2 info $0 ("Adding new DNS entry for " . ($HostnameSet . "." . $Domain) . ", address is " . $LeaseVal->"address" . ".") false;
      /ip/dns/static/add name=($HostnameSet . "." . $Domain) type=A address=($LeaseVal->"address") ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
      :if ([ :len $HostName ] > 0 && $UseDHCPComment = false) do={
        /ip/dns/static/add name=($HostName . "." . $Domain) type=CNAME cname=($HostnameSet . "." . $Domain) ttl=$Ttl comment=$Comment place-before=$PlaceBefore;
      }
    }
  } else={
    $LogPrintExit2 debug $0 ("No address available... Ignoring.") false;
  }
}

from routeros-scripts.

eworm-de avatar eworm-de commented on May 18, 2024

Having a name based on the mac address is the most reliable we can have here, and we should not dismiss this. Let's use a CNAME pointing to the mac address based name for everything else - coming from host name or comment.

So no, I will not accept your changes.

But I think d700dbc comes pretty close to what you requested and should serve you well.

from routeros-scripts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.