Skip to content

Instantly share code, notes, and snippets.

@typhonius
Created July 6, 2015 13:12
Show Gist options
  • Save typhonius/deb5aa0470093ab7c0a7 to your computer and use it in GitHub Desktop.
Save typhonius/deb5aa0470093ab7c0a7 to your computer and use it in GitHub Desktop.
Script that takes the current IP address of a server and updates its record with Google Domains
#!/usr/bin/env perl
use strict;
use warnings;
use HTTP::Request;
use LWP::UserAgent;
use Regexp::Common qw /net/;
use JSON;
use Socket;
my $username = '<FILLME>';
my $password = '<FILLME>';
my $hostname = '<FILLME>';
my $ua = LWP::UserAgent->new;
# Get the current external IP according to checkip.dyndns.org
my $ipreq = HTTP::Request->new(GET => 'http://checkip.dyndns.org');
my $ipres = $ua->request($ipreq);
my $ip = '';
if ($ipres->is_success) {
if ($ipres->content =~ /$RE{net}{IPv4}{-keep}/) {
$ip = $1;
}
}
else {
die ($ipres->content);
}
my $address = inet_ntoa(inet_aton($hostname));
# Print the time so we have a point of reference
print time() . "\n";
unless ($address =~ $ip) {
my $api = "https://domains.google.com/nic/update?hostname=${hostname}&myip=$ip";
my $req = HTTP::Request->new(GET => $api);
$req->authorization_basic($username, $password);
my $res = $ua->request($req);
if ($res->is_success) {
print $res->content . "\n";
}
else {
die ($res->content);
}
}
else {
print "No update needed\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment