Skip to content

Instantly share code, notes, and snippets.

@alexandreaquiles
Created October 18, 2014 01:53
Show Gist options
  • Save alexandreaquiles/d636059bb279df2e7946 to your computer and use it in GitHub Desktop.
Save alexandreaquiles/d636059bb279df2e7946 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
#
# SSL Client for DtDNS Version 1.0 06/01/01
# SSL Client for DtDNS Version 1.2 11/25/02
# SSL Client for DtDNS Version 1.3 10/17/14
# R.W. (Bob) Hughes
# Modified to check against nslookup by Kevin Killingsworth
# ver 1.2 added test for "not valid" hostname
# ver 1.3 changed external ip and dns lookup (Alexandre Aquiles)
#
# To add Net::SSLeay and LWP::Simple
# perl -MCPAN -eshell;
# cpan> install Net::SSLeay
# cpan> install Bundle::LWP
#========================================================================
require 5.004;
use Socket;
use Net::SSLeay qw(die_now die_if_ssl_error);
use Sys::Hostname;
use LWP::Simple;
use strict;
#========================================================================
if ($#ARGV < 0) {
print "\nUsage:\n\n";
print " $0\t\tPrints this screen\n";
print " $0 up\tSends our current IP\n";
print " $0 down\tSends 0.0.0.0 to put this host off line\n";
print " $0 sync\tChecks NS IP against current IP and updates if needed\n";
exit(1);
}
#========================================================================
#
# You will need to change these as needed
#
my $DtDNS_hostname="xxx"; # Your DtDNS hostname
my $DtDNS_passwd="xxxxx"; # Your DtDNS password
my $DtDNS_domain="dtdns.net"; # Your DtDNS domain
my $Ext_port="eth0"; # Your Interface to the Big I
#========================================================================
my $remote = 'www.dtdns.com';
my $port = 443;
#========================================================================
#
# Set the IP to either the IP of the Internet facing Interface or 0.0.0.0
# for off line notification, or check to see if they match and then
# set if they don't.
#
#
# Get the External IP
# (the real IP)
#
my ($My_ip);
chomp(my $My_ip = get("http://icanhazip.com/"));
die "Couldn't get external IP" unless defined $My_ip;
print "Current external IP: $My_ip\n";
#
# Get the NameServer IP
# (what the NameServer thinks the real IP is)
#
my $name = "$DtDNS_hostname.$DtDNS_domain";
my ($My_nsip);
$My_nsip = inet_ntoa(inet_aton($name)) or die "Couldn't resolve $name: $!\n";
print "Resolved '$name' to: $My_nsip\n";
#
# Now see what we need to be doing...
#
my $ToDo = shift(@ARGV);
$ToDo =~ tr/A-Z/a-z/;
if ($ToDo eq 'up' or $ToDo eq 'sync') {
if ($My_ip eq $My_nsip) {
# Do nothing, we've got everything right...
print "NameServer IP is current.... Exiting\n";
exit;
} else {
# Updatedate it...
print "NameServer IP is out of date, updating...\n";
}
} elsif ($ToDo eq 'down') {
$My_ip = "0.0.0.0"; # Set the ip to 'offline'...
} else {
print "The options are \"up\", \"down\" or \"sync\".... Exiting\n";
exit;
}
#========================================================================
#
# Message to send
#
my $msg="GET /api/autodns.cfm?id=$DtDNS_hostname&pw=$DtDNS_passwd&ip=$My_ip HTTP/1.1\nHost: www.dtdns.com\nUser-Agent: Hugheshut-ssl\n\n";
#========================================================================
#
# Strict likes these
#
my ($iaddr, $paddr, $ctx, $ssl, $res, $got);
#========================================================================
Net::SSLeay::load_error_strings();
Net::SSLeay::SSLeay_add_ssl_algorithms();
Net::SSLeay::randomize();
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
die "No port" unless $port;
$iaddr = inet_aton($remote) or die "No host: $remote";
$paddr = sockaddr_in($port, $iaddr);
print "Contacting DtDNS for update...\n";
socket(S, &AF_INET, &SOCK_STREAM, 0) or die "Can Not create Socket: $!\n";
connect(S, $paddr) or die "Can not connect to DtDNS: $!\n";
select(S);
$| = 1;
select(STDOUT);
$ctx = Net::SSLeay::CTX_new() or die_now("Failes to create SSL_CTX $!");
Net::SSLeay::CTX_set_options($ctx, &Net::SSLeay::OP_ALL)
and die_if_ssl_error("ssl ctx set options");
$ssl = Net::SSLeay::new($ctx) or die_now("Failed to create SSL $!");
Net::SSLeay::set_fd($ssl, fileno(S));
$res = Net::SSLeay::connect($ssl) and die_if_ssl_error("ssl connect:");
#
# Informational
#
#print "Cipher '" . Net::SSLeay::get_cipher($ssl) . "'\n";
# Now Write the message
$res = Net::SSLeay::write($ssl, $msg) or die_if_ssl_error("ssl write");
#print "Shutting down Socket\n";
shutdown S, 1;
while (1) {
$got = Net::SSLeay::read($ssl) or die_if_ssl_error("ssl read");
if ($got=~/Host / or $got=~/not valid/) {
print "$got\n";
last;
}
}
Net::SSLeay::free ($ssl);
Net::SSLeay::CTX_free ($ctx);
close (S) or die "close: $!";
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment