Skip to content

Instantly share code, notes, and snippets.

@terminalfool
Last active May 1, 2019 00:24
Show Gist options
  • Save terminalfool/11218330 to your computer and use it in GitHub Desktop.
Save terminalfool/11218330 to your computer and use it in GitHub Desktop.
Namecheap ddns updater. A self-contained version of http://search.cpan.org/~dwatson/App-DDNS-Namecheap/
#!/usr/bin/env perl
package App::DDNS::Namecheap;
use strict;
use warnings;
use Exporter;
use LWP::Simple qw($ua get);
$ua->agent("");
use Mozilla::CA;
sub new {
my ( $class, %self ) = @_;
my $self = { %self };
bless $self, $class;
}
sub update {
my $self = shift;
foreach ( "domain", "password", "hosts" ) { die "App::DDNS::Namecheap: $_ undefined" unless $self->{ $_ } }
foreach ( @{ $self->{hosts} } ) {
my $url = "https://dynamicdns.park-your-domain.com/update?domain=$self->{domain}&password=$self->{password}&host=$_";
if ( my $return = get($url) ) {
unless ( $return =~ /<errcount>0<\/errcount>/is ) {
$return = ( $return =~ /<responsestring>(.*)<\/responsestring>/is ? $1 : "unknown error" );
print "failure submitting host \"$_\.$self->{domain}\": $return\n";
}
}
}
}
package main;
my $timeout = 5; # 5 minute timeout
$timeout *= 60;
my $domain = App::DDNS::Namecheap->new(
domain => 'mysite.org',
password => 'abcdefghijklmnopqrstuvwxyz012345',
hosts => [ "@", "www", "*" ],
);
# my $domain2 = App::DDNS::Namecheap->new(
# domain => 'mysite2.org',
# password => 'abcdefghijklmnopqrstuvwxyz012345',
# hosts => [ "@", "www", "*" ],
#);
while (1) {
$domain->update();
# $domain2->update();
sleep ($timeout);
}
=head1 NAME
namecheap_ddns.pl - command line stub
=head1 SYNOPSIS
perl namecheap_ddns.pl
=head1 DESCRIPTION
Script for Namecheap dynamic dns updates.
=head1 AUTHOR
David Watson <dwatson@cpan.org>
=head1 SEE ALSO
App::DDNS::Namecheap on the CPAN
=head1 COPYRIGHT
This library is free software. You can redistribute it and/or modify it under the
same terms as Perl itself.
=cut
install as daemon on osx using launchctl: launchctl load -w /library/launchdaemons/us.dustbin.ddns.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>us.dustbin.ddns</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/namecheap_ddns</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment