Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "net/url"
- "os"
- "strings"
- "github.com/aws/aws-sdk-go/aws"
- "github.com/aws/aws-sdk-go/aws/credentials"
- "github.com/aws/aws-sdk-go/service/ec2"
- )
- func main() {
- if len(os.Args) == 1 {
- fmt.Println("usage: ec2 [profile]")
- return
- }
- profile := os.Args[1]
- svc := ec2.New(&aws.Config{
- Credentials: credentials.NewSharedCredentials("", profile),
- Region: "ap-northeast-1",
- })
- resp, err := svc.DescribeInstances(&ec2.DescribeInstancesInput{
- Filters: []*ec2.Filter{&ec2.Filter{
- Name: aws.String("instance-state-name"),
- Values: []*string{
- aws.String("running"),
- },
- }},
- })
- if err != nil {
- panic(err)
- }
- for _, res := range resp.Reservations {
- for _, inst := range res.Instances {
- if inst.PublicIPAddress == nil {
- continue
- }
- name := "None"
- for _, keys := range inst.Tags {
- if *keys.Key == "Name" {
- name = url.QueryEscape(*keys.Value)
- break
- }
- }
- line := strings.Join([]string{
- *inst.PublicIPAddress,
- *inst.PrivateIPAddress,
- name,
- *inst.KeyName,
- }, "\t")
- fmt.Println(line)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement