Skip to content

Instantly share code, notes, and snippets.

@gb-swatanabe
Created January 27, 2017 10:16
Show Gist options
  • Save gb-swatanabe/d0bd6e1c3efe95654004449fc6ce5448 to your computer and use it in GitHub Desktop.
Save gb-swatanabe/d0bd6e1c3efe95654004449fc6ce5448 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# coding: utf-8
# usage: $0 [some options for `aws ec2`]
require 'json'
line_format = "%-19s %-23s %-11s %-15s %-15s %-12s %-16s %s"
line_length = 142
# command line
aws_cli_cmd = ["aws","ec2",ARGV,"describe-instances"].join(" ")
# header
puts line_format % [
"InstanceID","Name","Status","PublicIP","PrivateIP","InstanceType","AvailabilityZone","LaunchTime"
]
puts "-" * line_length
# run aws cli and parse
JSON.parse(%x(#{aws_cli_cmd}))["Reservations"].each{|n|
# ReservationId loop
n["Instances"].each{|i|
puts line_format % [
i["InstanceId"],
if i.has_key?("Tags") then i["Tags"].find{|tag| tag["Key"] == "Name" }["Value"] else "" end,
i["State"]["Name"],
if i.has_key?("PublicIpAddress") then i["PublicIpAddress"] else "" end,
if i.has_key?("PrivateIpAddress") then i["PrivateIpAddress"] else "" end,
i["InstanceType"],
i["Placement"]["AvailabilityZone"],
i["LaunchTime"]
]
}
}
# footer
puts "-" * line_length
puts "%#{line_length}s" % [aws_cli_cmd]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment