3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWSで特定のリージョンの全ホストのIPアドレスを一覧表示するスクリプト

Posted at

事前準備

.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
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?