【问题标题】:Formatting list of Amazon EC2 instances from the command line从命令行格式化 Amazon EC2 实例列表
【发布时间】:2011-07-12 21:26:12
【问题描述】:

我使用 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 终端中看起来很乱。如何使此输出更具可读性?

【问题讨论】:

  • 可能会将输出存储在 .txt 文件中并查看它,它应该是 NEAT :-)

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


【解决方案1】:

我刚刚编写了一个您可能感兴趣的小 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

【讨论】:

    【解决方案2】:

    您想查看或查看哪些具体部分?

     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
    

    小免责声明上面的代码可能并不完美......它是为了给提出问题的人提供正确的信息来制作他们认为合适的“干净格式”。

    【讨论】:

    • 我希望以更清晰的格式查看所有数据,但这以不同的方式解决了问题。谢谢!
    • 谢谢!请注意,第 5 行的 instances 中缺少 i(尝试对其进行编辑,但至少有 6 个字符)。
    猜你喜欢
    • 2019-04-27
    • 2011-07-08
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多