ucomesdag

Mikrotik lease-script

Jan 28th, 2022 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #!rsc by RouterOS
  2. # RouterOS script: lease-script
  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. # Expected environment variables:
  42. # leaseBound 1 = lease bound, 0 = lease removed
  43. # leaseServerName Name of DHCP server
  44. # leaseActIP IP address of DHCP client
  45.  
  46. :if ($leaseBound = 1) do={
  47.  
  48. :local ttl [ /ip/dhcp-server/get [ /ip/dhcp-server/find name=$leaseServerName ] lease-time ];
  49. :local domain [ /ip/dhcp-server/network/get [ /ip/dhcp-server/network/find $leaseActIP in address ] domain ];
  50. :local leaseId [ /ip/dhcp-server/lease/find address=$leaseActIP ];
  51. :local hostname [ /ip/dhcp-server/lease/get $leaseId host-name ];
  52. :local comment [ /ip/dhcp-server/lease/get $leaseId comment ];
  53.  
  54. # Normalize the hostname and delete all disallowed chars from it
  55. :set hostname [ $normalizeHostname name=$hostname ];
  56.  
  57. # Set the host name to comment field value if set
  58. :if ( [ :len $comment ] != 0 ) do={
  59. :set hostname [ $normalizeHostname name=$comment ];
  60. };
  61.  
  62. # Use MAC address as a hostname if the hostname is missing or contains only disallowed chars
  63. :if ( [ :len $hostname ] = 0 ) do={
  64. :set hostname [ $normalizeHostname name=$leaseActMAC ];
  65. };
  66.  
  67. # Set ttl to script settings if enabled
  68. :if ( [ :len $dnsTTL ] != 0) do={
  69. :set ttl $dnsTTL;
  70. };
  71.  
  72. # Check for existing lease
  73. :if ( [ :len $leaseId ] != 1) do={
  74. :log info "DHCP2DNS: not registering domain name for address $leaseActIP because of multiple active leases for $leaseActIP";
  75. :error "multiple active leases for $leaseActIP";
  76. };
  77.  
  78. # Check if we have a network domain name
  79. :if ( [ :len $domain ] <= 0 ) do={
  80. :log error "DHCP2DNS: not registering domain name for address $leaseActIP because of empty network domain name";
  81. :error "empty network domain name";
  82. };
  83.  
  84. # Set the fqdn
  85. :local fqdn "$hostname.$domain";
  86.  
  87. # Atempt to register the lease
  88. :if ( [ :len [ /ip/dns/static/find name=$fqdn and address=$leaseActIP and disabled=no ] ] = 0 ) do={
  89. :log info "DHCP2DNS: registering static domain name $fqdn for address $leaseActIP with ttl $ttl";
  90. /ip/dns/static/add address=$leaseActIP name=$fqdn ttl=$ttl comment=$dhcpTag disabled=no;
  91. } else={
  92. :log error "DHCP2DNS: not registering domain name $fqdn for address $leaseActIP because of existing active static DNS entry with this name or address";
  93. };
  94.  
  95. } else={
  96.  
  97. # Find static lease with correct tag
  98. :local dnsDhcpId [ /ip/dns/static/find address=$leaseActIP and comment=$dhcpTag ];
  99.  
  100. # Remove lease
  101. :if ( [ :len $dnsDhcpId ] > 0 ) do={
  102. :log info "DHCP2DNS: removing static domain name(s) for address $leaseActIP";
  103. /ip/dns/static/remove $dnsDhcpId;
  104. };
  105.  
  106. };
Add Comment
Please, Sign In to add comment