-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (123 loc) · 2.97 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package ec2ifupifdown
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os/exec"
"strings"
)
type Interf struct {
DeviceNumber int
InterfaceId string
LocalHostname string
LocalIpv4s []string
Mac string
OwnerId string
SecurityGroupIds []string
SecurityGroups []string
SubnetId string
SubnetIpv4CidrBlock string
VpcId string
VpcIpv4CidrBlock string
VpcIpv4CidrBlocks []string
}
var (
macsURL = "http://169.254.169.254/latest/meta-data/network/interfaces/macs"
baseURL = "http://169.254.169.254/latest/meta-data/network/interfaces/macs/%s/%s"
)
func GetInterfaces() (interfaces []Interf, err error) {
macs, err := GetMacs()
if err != nil {
return interfaces, err
}
log.Println(macs)
for _, mac := range macs {
var interf Interf
interf.Mac = mac
interf.DeviceNumber, err = GetDeviceNumber(mac)
if err != nil {
return interfaces, err
}
interf.InterfaceId, err = GetInterfaceId(mac)
if err != nil {
return interfaces, err
}
interf.LocalHostname, err = GetLocalHostname(mac)
if err != nil {
return interfaces, err
}
interf.LocalIpv4s, err = GetLocalIpv4s(mac)
if err != nil {
return interfaces, err
}
interf.Mac = mac
interf.OwnerId, err = GetOwnerId(mac)
if err != nil {
return interfaces, err
}
interf.SecurityGroupIds, err = GetSecurityGroupIds(mac)
if err != nil {
return interfaces, err
}
interf.SecurityGroups, err = GetSecurityGroups(mac)
if err != nil {
return interfaces, err
}
interf.SubnetId, err = GetSubnetId(mac)
if err != nil {
return interfaces, err
}
interf.SubnetIpv4CidrBlock, err = GetSubnetIpv4CidrBlock(mac)
if err != nil {
return interfaces, err
}
interf.VpcId, err = GetVpcId(mac)
if err != nil {
return interfaces, err
}
interf.VpcIpv4CidrBlock, err = GetVpcIpv4CidrBlock(mac)
if err != nil {
return interfaces, err
}
interf.VpcIpv4CidrBlocks, err = GetVpcIpv4CidrBlocks(mac)
if err != nil {
return interfaces, err
}
interfaces = append(interfaces, interf)
}
return interfaces, err
}
// GetMacs returns a list of al interfaces' mac addresses
func GetMacs() (macs []string, err error) {
resp, err := http.Get(macsURL)
if err != nil {
return macs, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return macs, err
}
macs = strings.Split(string(body), "\n")
if len(macs) == 0 {
return macs, errors.New("no interfaces")
}
return macs, nil
}
// Ifdown disables interface
// devicenumber 0 -> disables eth0
// devicenumber 1 -> disables eth1
func (i Interf) Ifdown() (err error) {
interf := fmt.Sprintf("eth%d", i.DeviceNumber)
cmd := exec.Command("ifdown", interf)
return cmd.Run()
}
// Ifup enables interface
// devicenumber 0 -> enables eth0
// devicenumber 1 -> enables eth1
func (i Interf) Ifup() (err error) {
interf := fmt.Sprintf("eth%d", i.DeviceNumber)
cmd := exec.Command("ifup", interf)
return cmd.Run()
}