Skip to content

Instantly share code, notes, and snippets.

@tacitochaves
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save tacitochaves/eb02c786a2a0f000a970 to your computer and use it in GitHub Desktop.

Select an option

Save tacitochaves/eb02c786a2a0f000a970 to your computer and use it in GitHub Desktop.
Updating records with dynamic dns TSIG
#!/usr/bin/perl
#
# This program will include a record in DNS for hosts with dynamic link.
#
# Author: Tácito Chaves - 2014-06-05
# e-mail: [email protected]
# fone: (98) 8123-8153 / 8857-9777
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 ) {
# ALL arises here the update of the 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";
# ADD register in the dns with use of pipe!
open my $pipe, "| nsupdate -d " or die "Cannot open nsupdate: $!";
print $pipe "server $ns_server\n";
print $pipe "zone $zone\n";
print $pipe "key $keyname $hash\n";
print $pipe "update delete $host A\n";
print $pipe "update add $host 60 A $ip\n";
print $pipe "send\n";
print $pipe "quit\n";
close $pipe;
# Writing information in the file
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