IPv4 Fremnet Logo
TOOLS, TINKERINGS & CODE

Do you use my code?

DHS.org Auto-Update Script · Mar 13, 22:15 by Shannon Wynter

Ok, so you may or may not be using your DSL connection for hosting a website, or… perhaps you’d just like to be able to log into your boxes from the other side of the world

Anyhow, this perl script will help you keep a DHS.org domain up to date.

  1. #!/usr/bin/perl
  2. ###############################################
  3. # pl Update DHS                               #
  4. ###############################################
  5. # By Freman                                   #
  6. #         http://fremnet.net/contact          #
  7. ###############################################
  8. # For those of you who are woundering, DHS    #
  9. # was a free domain service, but now requires #
  10. # donation. This script is was started        #
  11. # because the most reliable shell script that #
  12. # was availible didn't work as well as I      #
  13. # would have hoped.                           #
  14. ###############################################
  15. # Usage:                                      #
  16. #        Fill in the details, and chuck it in #
  17. #   your crontab, it's as simple as that      #
  18. # Features:                                   #
  19. #        Won't try to update unless your IP   #
  20. #   has changed (or the cache file is erased) #
  21. # More Information:                           #
  22. #        Visit: www.dhs.org                   #
  23. ###############################################
  24. # Oh, and before you email me saying I could  #
  25. # use sockets or something similar, I'm going #
  26. # to jump in and say NOPE, I'm happy with the #
  27. # way I've done it. It's much cleaner then    #
  28. # the original SH script.                     #
  29. ###############################################
  30.   
  31. # What interface should I be probing for an IP?
  32. $Interface  = "ppp0";
  33.   
  34. $DHS_UserName = "theuser";
  35. $DHS_Password = "thepass";
  36. $DHS_Host = "thehost";
  37. $DHS_Domain = "dhs.net";
  38.   
  39. #Example
  40. #$DHS_UserName = "methegreat";
  41. #$DHS_Password = "mypassword";
  42. #$DHS_Host = "box";
  43. #$DHS_Domain = "dhs.org";
  44.   
  45. $Log_Enabled = 1;
  46.   
  47. #--== You shouldn't need to edit below here ==--#
  48.   
  49. # File to cache the last known IP in
  50. # Note: $DHS_Host gets appended to this.
  51. $IP_Cache_File = "/var/run/ip.cache";
  52.   
  53. $ToolToUse = "/sbin/ifconfig";
  54.   
  55. $IP_Address = &Get_IP;
  56. $Cached_IP = &Get_Cached_IP;
  57.   
  58. if (not($IP_Address eq $Cached_IP)) {
  59. #THE IP ADDRESS HAS CHANGED!!!
  60. use LWP::UserAgent;
  61. $ua = LWP::UserAgent->new;
  62. $ua->agent("plUpdateDHS/0.1");
  63.   
  64. $req = HTTP::Request->new(GET => "http://members.dhs.org/nic/hosts?hostscmd=edit&hostscmdstage=2&type=4&domain=".$DHS_Domain);
  65. $req->authorization_basic($DHS_UserName, $DHS_Password);
  66. $ua->request($req)->as_string;
  67.   
  68. if ($Log_Enabled == 1) {
  69.   open(LOG,">>/var/log/updatedhs");
  70.   print LOG "[".localtime(time)."] ($DHS_Host.$DHS_Domain) Address Change : $IP_Address\n";
  71.   close(LOG);
  72. }
  73. open(C,">$IP_Cache_File.$DHS_Host") || die "I can't open the IP Cache File: $1";
  74. print C "$IP_Address\n";
  75. close(C);
  76. system('/scripts/checkhost.pl &');
  77. }
  78.   
  79. # Function to whip the current ip from thin air.
  80. sub Get_IP {
  81. my($IP) = '0.0.0.0';
  82. my($Line);
  83. open (IFINFO,"$ToolToUse  $Interface |");
  84. while (<IFINFO>) {
  85.   chomp;
  86.   $Line = $_;
  87.   if ($Line =~ /(\d*\.\d*\.\d*\.\d*)/) {
  88.   $IP = $1;
  89.   }
  90. }
  91. if ($IP eq "0.0.0.0") {
  92.   die "I cannoh get the ip address capt`n\n";
  93. }
  94. close(IFINFO);
  95. return($IP);
  96. }
  97.  
  98. # Function to pull the IP stored in the cache.
  99. sub Get_Cached_IP {
  100. my($IP) = '-.-.-.-';
  101. my($Line);
  102. if (-e "$IP_Cache_File.$DHS_Host") {
  103.   open (CACHE,"<$IP_Cache_File.$DHS_Host");
  104.   while (<CACHE>) {
  105.   chomp;
  106.   $Line = $_;
  107.   if ($Line =~ /(\d*\.\d*\.\d*\.\d*)/) {
  108.     $IP = $1;
  109.   }
  110.   }
  111.   close(CACHE);
  112. }
  113. return($IP);
  114. }
  115. Download this code: dhsupdate.pl (Downloaded 236 time(s))
Comments

Spam no more - rel=nofollow is active here, spamming my comments will not help your page rank.

  Textile help
---== Copyright Shannon Wynter - All rights reserved - All wrongs avenged ==--- contact