2011-02-10

The Need

When I'm away from home, I often find it rather useful to connect to my home network. Due to the dynamic IP address assigned by my Internet Service Provider, I use a third-party service to map a domain name to my dynamic address. This scenario requires that a machine in my network notifies the third-party service of network address changes; and a script to do this was fairly easy to write.

The Preparation

The first thing I need to do was create an account with a dynamic IP Domain Name Service provider. Previously I had use http://dyndns.com but their service has been intermittent for the last few days so I though a switch to http://no-ip.com was in order.

Aside from providing Dynamic DNS, No-IP provides software allowing Linux, Macintosh, and Windows computer users to easily update their dynamic address information. Even more importantly, No-IP provides an API for programmers to utilize when writing their own software to update address information.

The Script

Honestly, this could be done with a single line curl command, but that wouldn't be fun and it wouldn't help me learn Ruby. Enter The Ruby

#!/usr/bin/env ruby

require 'open-uri'

#set up some variables
"MYUSERNAME"
"MyPaSsWoRd"
"my.hostname.com"

url "https://dynupdate.no-ip.com/nic/update?hostname=#{h}"

#open the url
open(url,
  "User-Agent" => "Jezra's No-IP Update Script",
  :http_basic_authentication => [u,p]
)

#output the return data
puts f.read()

This script is now happily running on my miniserver at 4 hour intervals.

The big take away here for me, was learning how to use open() from the open-uri module to set the User-Agent of a request as well as utilizing http basic authentication.

One More Thing

Since I'm learning Ruby, and since I learn by doing, I force myself to write (semi)useful utilities for everyday task, like creating a password for my No-IP account. Instead of just coming up with something I'll remember, like 1 2 3 4 5, I decided to write a string generator. woohoo!

#!/usr/bin/env ruby

def help()
  puts "--Hey, you are doing it wrong--
#{$0requires a numeric argument 
examples:
# create a 20 character string
#{$020
# create a 10 character string
#{$010

"
end

#what chars do we have to pick from?
chars "!@$%^*_+=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

#get the first argument as an integer
num ARGV[0].to_i

#the num better exist, and it better not be zero!
if num.nil? or num.zero?
  help()
  exit()
end

charslen chars.length
string  ''
(1..num).each do 
  char charsrand(charslen) ]
  string << char
end

puts string

Sweet! Now quit reading, and go learn something new.

Comments
2011-02-11 jamba:
pretty cool. how are you liking ruby so far?
2011-02-11 jezra:
I dig it. The standard library comes with buckets of useful modules and I *seem* to get what I need done fairly quickly
2011-02-12 James:
Hey, if you're digging Ruby you might check out http://www.sinatrarb.com/ It's a pretty sweet DSL for Ruby web development. I tend to use it for simple API stuff.
2011-02-12 jezra:
I already have and I plan on using sinatra and thin server to add a web interface on my next project
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
1 plus 5
Answer:
required
subscribe
 
2019
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008