Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "net/url"
  6. "os"
  7. "strings"
  8.  
  9. "github.com/aws/aws-sdk-go/aws"
  10. "github.com/aws/aws-sdk-go/aws/credentials"
  11. "github.com/aws/aws-sdk-go/service/ec2"
  12. )
  13.  
  14. func main() {
  15. if len(os.Args) == 1 {
  16. fmt.Println("usage: ec2 [profile]")
  17. return
  18. }
  19. profile := os.Args[1]
  20. svc := ec2.New(&aws.Config{
  21. Credentials: credentials.NewSharedCredentials("", profile),
  22. Region: "ap-northeast-1",
  23. })
  24. resp, err := svc.DescribeInstances(&ec2.DescribeInstancesInput{
  25. Filters: []*ec2.Filter{&ec2.Filter{
  26. Name: aws.String("instance-state-name"),
  27. Values: []*string{
  28. aws.String("running"),
  29. },
  30. }},
  31. })
  32. if err != nil {
  33. panic(err)
  34. }
  35. for _, res := range resp.Reservations {
  36. for _, inst := range res.Instances {
  37. if inst.PublicIPAddress == nil {
  38. continue
  39. }
  40. name := "None"
  41. for _, keys := range inst.Tags {
  42. if *keys.Key == "Name" {
  43. name = url.QueryEscape(*keys.Value)
  44. break
  45. }
  46. }
  47. line := strings.Join([]string{
  48. *inst.PublicIPAddress,
  49. *inst.PrivateIPAddress,
  50. name,
  51. *inst.KeyName,
  52. }, "\t")
  53. fmt.Println(line)
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement