ucomesdag

Mikrotik dhcp2dns

Jan 28th, 2022 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #!rsc by RouterOS
  2. # RouterOS script: dhcp2dns
  3. # Copyright (c) 2022 Uco Mesdag <uco@mesd.ag>
  4.  
  5. ###
  6. # Settings
  7.  
  8. :local dhcpTag "#DHCP";
  9. :local dnsTTL "1d 00:00:00";
  10.  
  11. # Normalize hostname (e.g. "-= My Phone =-" -> "My-Phone")
  12. # - truncate length to 63 chars
  13. # - substitute disallowed chars with a hyphen
  14. # param: name
  15. :local normalizeHostname do={
  16. :local result;
  17. :local isInvalidChar true;
  18. :for i from=0 to=([:len $name]-1) do={
  19. :local char [:pick $name $i];
  20. :if ($i < 63) do={
  21. :if ($char~"[a-zA-Z0-9]") do={
  22. :set result ($result . $char);
  23. :set isInvalidChar false;
  24. } else={
  25. :if (!$isInvalidChar) do={
  26. :set result ($result . "-");
  27. :set isInvalidChar true;
  28. };
  29. };
  30. };
  31. };
  32. # Delete trailing hyphen
  33. :if ($isInvalidChar) do={
  34. :set result [:pick $result 0 ([:len $result]-1)];
  35. }
  36. :return $result;
  37. };
  38.  
  39. ###
  40. # Script entry point
  41.  
  42. :foreach leaseId in=[ /ip/dhcp-server/lease/find ] do={
  43.  
  44. :local leaseActIP [ /ip/dhcp-server/lease/get $leaseId address ];
  45. :local leaseActMAC [ /ip/dhcp-server/lease/get $leaseId mac-address ];
  46. :local leaseServerName [ /ip/dhcp-server/lease/get $leaseId server ];
  47.  
  48. :local hostname [ /ip/dhcp-server/lease/get $leaseId host-name ];
  49. :local ttl [ /ip/dhcp-server/get [ /ip/dhcp-server/find name=$leaseServerName ] lease-time ];
  50. :local domain [ /ip/dhcp-server/network/get [ /ip/dhcp-server/network/find $leaseActIP in address ] domain ];
  51. :local comment [ /ip/dhcp-server/lease/get $leaseId comment ];
  52.  
  53. # Normalize the hostname and delete all disallowed chars from it
  54. :set hostname [ $normalizeHostname name=$hostname ];
  55.  
  56. # Set the host name to comment field value if set
  57. :if ( [ :len $comment ] != 0 ) do={
  58. :set hostname [ $normalizeHostname name=$comment ];
  59. };
  60.  
  61. # Use MAC address as a hostname if the hostname is missing or contains only disallowed chars
  62. :if ( [ :len $hostname ] = 0 ) do={
  63. :set hostname [ $normalizeHostname name=$leaseActMAC ];
  64. };
  65.  
  66. # Set ttl to script settings if enabled
  67. :if ( [ :len $dnsTTL ] != 0) do={
  68. :set ttl $dnsTTL;
  69. };
  70.  
  71. # Check if we have a network domain name
  72. :if ( [ :len $domain ] <= 0 ) do={
  73. :log error "DHCP2DNS: not registering domain name for address $leaseActIP because of empty network domain name";
  74. };
  75.  
  76. # Set the fqdn
  77. :local fqdn "$hostname.$domain";
  78.  
  79. # Atempt to register the lease
  80. :if ( [ :len [ /ip/dns/static/find name=$fqdn and address=$leaseActIP and disabled=no ] ] = 0 ) do={
  81. :log info "DHCP2DNS: registering static domain name $fqdn for address $leaseActIP with ttl $ttl";
  82. /ip/dns/static/add address=$leaseActIP name=$fqdn ttl=$ttl comment=$dhcpTag disabled=no;
  83. } else={
  84. :log error "DHCP2DNS: not registering domain name $fqdn for address $leaseActIP because of existing active static DNS entry with this name or address";
  85. };
  86.  
  87. };
Add Comment
Please, Sign In to add comment