SHARE
TWEET

CloudFlare/DynDNS Updater

a guest Jul 1st, 2011 363 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl -w
  2. # This is a handy script for keeping your CloudFlare DNS & DynDNS records up to date using their respective api's.
  3. # Run it in a cron job, run it at start up, run it whenever, wherever.
  4.  
  5. use strict;
  6.  
  7. ##########
  8. package CloudFlare;
  9.  
  10. use LWP::UserAgent;
  11.  
  12. sub new {
  13.     my ( $class, $url, $domains, $user, $token ) = @_;
  14.     my ($self) = {};
  15.     bless( $self, $class );
  16.  
  17.     $self->{'_url'} = $url;
  18.  
  19.     my $ua = LWP::UserAgent->new;
  20.     $ua->agent('Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.1;)');
  21.     $self->{'_ua'} = $ua;
  22.  
  23.     my $ip;
  24.     $self->{'_ip'} = $ip;
  25.  
  26.     my $api =
  27.         'https://www.cloudflare.com/api.html?a=DIUP'
  28.       . '&hosts='
  29.       . $domains . '&u='
  30.       . $user . '&tkn='
  31.       . $token;
  32.     $self->{'_api'} = $api;
  33.  
  34.     return $self;
  35. }
  36.  
  37. sub main {
  38.     my ($self) = shift;
  39.     $self->{'_ip'} = $self->getIp();
  40.     $self->callApi();
  41. }
  42.  
  43. sub getIp {
  44.     my ($self) = shift;
  45.     my $response = $self->{'_ua'}->get( $self->{'_url'} );
  46.     if ( $response->is_success ) {
  47.         my @ip =
  48.           ( $response->decoded_content =~
  49. m/((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3})/g
  50.           );
  51.         return $ip[0];
  52.     }
  53.     else {
  54.         die $response->status_line;
  55.     }
  56. }
  57.  
  58. sub callApi {
  59.     my ($self) = shift;
  60.     my $response =
  61.       $self->{'_ua'}->get( $self->{'_api'} . '&ip=' . $self->{'_ip'} );
  62.     if ( $response->is_success ) {
  63.         die $response->decoded_content;
  64.     }
  65.     else {
  66.         die $response->status_line;
  67.     }
  68. }
  69.  
  70. ##########
  71. package DynDNS;
  72.  
  73. use LWP::UserAgent;
  74. use Socket;
  75.  
  76. sub new {
  77.     my ( $class, $url, $domain, $user, $pass ) = @_;
  78.     my ($self) = {};
  79.     bless( $self, $class );
  80.  
  81.     $self->{'_url'}    = $url;
  82.     $self->{'_domain'} = $domain;
  83.  
  84.     my $ua = LWP::UserAgent->new;
  85.     $ua->agent('Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.1;)');
  86.     $self->{'_ua'} = $ua;
  87.  
  88.     my $ip;
  89.     $self->{'_ip'} = $ip;
  90.  
  91.     my $api =
  92.         'https://'
  93.       . $user . ':'
  94.       . $pass . '@'
  95.       . 'members.dyndns.org/nic/update?wildcard=NOCHG&mx=NOCHG&backmx=NOCHG'
  96.       . '&hostname='
  97.       . $domain;
  98.     $self->{'_api'} = $api;
  99.  
  100.     return $self;
  101. }
  102.  
  103. sub main {
  104.     my ($self) = shift;
  105.     $self->{'_ip'} = $self->getIp();
  106.     if ( $self->{'_ip'} != inet_ntoa( inet_aton( $self->{'_domain'} ) ) ) {
  107.         $self->callApi();
  108.     }
  109. }
  110.  
  111. sub getIp {
  112.     my ($self) = shift;
  113.     my $response = $self->{'_ua'}->get( $self->{'_url'} );
  114.     if ( $response->is_success ) {
  115.         my @ip =
  116.           ( $response->decoded_content =~
  117. m/((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3})/g
  118.           );
  119.         return $ip[0];
  120.     }
  121.     else {
  122.         die $response->status_line;
  123.     }
  124. }
  125.  
  126. sub callApi {
  127.     my ($self) = shift;
  128.     my $response =
  129.       $self->{'_ua'}->get( $self->{'_api'} . '&myip=' . $self->{'_ip'} );
  130.     if ( $response->is_success ) {
  131.         die $response->decoded_content;
  132.     }
  133.     else {
  134.         die $response->status_line;
  135.     }
  136. }
  137.  
  138. ##########
  139. package main;
  140. my $cloudFlare = CloudFlare->new(
  141.     'http://checkip.dyndns.com/', 'example.com,www.example.com',
  142.     'email@example.com',          '_CLOUDFLARE_API_KEY_'
  143. );
  144. $cloudFlare->main();
  145. undef $cloudFlare;
  146.  
  147. my $dyndns = DynDNS->new(
  148.     'http://checkip.dyndns.com/', 'example.dyndns.com',
  149.     '_DYNDNS_USER_',              '_DYNDNS_PASSWORD_'
  150. );
  151. $dyndns->main();
  152. undef $dyndns;
RAW Paste Data
Top