Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pingali/86265 to your computer and use it in GitHub Desktop.
Save pingali/86265 to your computer and use it in GitHub Desktop.
# http://ec2dream.blogspot.com/2009/03/networking-in-ec2.html
#Setup a cron job to update /etc/hosts as often as you like. I do it once per hour on #all my machines
#
#0 * * * * /usr/local/sbin/hosts -a myaccess -s mysecret >/etc/hosts
#
#All my machines have this ec2 security key + script + cron approach. I do not have to #run dyndns or any private dns servers to keep track of all my internal server ip #addresses. My /etc/hosts looks like the following on the three machines in the test #cluster:
#127.0.0.1 localhost
#10.252.202.221 oahu.ec2 oahu
#10.253.115.175 maui.ec2 maui
#10.253.114.190 hawaii.ec2 hawaii
#(from http://tim.dysinger.net/2008/10/13/using-amazon-ec2-metadata-as-a-simple-dns)
#!/usr/bin/env ruby
%w(optparse rubygems EC2 resolv pp).each { l require l }
options = {}
parser = OptionParser.new do p
p.banner = "Usage: hosts [options]"
p.on("-a", "--access-key USER", "The user's AWS access key ID.") do aki
options[:access_key_id] = aki
end
p.on("-s",
"--secret-key PASSWORD",
"The user's AWS secret access key.") do sak
options[:secret_access_key] = sak
end
p.on_tail("-h", "--help", "Show this message") {
puts(p)
exit
}
p.parse!(ARGV) rescue puts(p)
end
if options.key?(:access_key_id) and options.key?(:secret_access_key)
puts "127.0.0.1 localhost"
EC2::Base.new(options).describe_instances.reservationSet.item.each do r
r.instancesSet.item.each do i
if i.instanceState.name =~ /running/
puts(Resolv::DNS.new.getaddress(i.privateDnsName).to_s +
" #{i.keyName}.ec2 #{i.keyName}")
end
end
end
else
puts(parser)
exit(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment