事前準備
.bashrcなどで以下の環境変数を設定しておきます。
export AWS_ACCESS_KEY=${Your_access_key_here}
export AWS_SECRET_KEY=${Your_secret_key_here}
export EC2_URL=${Your_region_url_here}
Regions and Endpoints - Amazon Web Services General Reference
スクリプト
awk版
show_hosts_ip.sh
#!/bin/sh
ec2-describe-instances --show-empty-fields | awk '
$1=="INSTANCE" { public_ip=$17; private_ip=$18 }
$1=="TAG" {
host=$0; sub("^.*Name\t", "", host); sub("\t.*", "", host);
printf("%s\t%s\t%s\n", host, public_ip, private_ip)
}' | sort
gawk版
show_hosts_ip.sh
#!/bin/sh
ec2-describe-instances --show-empty-fields | gawk '
$1=="INSTANCE" { public_ip=$17; private_ip=$18 }
$1=="TAG" {
host = gensub("^.*Name\t([^\t]*).*$", "\\1", 1)
printf("%s\t%s\t%s\n", host, public_ip, private_ip)
}' | sort