Skip to content

Instantly share code, notes, and snippets.

@iammuho
Created October 21, 2016 11:44
Show Gist options
  • Save iammuho/77aa006bd53f9b6988a6e3180aba4abf to your computer and use it in GitHub Desktop.
Save iammuho/77aa006bd53f9b6988a6e3180aba4abf to your computer and use it in GitHub Desktop.
#! /usr/bin/perl -w
use JSON;
#my $instance_ref = $var->{"Reservations"};
##Function: GetInstanceInfo()
##Description:
##get instance id, instance Name, instance status, instance public IP, Security Group
##Usage: GetInstanceInfo($instance_ref)
##Requirement: JSON
##Date: 2015-9-16
sub GetInstanceInfo(){
my $Reservations_ref = $_[0]; #$Reservations_ref = $instance_hashref->{"Reservations"}[i];
my ($Name, $instance_id, $status, $public_ip, $security_group);
foreach my $reser_item (@{$Reservations_ref}){
my $instance_ref = $reser_item->{"Instances"};
foreach my $server_ref (@{$instance_ref}) {
$status = $server_ref->{"State"}{"Name"};
if(defined($server_ref->{"PublicIpAddress"})){
$public_ip = $server_ref->{"PublicIpAddress"};
}else{
$public_ip = "NULL";
}
$instance_id = $server_ref->{"InstanceId"};
#my $security_groups = $server_ref->{"SecurityGroups"};
#foreach my $sg (@{$security_groups}) {
#$security_group = $security_group . " " . $sg->{"GroupName"};
# $security_group = $sg->{"GroupName"};
#}
my $tags = $server_ref->{"Tags"};
foreach my $tag (@{$tags}) {
if($tag->{"Key"} eq "Name"){
$Name = $tag->{"Value"};
}
}
printf "\n%s\t%-25s\t%-15s\t%-16s\n", $instance_id,$Name,$status,$public_ip;
}
#print "\n$instance_id\t$Name\t$status\t$public_ip\t$security_group\n";
}
}
my $profile = $ARGV[0];
print "=" x 120 ."\n";
print "instances in $profile\n";
print "_" x 120 ."\n";
#print "InstanceID\tName\tStatus\tIP\tSecurityGroup\n";
printf "\n%s\t%-25s\t%-15s\t%-16s\n", "InstanceID","Name","Status","IP";
my $json_data = `aws --profile $profile ec2 describe-instances`;
my $instance_info = decode_json $json_data;
my $Reservations_ref = $instance_info->{"Reservations"};
&GetInstanceInfo($Reservations_ref);
print "=" x 120 ."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment