Advertisement
felmoltor

Autorenew noip.com hosts

Jul 22nd, 2013
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.41 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. # Summary:  The script avoid the need to manualy login every month in http://www.noip.com and update your domains to keep them alive.
  4. #           It will automaticaly retrieve his current public IP from http://checkip.dyndns.org/
  5. # Author: Felipe Molina (@felmoltor)
  6. # Date: July 2013
  7. # License: GPLv3
  8.  
  9. require 'date'
  10. require 'mechanize'
  11.  
  12. def getMyCurrentIP()
  13.     m = Mechanize.new
  14.     m.user_agent_alias = 'Windows IE 8'
  15.    
  16.     ip_page = m.get("http://checkip.dyndns.org/")
  17.     ip = ip_page.root.xpath("//body").text.gsub(/^.*: /,"")
  18.     return ip
  19. end
  20.  
  21. # ================
  22.  
  23. def setMyCurrentNoIP(user,password,my_public_ip)
  24.    
  25.     updated_hosts = []
  26.    
  27.     m = Mechanize.new
  28.     m.user_agent_alias = 'Windows IE 8'
  29.    
  30.     loginpage = m.get("https://www.noip.com/login/")
  31.    
  32.     members_page = loginpage.form_with(:id => 'clogs') do |form|
  33.         form.username = user
  34.         form.password = password
  35.     end.submit
  36.    
  37.     # Once successfuly loged in, access to DNS manage page
  38.     dns_page = m.get("https://www.noip.com/members/dns/")
  39.     dns_page.links_with(:text => "Modify").each do |link|
  40.         # Update all the domains with my current IP
  41.         update_host_page = m.click(link)
  42.         hostname = update_host_page.forms[0].field_with(:name => "host[host]").value
  43.         domain = update_host_page.forms[0].field_with(:name => "host[domain]").value
  44.         updated_hosts << "#{hostname}.#{domain}"
  45.         update_host_page.forms[0].field_with(:name => "host[ip]").value = my_public_ip
  46.         update_host_page.forms[0].submit
  47.     end
  48.    
  49.     return updated_hosts
  50. end
  51.  
  52. # ================
  53.  
  54. puts "======= #{Date.today.to_s} ========"
  55.  
  56. if ARGV[0].nil? or ARGV[1].nil?
  57.     puts "Error. Especifica el usuario y la contrasenna para acceder a tu cuenta de noip.com"
  58.     exit(1)
  59. else
  60.     user = ARGV[0]
  61.     password = ARGV[1] 
  62.     # domain_to_update = ARGV[3] if !ARGV[3].nil? # TODO: If you want to keep alive only one domain, specify it
  63.    
  64.     puts "Getting my current public IP..."
  65.     my_public_ip = getMyCurrentIP()
  66.     puts "Done: #{my_public_ip}"
  67.     puts "Sending Keep Alive request to noip.com..."
  68.     updated_hosts = setMyCurrentNoIP(user,password,my_public_ip)   
  69.     if !updated_hosts.nil? and updated_hosts.size > 0
  70.         puts "Done. Keeping alive #{updated_hosts.size} host with IP '#{my_public_ip}':"
  71.         updated_hosts.each do |host|
  72.             puts "- #{host}"
  73.         end
  74.     else
  75.         $stderr.puts "There was an error updating the domains of noip.com or there were no hosts to update"
  76.     end
  77. end
  78.  
  79. puts "==============================="
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement