#!/sw/bin/perl $self = $0; $self =~ s!^.*/!!; $target = "/etc/hosts"; $mnt_target = "/mnt"; $mnt_source = "sunbi1:/s/tftpboot/host"; $rc = system("/usr/etc/mount $mnt_source $mnt_target") >> 8; if ($rc) { die "$self: can't mount '$mnt_source' to '$mnt_target': $rc\n"; } unless (opendir(HOSTS, $mnt_target)) { die "$self: can't read directory '$mnt_source': $!\n"; } @hosts = sort readdir(HOSTS); closedir(HOSTS); foreach $file (@hosts) { next if ($file eq '.'); next if ($file eq '..'); $host=''; $alias=''; $domain=''; $hostaddr=''; $hostname=''; unless (open(HOST, "<$mnt_target/$file")) { warn "$self: can't read '$mnt_source/$file': $!\n"; next; } while () { chop; ($var, $value) = split(/\s*=\s*/); if (($var eq "ip-nr") && ($value =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/)) { $host = sprintf("%.2X%.2X%.2X%.2X", $1, $2, $3, $4); $hostaddr = sprintf("%d.%d.%d.%d", $1, $2, $3, $4); } if ($var eq "hostname") { $hostname=$value; } if ($var eq "domain") { $domain=$value; } if ($var eq "alias") { $alias=$value; } } close(HOST); if (($host ne '') && ($hostname ne '')) { if (($domain ne '') && ($hostname !~ /\./)) { $hostname = "$hostname.$domain $hostname"; # $hostname .= " $hostname.$domain"; } if (defined $addr{$host}) { warn "$self: $addr{$host} ($name{$host}) redefined as '$hostname' in '$file'!\n"; if ($alias{$host} ne '') { $alias{$host} .= " $hostname"; } else { $alias{$host} = $hostname; } } else { $addr{$host} = $hostaddr; $name{$host} = $hostname; $alias{$host} = $alias; } } } undef @hosts; $rc = system("/usr/etc/umount $mnt_target") >> 8; if ($rc) { die "$self: can't unmount '$mnt_source' at '$mnt_target': $rc\n"; } $ext = '000'; $backup = "$target.$ext"; while (-e $backup) { $ext++; $backup = "$target.$ext"; } unless (open(OLDHOSTS, "<$target")) { die "$self: can't read '$target': $!\n"; } unless (rename($target,$backup)) { die "$self: can't rename '$target' to '$backup': $!\n"; } unless (open(NEWHOSTS, ">$target")) { die "$self: can't write '$target': $!\n"; } while () { last if /DO NOT MODIFY OR RELOCATE THIS LINE/; print NEWHOSTS; } close(OLDHOSTS); print NEWHOSTS <<"VERBATIM"; # DO NOT MODIFY OR RELOCATE THIS LINE - # it serves as an anchor for the automatic generation of what follows! # # Entries below are generated automatically. # DO NOT EDIT THE SECTION BELOW! CHANGES WILL BE LOST! # Edit the source files in '$mnt_source' instead! # ####################################### # VERBATIM foreach $host (sort keys(%name)) { if ($alias{$host} ne '') { $name{$host} .= " $alias{$host}"; } printf(NEWHOSTS "%-16s %s\n", $addr{$host}, $name{$host}); } close(NEWHOSTS); __END__