Formatting list of Amazon EC2 instances from the command line
I retrieve data about my current Amazon EC2 instances with ec2-describe-instances -H
, which returns something like the following:
Type ReservationID Owner Groups Platform
RESERVATION r-xxxxxxxx xxxxxxxxxxxx default
INSTANCE i-xxxxxxxx ami-2b5fba42 ec2-xx-xx-xx-xx.compute-1.amazonaws.com ip-xx-xx-xx-xx.ec2.internal running user 0 m1.small 2011-07-12T21:15:39+0000 us-east-1a aki-xxxxxxxx ari-xxxxxxxx monitoring-disabled xx.xx.xx.xx xx.xx.xx.xx instance-store paravirtual xen sg-xxxxxxxx default
This looks messy in the OS X terminal. How can I make this outp开发者_JAVA百科ut more readable?
I just whipped up a little bash/awk script you might be interested in:
https://github.com/epheph/fec2din
It displays EC2 instance output in the format of:
Instance: i-57192e11
AMI: ami-a7f539ce
Type: t1.micro
Public IP: 50.99.41.60 (ec2-50-99-41-60.compute-1.amazonaws.com)
Private IP: 10.99.241.197
Public Key: production
Start Time: 2011-11-09
Security Group: production
Block Devices:
/dev/sda1
/dev/sdj
/dev/sdk
What specific portions would you like to see, or view?
ec2-describe-instances | grep INSTANCE | awk {'print $4'}
This will give you the instance name, which would be ec2-xx-xx-xx-xx.compute-1.amazonaws.com
in your example.
Edit
As the person asking the question said they wanted it in a cleaner format ... without specifying what that is ... here is my feeble attempt:
#!/bin/bash
tmpFile="/tmp/ec2.info"
ec2Info=`ec2-describe-instances > $tmpFile`
instances=`cat $tmpFile | grep TAG | awk {'print $3'}`
numOfInstances=`cat $tmpFile | grep INSTANCE | wc -l`
you=`whoami`
echo "Dear $you, I wanted to describe for you the current number of instances you have: $numOfInstances"
echo "The instances you have, by hostname, are as follows ..."
for instance in $instances
do
hostname=`cat $tmpFile | grep INSTANCE | grep $instance | awk {' print $4 '}`
echo "$hostname"
done
small disclaimer the above code may not be perfect ... it's to give the person asking the question the right information to make a "clean format" however they see fit.
精彩评论