既にRubyだとやってる人がいて便利そうだったので、Groovy(Java)でも書いてみた。
gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grapes([ | |
@Grab(group='commons-codec', module='commons-codec', version='1.10'), | |
@Grab(group='com.amazonaws', module='aws-java-sdk', version='1.9.13') | |
]) | |
import com.amazonaws.services.ec2.* | |
import com.amazonaws.auth.* | |
import com.amazonaws.auth.profile.* | |
import com.amazonaws.regions.* | |
credentials = new ProfileCredentialsProvider().getCredentials() | |
ec2 = new AmazonEC2Client(credentials) | |
ec2.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1)) | |
result = ec2.describeInstances() | |
result.reservations.instances.each { | |
println "${it.tags[0].value[0]}\t${it.state.name[0]}\t${it.publicDnsName[0]}" | |
} |
使うとき
- ~/.ssh/configにログイン対象のEC2のユーザー名、KeyPair、ホスト名を予め指定しておくこと
- ~/.aws/credentialsの設定をしておくこと。AWS CLIでconfigure設定をすればOKだと思われる。
- groovy ec2list.groovy| percol | cut -f 3 | xargs -o -n 1 ssh
- 上記コマンドで対象のEC2にSSHログインできる
- いちいちコマンドを打つのが面倒なので適当にalias設定をしておくととても便利
0 件のコメント:
コメントを投稿