Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env ruby
#
# This Ruby script will convert between Amazon EC2 public and private DNS names.
#
# The first command-line argument is used as a regular expression to perform pattern-matching
# against leading strings of all public and private DNS names returned from an EC2 'describe-instances'
# API call. (ie, if the argument is 'domU', we will find all DNS names that match /domU.*/)
# Whenever a match is found, the "other" type of DNS name is printed. (private <=> public)
#
# Any arguments after the first are ignored.
#
# Be sure to modify this script to add your AWS access and secret keys in the appropriate places.
#
# Requires the amazon-ec2 gem (gem install amazon-ec2)
#
# Author: Darren Lee (darren@bizo.com)
require 'rubygems'
require 'EC2'
ACCESS_KEY_ID = '(your AWS access key)'
SECRET_ACCESS_KEY = '(your AWS secret key)'
if ARGV.length < 1 then
puts "Error: no leading string specified"
exit
end
regex = /#{ARGV[0]}.*/
ec2 = EC2::Base.new(:access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY)
ec2.describe_instances.reservationSet.item.each do |instance|
# Note that instance id is also available at instance.instancesSet.item[0]['instanceId']
publicDns = instance.instancesSet.item[0]['dnsName']
privateDns = instance.instancesSet.item[0]['privateDnsName']
if !publicDns.nil? && !privateDns.nil? then
if privateDns =~ regex then
puts publicDns
end
if publicDns =~ regex then
puts privateDns
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment