forked from merkez/netman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (67 loc) · 1.35 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
package main
import (
"bytes"
"fmt"
"net"
"os"
"os/exec"
"strings"
"text/template"
)
const (
IFUP = "ifup"
)
type Interfaces struct {
NetInt []NetInterface
}
type NetInterface struct {
InetName string
}
func main() {
var netInterfaces []NetInterface
ineti, err := net.Interfaces()
if err != nil {
panic(err)
}
for _, inter := range ineti {
if !strings.Contains(inter.Flags.String(), "up") {
netInterfaces = append(netInterfaces, NetInterface{InetName: inter.Name})
}
}
downInterfaces := Interfaces{NetInt: netInterfaces}
info := createInterfacesSetting(downInterfaces)
fmt.Printf("%s\n", info)
createInterfacesFile(info)
for _, i := range downInterfaces.NetInt {
if err := execUp(i.InetName); err != nil {
panic(err)
}
}
}
func execUp(iname string) error {
cmd := exec.Command(IFUP, iname)
err := cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
return err
}
func createInterfacesSetting(interfaces Interfaces) string {
var tpl bytes.Buffer
tmpl := template.Must(template.ParseFiles("/home/vagrant/netman/interfaces.tmpl"))
tmpl.Execute(&tpl, interfaces)
return tpl.String()
}
func createInterfacesFile(content string) error {
f, err := os.Create("/etc/network/interfaces")
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(content)
if err != nil {
return err
}
return nil
}