/
main.go
126 lines (100 loc) · 2.53 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"flag"
"fmt"
"os"
"text/tabwriter"
"github.com/photosojourn/aws-tools/pkg/awstools"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
func getSubnets(vpc ec2.Vpc) {
ec2session := ec2.New(session.New(), aws.NewConfig())
subnetFilter := &ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{
aws.String(*vpc.VpcId),
},
},
},
}
resultSub, err := ec2session.DescribeSubnets(subnetFilter)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Subnet Information")
fmt.Println("==================")
w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0)
fmt.Fprintln(w, "Subnet ID\tName\tCIDR Block\tAvailable Ip's\tAvailability Zone\tPublic IP")
for _, subnet := range resultSub.Subnets {
fmt.Fprintf(w, "%v\t", *subnet.SubnetId)
fmt.Fprintf(w, "%v\t", awstools.GetSubnetName(*subnet))
fmt.Fprintf(w, "%v\t", *subnet.CidrBlock)
fmt.Fprintf(w, "%v\t", *subnet.AvailableIpAddressCount)
fmt.Fprintf(w, "%v\t", *subnet.AvailabilityZone)
fmt.Fprintf(w, "%v\t", *subnet.MapPublicIpOnLaunch)
fmt.Fprintf(w, "\n")
}
w.Flush()
fmt.Println("")
}
func getNacls(vpc ec2.Vpc) {
ec2session := ec2.New(session.New(), aws.NewConfig())
naclFilter := &ec2.DescribeNetworkAclsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{
aws.String(*vpc.VpcId),
},
},
},
}
resultNacl, err := ec2session.DescribeNetworkAcls(naclFilter)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("NACL Information")
fmt.Println("==================")
for _, nacl := range resultNacl.NetworkAcls {
fmt.Println(*nacl.NetworkAclId)
}
fmt.Println("")
}
func main() {
vpcID := flag.String("i", "", "ID of VPC")
vpcSubnet := flag.Bool("s", false, "Subnet info")
vpcNacl := flag.Bool("n", false, "NACL Information")
vpcAll := flag.Bool("a", false, "List of info")
flag.Parse()
ec2session := ec2.New(session.New(), aws.NewConfig())
params := &ec2.DescribeVpcsInput{
VpcIds: []*string{
aws.String(*vpcID),
},
}
result, err := ec2session.DescribeVpcs(params)
if err != nil {
fmt.Println(err)
return
}
vpc := result.Vpcs[0]
//subnets := getSubnets(*vpc)
name := awstools.GetVpcName(*vpc)
fmt.Println("VPC Info")
fmt.Println("========")
fmt.Println("VPC Id: " + *vpc.VpcId)
fmt.Println("Name: " + name)
fmt.Println("")
if *vpcSubnet || *vpcAll {
getSubnets(*vpc)
}
if *vpcNacl || *vpcAll {
getNacls(*vpc)
}
}