Posted to tcl by stever at Mon Jul 05 15:53:39 GMT 2010view raw

  1. proc dnsupdate {fqdname ipaddr local_ip} {
  2. puts "$fqdname * $ipaddr * $local_ip"
  3. set hostname [lindex [split $fqdname .] 0]
  4. if {$hostname == $fqdname} {
  5. set domain $::defaultdomain
  6. } else {
  7. set domain [string map {" " .} [lrange [split $fqdname .] 1 end]]
  8. }
  9. set ipchanged 0
  10. puts "hostname=$hostname domain=$domain"
  11. if {! [file exists /var/named/${domain}]} {
  12. set outfile [open "/var/named/${domain}" "CREAT WRONLY"]
  13. puts $outfile "\$TTL\t60\n@\t\tIN\tSOA\t$::dnsserver.\t$::hostmaster.\t\
  14. (\n\t\t\t1024 ; serial\n\t\t\t1M ; refresh\n\t\t\t1M ; retry\n\
  15. \t\t\t1M ; expire\n\t\t\t1M ; default_ttl\n\t\t\t)"
  16. close $outfile
  17. set ipchanged 1
  18. }
  19.  
  20. set infile [open "/var/named/${domain}" RDWR]
  21. file delete "/var/named/${domain}.new"
  22.  
  23. set newhostfiledata ""
  24.  
  25. while {[gets $infile line] >= 0} {
  26. lappend filelist $line
  27. }
  28.  
  29. set entryexists 0
  30.  
  31. set listlines [llength $filelist]
  32. for {set i 0} {$i < $listlines} {incr i 1} {
  33. set origline [lindex $filelist $i]
  34. regsub -all {\t} [lindex $filelist $i] { } currline
  35. set currlinelist [split $currline ]
  36.  
  37. if {[lindex $currlinelist 5] == "serial"} {
  38. set newserial [expr [lindex $currlinelist 3]+1]
  39. append newhostfiledata "\t\t\t$newserial ; serial\n"
  40. } elseif {[lindex $currlinelist 0] == $hostname} {
  41. set entryexists 1
  42. if {[string first $ipaddr $currlinelist] == -1} {set ipchanged 1}
  43. } elseif {[lindex $currlinelist 0] == "x$hostname" && $local_ip != ""} {
  44. append newhostfiledata "x$hostname\t\tIN\tA\t$local_ip\n"
  45. if {[string first $local_ip $currlinelist] == -1} {set ipchanged 1}
  46. } else {
  47. append newhostfiledata "$origline\n"
  48. }
  49. }
  50.  
  51.  
  52.  
  53. set logfile [open $::logfile "CREAT APPEND WRONLY"]
  54.  
  55. if {$ipchanged == 1 || ! $entryexists} {
  56. puts "updating record"
  57. append newhostfiledata "$hostname\t\tIN\tA\t$ipaddr\n"
  58. if {$local_ip != ""} {append newhostfiledata "x$hostname\t\tIN\tA\t$local_ip\n"}
  59.  
  60. set outfile [open "/var/named/${domain}.new" "TRUNC CREAT WRONLY"]
  61. puts -nonewline $outfile $newhostfiledata
  62. close $outfile
  63. file rename -force "/var/named/${domain}" "/var/named/${domain}.bak"
  64. file rename -force "/var/named/${domain}.new" "/var/named/${domain}"
  65. puts $logfile "[clock format [clock seconds] -format "%D %T"] $hostname set to $ipaddr local ip $local_ip"
  66. #reload named as done on RedHat
  67. #catch {exec service named reload}
  68. set fh [open /var/run/named/named.pid RDONLY]
  69. set namedpid [string trim [read $fh]]
  70. close $fh
  71. set command "kill -HUP $namedpid"
  72. catch {eval exec $command &}
  73. } else {
  74. puts $logfile "[clock format [clock seconds] -format "%D %T"] $hostname unchanged"
  75. }
  76.  
  77. close $logfile
  78. close $infile
  79.  
  80. return $ipchanged
  81. }
  82.