Skip to content

Instantly share code, notes, and snippets.

@ardavey
Last active December 20, 2015 20:59
Show Gist options
  • Save ardavey/6194007 to your computer and use it in GitHub Desktop.
Save ardavey/6194007 to your computer and use it in GitHub Desktop.
A small Perl script to automatically log in to dyndns.org, to avoid the recent requirement to log in to the website once every 30 days. Designed to either be run regularly from cron, or manually (but that kind of defeats the purpose).
#!/usr/bin/perl -w
use strict;
# A small script to automatically log in to dyndns.org
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
$mech->agent_alias( 'Windows IE 6' ); # honest, guv!
my $url = 'https://account.dyn.com/';
my $creds = get_credentials();
$mech->get( $url );
my $form_id = undef;
# Find the relevant form on the page. For "security", the ID of the form changes every
# time the page loads, but it's always of the form 'loginN', where N is one or more numerics
foreach my $form ( $mech->forms() ) {
( $form_id ) = $form->attr( 'id' ) =~ m/(^login\d+$)/;
last if ( $form_id );
}
if ( $form_id ) {
print "Found form with id '$form_id'. Submitting...\n";
}
else {
die "login form not found";
};
$mech->submit_form(
form_id => $form_id,
fields => {
username => $creds->{user},
password => $creds->{pass},
},
);
# The account page shows "Welcome&nbsp;<b>username</b>" - we'll just
# check for the name to see if we've managed to log in
if ( $mech->content() =~ m!<b>$creds->{user}</b>!x ) {
print "Logged in successfully!\n";
}
else {
print "Failed to log in.\n";
}
# In this implementation, dyndns account credentials are stored in a file with restricted permissions.
# It's just a text file containing a single line of the form:
#
# $_ = { user => 'myUsername', pass => 'myPassword' }
#
# Feel free to hardcode these in the script, if you're not too fussed about security.
sub get_credentials {
if ( open( CREDS, '/path/to/.dyndns-creds' ) ) {
return eval <CREDS>;
}
else {
die "Failed to get credentials: $!";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment