从命令行格式化Amazon EC2实例列表

时间:2011-07-12 21:26:12

标签: macos amazon-ec2 terminal osx-snow-leopard amazon

我使用ec2-describe-instances -H检索有关当前Amazon EC2实例的数据,该实例返回如下内容:

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

在OS X终端中看起来很乱。如何使这个输出更具可读性?

2 个答案:

答案 0 :(得分:4)

我刚刚发了一篇你可能感兴趣的bash / awk脚本:

https://github.com/epheph/fec2din

它以以下格式显示EC2实例输出:

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

答案 1 :(得分:3)

您希望查看或查看哪些具体部分?

 ec2-describe-instances | grep INSTANCE | awk {'print $4'}

这将为您提供实例名称,在您的示例中为ec2-xx-xx-xx-xx.compute-1.amazonaws.com

修改

当提问的人说他们希望用更清洁的格式 ...而没有说明那是什么......这是我的微弱尝试:

 #!/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

小免责声明上述代码可能并不完美......它可以让提出问题的人提供正确的信息,以便制作一个干净的格式"但他们认为合适。