Skip to content

Instantly share code, notes, and snippets.

@simplytunde
Created August 3, 2016 16:09
Show Gist options
  • Save simplytunde/f84572d5544488a10c325215c21dcdaf to your computer and use it in GitHub Desktop.
Save simplytunde/f84572d5544488a10c325215c21dcdaf to your computer and use it in GitHub Desktop.
AWS & Thor: Get User Instance Information
require "aws-sdk"
require "thor"

class MyInstances < Thor
   desc "allMyEc2","Simply get all public ip address of an AWS user account"
   def allMyEc2
        ec2Client=Aws::EC2::Client.new()
        ec2Client.describe_instances.reservations.each do |reservation|
              reservation.instances.each do |instance|
                puts instance.public_ip_address  if instance.public_ip_address
              end
        end
   end

   desc 'instance','Get Information about this specific ip address'
   option :ip,:type => :string,:required => false
   option :id,:type => :string,:required => false
   option :type,:type => :boolean,:required => false, :aliases => ['y']
   option :dns,:type => :boolean,:required => false, :aliases => ['d']
   option :vpc,:type => :boolean,:required => false, :aliases => ['v']
   def instance
     ec2Client=Aws::EC2::Client.new()
     myInstance=false
     if !options[:ip] and !options[:id]
       puts "Please enter a valid instance public id or instance id"
       exit
     end
     ec2Client.describe_instances.reservations.each do |reservation|
           reservation.instances.each do |instance|
              myInstance=instance if instance.public_ip_address == options[:ip]
              myInstance=instance if instance.instance_id == options[:id]
              break if myInstance
           end
           break if myInstance
     end
     if myInstance
       puts myInstance.public_ip_address  if !options[:ip]
       puts myInstance.instance_id if !options[:id]
       puts myInstance.instance_type if options[:type]
       puts myInstance.public_dns_name if options[:dns]
       puts myInstance.vpc_id if (options[:vpc] and myInstance.vpc_id)
     else
        puts "Please enter a valid instance public id or instance id"
     end
   end
end

t=MyInstances.start(ARGV)
#t.allEc2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment