Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. # NOT fully complete with all the desired features but it does work
  2. # Suggested usage - place in the system scheduler and run every few minutes
  3.  
  4. # Set root domain here
  5. :local zone "your.internal.domain.com";
  6. :local ttl "00:00:10"
  7. :local staticTtl "00:00:30"
  8.  
  9. # TODO : add "nodns" check in static lease comment
  10.  
  11. # Number of pings used to seperate between multiple address hostnames
  12. # zero to disable ping check function
  13. # currently only takes into account up/down.
  14. # TODO: Make it ping both and check response time, returning the fastest
  15. # TODO: Rewrite to make use of :execute to run pings in parallel
  16. :local pingCount 1
  17.  
  18.  
  19. # TODO: Fix bug where old records are not removed from dhcp properly
  20.  
  21. :local hostname
  22. :local ip
  23. :local dnsip
  24. :local dhcpip
  25. :local dnsnode
  26. :local dhcpnode
  27.  
  28. # Loop through DNS records to check
  29. /ip dns static;
  30. :foreach i in=[find where name ~ (".*\\.".$zone) ] do={
  31. :set hostname [ get $i name ];
  32. :set hostname [ :pick $hostname 0 ( [ :len $hostname ] - ( [ :len $zone ] + 1 ) ) ];
  33.  
  34. /ip dhcp-server lease;
  35. :set dhcpnode [ find where host-name=$hostname ];
  36. :if ( [ :len $dhcpnode ] > 0) do={
  37. :log debug ("Lease for ".$hostname." still exists. Not deleting.");
  38. # :put ("Lease for ".$hostname." still exists. Not deleting.");
  39. } else={
  40. # there's no lease by that name. Maybe this mac has a static name.
  41. :local found false
  42. /system script environment
  43. :foreach n in=[ find where name ~ "shost[0-9A-F]+" ] do={
  44. :if ( [ get $n value ] = $hostname ) do={
  45. :set found true;
  46. }
  47. }
  48. :if ( found ) do={
  49. :log debug ("Hostname ".$hostname." is static");
  50. :put ("Hostname ".$hostname." is static");
  51. } else={
  52. :log info ("Lease expired for ".$hostname.", deleting DNS entry.");
  53. :put ("Lease expired for ".$hostname.", deleting DNS entry.");
  54. /ip dns static remove $i;
  55. }
  56. }
  57. }
  58.  
  59. /ip dhcp-server lease;
  60. # :foreach i in=[find] do={
  61. :foreach i in=[find where disabled=no] do={
  62. :set hostname ""
  63. :local mac
  64. :set dhcpip [ get $i address ];
  65. :set mac [ get $i mac-address ];
  66. :while ($mac ~ ":") do={
  67. :local pos [ :find $mac ":" ];
  68. :set mac ( [ :pick $mac 0 $pos ] . [ :pick $mac ($pos + 1) 999 ] );
  69. };
  70. :foreach n in=[ /system script environment find where name=("shost" . $mac) ] do={
  71. :set hostname [ /system script environment get $n value ];
  72. }
  73. :if ( [ :len $hostname ] = 0) do={
  74. :set hostname [ get $i host-name ];
  75. }
  76. :if ( [ :len $hostname ] > 0) do={
  77. :set hostname ( $hostname . "." . $zone );
  78. /ip dns static;
  79. :set dnsnode [ find where name=$hostname ];
  80. :if ( [ :len $dnsnode ] > 0 ) do={
  81. # DNS record exists. Is its IP the same?
  82. :set dnsip [ get $dnsnode address ];
  83. :if ( $dnsip = $dhcpip ) do={
  84. :log debug ("DNS entry for " . $hostname . " does not need updating.");
  85. } else={
  86. :put ("STOP")
  87. # However, only replace if the new host responds to pings, to ensure
  88. # a dead address isn't replacing a live one
  89. :if ($pingCount > 0) do={
  90. :put ("Checking " . $hostname . " with ping before replacing DNS entry");
  91. # Ping new ip we are attempting to add to DNS record
  92. :local pNew [/ping $dhcpip count=$pingCount];
  93. }
  94.  
  95. :if ($pingCount = 0 || $pNew = $pingCount) do={
  96. :log info ("Replacing existing DNS entry for " . $hostname);
  97. :put ("Replacing existing DNS entry for " . $hostname);
  98. /ip dns static remove $dnsnode;
  99. /ip dns static add name=$hostname address=$dhcpip ttl=$ttl comment="dhcp autogenerated";
  100. }
  101. }
  102. } else={
  103. # DNS Record doesn't exist. Add it
  104. :log info ("Adding new DNS entry for " . $hostname);
  105. :put ("Adding new DNS entry for " . $hostname);
  106. /ip dns static add name=$hostname address=$dhcpip ttl=$ttl comment="dhcp autogenerated";
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement