Posted to tcl by stever at Mon Jul 05 15:53:39 GMT 2010view raw
- proc dnsupdate {fqdname ipaddr local_ip} {
- puts "$fqdname * $ipaddr * $local_ip"
- set hostname [lindex [split $fqdname .] 0]
- if {$hostname == $fqdname} {
- set domain $::defaultdomain
- } else {
- set domain [string map {" " .} [lrange [split $fqdname .] 1 end]]
- }
- set ipchanged 0
- puts "hostname=$hostname domain=$domain"
- if {! [file exists /var/named/${domain}]} {
- set outfile [open "/var/named/${domain}" "CREAT WRONLY"]
- puts $outfile "\$TTL\t60\n@\t\tIN\tSOA\t$::dnsserver.\t$::hostmaster.\t\
- (\n\t\t\t1024 ; serial\n\t\t\t1M ; refresh\n\t\t\t1M ; retry\n\
- \t\t\t1M ; expire\n\t\t\t1M ; default_ttl\n\t\t\t)"
- close $outfile
- set ipchanged 1
- }
-
- set infile [open "/var/named/${domain}" RDWR]
- file delete "/var/named/${domain}.new"
-
- set newhostfiledata ""
-
- while {[gets $infile line] >= 0} {
- lappend filelist $line
- }
-
- set entryexists 0
-
- set listlines [llength $filelist]
- for {set i 0} {$i < $listlines} {incr i 1} {
- set origline [lindex $filelist $i]
- regsub -all {\t} [lindex $filelist $i] { } currline
- set currlinelist [split $currline ]
-
- if {[lindex $currlinelist 5] == "serial"} {
- set newserial [expr [lindex $currlinelist 3]+1]
- append newhostfiledata "\t\t\t$newserial ; serial\n"
- } elseif {[lindex $currlinelist 0] == $hostname} {
- set entryexists 1
- if {[string first $ipaddr $currlinelist] == -1} {set ipchanged 1}
- } elseif {[lindex $currlinelist 0] == "x$hostname" && $local_ip != ""} {
- append newhostfiledata "x$hostname\t\tIN\tA\t$local_ip\n"
- if {[string first $local_ip $currlinelist] == -1} {set ipchanged 1}
- } else {
- append newhostfiledata "$origline\n"
- }
- }
-
-
-
- set logfile [open $::logfile "CREAT APPEND WRONLY"]
-
- if {$ipchanged == 1 || ! $entryexists} {
- puts "updating record"
- append newhostfiledata "$hostname\t\tIN\tA\t$ipaddr\n"
- if {$local_ip != ""} {append newhostfiledata "x$hostname\t\tIN\tA\t$local_ip\n"}
-
- set outfile [open "/var/named/${domain}.new" "TRUNC CREAT WRONLY"]
- puts -nonewline $outfile $newhostfiledata
- close $outfile
- file rename -force "/var/named/${domain}" "/var/named/${domain}.bak"
- file rename -force "/var/named/${domain}.new" "/var/named/${domain}"
- puts $logfile "[clock format [clock seconds] -format "%D %T"] $hostname set to $ipaddr local ip $local_ip"
- #reload named as done on RedHat
- #catch {exec service named reload}
- set fh [open /var/run/named/named.pid RDONLY]
- set namedpid [string trim [read $fh]]
- close $fh
- set command "kill -HUP $namedpid"
- catch {eval exec $command &}
- } else {
- puts $logfile "[clock format [clock seconds] -format "%D %T"] $hostname unchanged"
- }
-
- close $logfile
- close $infile
-
- return $ipchanged
- }
-