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 :P
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/](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:
```
<? echo $_SERVER['REMOTE_ADDR']; ?>
```
And one more thing, if you are so inclined is to add the script as a cronjob. 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
```