Last active
August 29, 2015 14:02
-
-
Save tacitochaves/8a30028cdadc11b8c866 to your computer and use it in GitHub Desktop.
Update DNS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# | |
# update_dns.pl | |
# | |
# This program is responsible for performing the backup of the firewall. | |
# | |
# Author: Tácito Chaves | |
# Contact: (98) 8123 - 8153 / (98) 8857 - 9777 | |
# E-mail: chaves@tchaves.com.br | |
use strict; | |
use warnings; | |
use LWP::Simple; | |
# constants | |
use constant { | |
TMPDIR => '/tmp', | |
NSFILE => '/tmp/ddns.ip' | |
}; | |
# getting ip | |
my $ip = get_ip(); | |
# check | |
if ( !-e NSFILE ) { | |
open my $fh, '>', NSFILE or die "Error: $!"; | |
print $fh "0.0.0.0"; | |
close $fh; | |
} | |
my $nsfile; | |
open my $fhreader, NSFILE or die "Error reading: $!"; | |
$nsfile = join( '', <$fhreader> ); | |
chomp $nsfile; | |
close $fhreader; | |
# ip not equals | |
if ( $nsfile ne $ip ) { | |
# TODO coloca aqui o update do ip | |
my $keyname = "tchaves"; | |
my $hash = "BAb8A39m6gXtmraR1iqUkbnrBNer3vSnZ1T1+HC9djnY0RdJ0+eaYswsB9m5oMkQ56lV6DdFrI9jQ8nnz2VvkQ=="; | |
my $ns_server = "ns1.tchaves.com.br"; | |
my $zone = "din.tchaves.com.br"; | |
my $host = "teste.din.tchaves.com.br"; | |
my $nsupdate = qx(/usr/bin/nsupdate -y $keyname:$hash); | |
my $exec = qx(server $ns_server\n | |
zone $zone\n | |
update delete $host A\n | |
update add $host 60 A $ip\n | |
send | |
quit); | |
open my $fh, '>', NSFILE or die "Error: $!"; | |
print $fh "$ip"; | |
close $fh; | |
} | |
else { | |
print "Endereço de IP não mudou!\n"; | |
} | |
sub get_ip { | |
my $html = get "http://meuip.datahouse.com.br"; | |
if ($html) { | |
$html =~ m/(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})/; | |
return $1 if $1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment