Updated DNS Zone Update tool for Hostmonster

A couple years ago I posted a slightly modified script for hostmonster to update the dns zone entries for subdomains. It used mechanize and ruby. However, since then my script broke as hostmonster made some changes to their backend. Another guy made some changes that seemed to work for a while, but again it has broken. My previous post about this is here.

The culprit seems to be the mixture of javascript / ajax and the fact that hostmonster returns an empty page when you append the /ajax onto the dnszone url. No worries though, I found another way to do it.

Using Watir and Headless, it is possible to achieve the same functionality.

You need to add a few things to ubuntu to make it work:
sudo apt-get install ruby ruby-dev xvfb
sudo gem install watir
sudo gem install headless

Here’s the updated script (note, you can still use the same cron jobs and ip scripts etc. from the previous two techniques.:

#!/usr/bin/ruby
(more…)

Hostmonster auto update IP address of subdomain

With my Hostmonster account, I host this website with my www.jasonernst.com domain, but I also have many other machines that are referred to with subdomains. For example, dev.jasonernst.com is my home machine and lab.jasonernst.com is my office machine at school.

However, as you can imagine with the home machine in particular the IP is prone to change occasionally since it is given from the ISP using DHCP. So I searched around for some script to be able to change the zone file in Hostmonster since this controls the IP addresses of all my subdomains.

** Of course the obvious and easy way to do this is with some kind of dyndns account, but I’m picky and like to have everything working under my own domain 😛

I was looking for some type of SSH script way to do it, but it looks like there is no easy way to do this, but I found a ruby script here: http://deathwarrior.wordpress.com/ which allows me to do what I want. Unfortunately the script was written a little while ago and some of it needs to be changed a bit to work.

The first part is the required libraries that must be installed. This has changed because the ruby-mechanize library does not seem to be found in the Ubuntu 12.04 repos. This should do the trick though:

sudo apt-get install libwww-mechanize-ruby ruby-json ruby

The other problem is in the ruby script itself. It seems verify_mode does not exist anymore, so just comment that line out. Another problem is the user-agent which does not seem to be found in the version I was using with Ubuntu. So you can change that to “Windows IE 7” and it should fix it. Here is the code with the changes:


#!/usr/bin/env ruby

require 'mechanize'
require 'json'

USERNAME = 'MY_USERNAME'
PASSWORD = 'MY_PASSWD'
DOMAIN = 'positrones.net'
SUBDOMAIN = 'my_subdomain'
ADDRESS = '0.0.0.0' # If nil then try to get automagicaly

URLS = {
'login' => 'https://login.hostmonster.com/?',
'drecords' => 'https://my.hostmonster.com/cgi/dm/zoneedit/ajax'
}

USER_AGENT = 'Windows IE 7'

m = Mechanize.new do |a|
#a.verify_mode = OpenSSL::SSL::VERIFY_NONE
a.user_agent_alias = USER_AGENT
end

def get_ip
r = Net::HTTP.get('jasonernst.com', '/ip.php')
ip = r.match(/\d{1,4}\.\d{1,4}\.\d{1,4}\.\d{1,4}/)

ip[0].to_s
end

# Do the login stuff
print "Checking user and password... "
page = m.get( URLS['login'] );
form = page.form_with( :name => 'theform' )
form['login'] = USERNAME
form['password'] = PASSWORD
send_button = form.button_with(:value => 'Login')
form.click_button(send_button)
page = page.links[0].click
puts "done!"

# Edit the DNS Zone
page = page.link_with(:text => 'DNS Zone Editor').click
print "Getting old zone records... "
json = JSON.parse( m.post( URLS['drecords'], {'op' => 'getzonerecords', 'domain' => DOMAIN} ).body )
puts "done!"

print "Trying to get subdomain old info... "
json['data'].each do |r|
if r['name'] == SUBDOMAIN
print "done!\nSaving new address[#{r['address']} => #{ADDRESS||get_ip}]... "
json = m.post( URLS['drecords'], {'op' => 'editzonerecord',
'domain' => DOMAIN,
'name' => SUBDOMAIN,
'orig__name' => SUBDOMAIN,
'address' => ADDRESS||get_ip,
'orig__adress' => r['address'],
'ttl' => r['ttl'],
'orig__ttl' => r['ttl'],
'Line' => r['Line'],
'type' => r['type']}
)
json = JSON.parse(json.body)

if( json['result'] == 1 )
puts "done!\nAddress changed succesfully!"
Kernel::exit(0)
else
puts "\nAn error has ocurred trying to save new address :("
Kernel::exit(1)
end
end
end

puts "The subdomain #{SUBDOMAIN} cannot be found!"
Kernel::exit(1)

The last thing that should be done is a server page somewhere for this code to find out it’s own IP address. All the page should return is the IP address itself, not any HTML. You can leave the default in the script which will use my IP script, or you can make your own. It’s as simple as this:

And one more thing, if you are so inclined is to add the script as a chronjob. I have mine set to run every hour, but you can do it more or less often according to your preferences.

crontab -e

I named put my script in a /scripts/ folder I made and named it “hostmonster-auto-ip.sh”

So the crontab entry is:

@hourly /scripts/hostmonster-auto-ip.sh

Some Useful Information for Setting Up Subversion on Ubuntu

Not a tutorial or guide this time, since everyone else seems to have this covered quite well, but thought I’d collect a few good links together in one place. Hopefully these links will be useful to someone just starting with SVN. For everyone who isn’t using some type of version control, nows your chance!

Ubuntu tutorial for installation:
http://danielabrantes.blogspot.com/2010/11/install-subversion-in-ubuntu-1010.html

Beginner HowTo (suited to WordPress plugin devs, but still good to apply to your own setup):
http://codex.wordpress.org/Using_Subversion
http://wordpress.org/extend/plugins/about/svn/

Multi-platform SVN client – RapidSVN:
I found this client especially useful since it works across platforms and it is lightweight and quick.
http://rapidsvn.tigris.org/


RapidSVN Screenshot
 

Popular Windows SVN client – TortoiseSVN:
This client is useful if you like tight integration with Windows. It shows up in the context menus and is very easy to work with.
http://tortoisesvn.tigris.org/


TortoiseSVN Screenshot
 

WebSVN:
A more refined web interface to svn, compared with the one provided using the initial setup.
http://agilior.pt/blogs/pedro.rainho/archive/2010/02/06/11698.aspx


WebSVN Screenshot
 

SVN on Hostmonster (or other hosts with blocked svn port, but SSH access):
http://hostmonsterforum.com/showthread.php?1294-Subversion