Public feed #603518

Miscellany

public text v1 · immutable
Raw Edit
·published 2007-07-04 21:15 UTC
rendered paste body
#!/usr/bin/perl

use POSIX qw(strftime);
###CONFIG###
my $ext_if = "eth1";
my $log = "/home/bitrot/dynip";
my $zone = "/home/bitrot/pengu.in";
###END CONFIG###
my $cur_ip = `/sbin/ifconfig $ext_if|grep inet|grep -v inet6|awk '{print \$2}'|sed -e 's|addr:||'`;
my $now_time =  strftime "%m-%d-%Y-%H:%M:%S", localtime;
my $noup=0;

chomp($cur_ip);

if (!-e $log) {
        `touch $log`;
        open(WRITE, ">$log") or die "$file cannot be opened for writing.";
        printf WRITE "$now_time" . "->$cur_ip\n";
        close(WRITE);
}

open(READ, "<$log") or die "$file cannot be opened .";
while (<READ>) {
        $_      =~ m/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/;
        $old_ip = $1;
        if ("$1" eq "$cur_ip") {
                $noup=1;
        } else {
                $noup=0;
                open(WRITE, ">$log") or die "$file cannot be opened.";
                printf WRITE "$now_time" . "->$cur_ip\n";
                close(WRITE);
        }
}
close(READ);

if ($noup == 1) {
        print "IP has not changed.";
} else {
        print "Old IP: $old_ip\nNew IP: $cur_ip\nUpdating DNS Zone.\n";
        my $oip_exp = $old_ip;
        $oip_exp =~ s/\./\\./g;
        open(ZONE, "<$zone") or die "$zone cannot be opened for reading.";
        $nip = "$cur_ip\n";
        while (<ZONE>) {
                $_ =~ s/$oip_exp/$nip/g;
                push(@zdata, "$_");
        }
        close(ZONE);
        open(WZONE, ">$zone") or die "$zone cannot be opened for writing.";
        foreach (@zdata) {
                printf WZONE $_;
        }
        close(WZONE);
}