-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwinrm.go
254 lines (187 loc) · 5.87 KB
/
winrm.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"errors"
"fmt"
"log"
"math/rand"
"net"
"regexp"
"strings"
"time"
"github.com/masterzen/winrm"
)
type WinrmError struct {
codeReturn int
message string
stderr string
}
func (e *WinrmError) Error() string {
return fmt.Sprintf("Winrm (%d) - %s", e.codeReturn, e.message)
}
type Communicator struct {
username string
password string
client *winrm.Client
endpoint *winrm.Endpoint
}
func (c *Communicator) Connect() error {
params := winrm.DefaultParameters
client, err := winrm.NewClientWithParameters(c.endpoint, c.username, c.password, params)
if err != nil {
return err
}
shell, err := client.CreateShell()
if err != nil {
// error here if cannot connect
return err
}
shell.Close()
c.client = client
return nil
}
func (c *Communicator) AddFilterAllowAddress(mac string, description string) error {
command := fmt.Sprintf(
"Add-DhcpServerv4Filter -List Allow -macAddress \"%s\" -Description \"%s\" -Force",
mac,
description,
)
_, stderr, returnCode := c.Execute(command)
if returnCode != 0 {
return &WinrmError{returnCode, "Cannot allow mac address in dhcp.", stderr}
}
return nil
}
func (c *Communicator) RemoveFilterAllowAddress(mac string) error {
command := fmt.Sprintf(
"Remove-DhcpServerv4Filter \"%s\"", mac,
)
c.Execute(command)
return nil
}
func (c *Communicator) GetAllAllowedMacAddress() []string {
stdout, _, _ := c.Execute("Get-DhcpServerv4Filter -List Allow")
lines := strings.Split(stdout, "\n")
var macs []string
re := regexp.MustCompile(`(([0-9ABCDEF]{2})-?){6,8}`)
for _, element := range lines {
matched, _ := regexp.MatchString(`^(([0-9ABCDEF]{2})-?){6,8}`, element)
if matched {
mac := string(re.Find([]byte(element)))
macs = append(macs, mac)
}
}
return macs
}
func (c *Communicator) AddDHCPReservation(mac string, ip net.IP, scopeId string, description string, name string) error {
command := fmt.Sprintf(
"Add-DhcpServerv4Reservation -ScopeId %s -Description \"%s\" -IPAddress %s -Name \"%s\" -ClientId %s -Type Dhcp",
scopeId, description, ip.String(), name, mac,
)
_, stderr, returnCode := c.Execute(command)
if returnCode != 0 {
return &WinrmError{returnCode, "Cannot add reservation in dhcp server.", stderr}
}
return nil
}
func (c *Communicator) RemoveDHCPReservation(mac string, scopeId string) error {
command := fmt.Sprintf(
"Remove-DhcpServerv4Reservation -ScopeId %s -ClientId \"%s\"",
scopeId, mac,
)
c.Execute(command)
return nil
}
func (c *Communicator) RemoveDHCPLease(scopeId string, mac string, ip string) error {
command := fmt.Sprintf(
"Remove-DhcpServerv4Lease -ScopeId %s -ClientId \"%s\" -IPAddress %s",
scopeId, mac, ip,
)
c.Execute(command)
return nil
}
func (c *Communicator) GetFreeIp(scopeId string) (net.IP, error) {
command := fmt.Sprintf(
"Get-DhcpServerv4FreeIPAddress -ScopeId %s -NumAddress 1024",
scopeId,
)
stdout, stderr, exitCode := c.Execute(command)
if exitCode != 0 {
return nil, &WinrmError{exitCode, "Cannot get a free ip.", stderr}
}
ips := strings.Split(stdout, "\n")
if len(ips) == 0 {
return nil, errors.New("No ip available in dhcp")
}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(ips), func(i, j int) { ips[i], ips[j] = ips[j], ips[i] })
ipv4String := strings.TrimSpace(ips[0])
ipv4 := net.ParseIP(ipv4String)
if ipv4 == nil {
return nil, errors.New("Canot parse ip " + ipv4String)
}
log.Printf("[DEBUG] free ip for scope " + scopeId + " is " + ipv4.String())
return ipv4, nil
}
func (c *Communicator) AddDNSRecordA(zone string, ip net.IP, name string) error {
command := fmt.Sprintf(
"Add-DnsServerResourceRecordA -name \"%s\" -zonename \"%s\" -allowupdateany -ipv4address \"%s\"",
name, zone, ip.String(),
)
_, stderr, exitCode := c.Execute(command)
if exitCode != 0 {
return &WinrmError{exitCode, "Cannot add record A.", stderr}
}
return nil
}
func (c *Communicator) AddDNSRecordPTR(zone string, ip net.IP, name string, ptrArr []string, lastByteArr []string) error {
ptrdomainname := name + "." + zone
for i, j := 0, len(ptrArr)-1; i < j; i, j = i+1, j-1 {
ptrArr[i], ptrArr[j] = ptrArr[j], ptrArr[i]
}
for i, j := 0, len(lastByteArr)-1; i < j; i, j = i+1, j-1 {
lastByteArr[i], lastByteArr[j] = lastByteArr[j], lastByteArr[i]
}
lastByte := strings.Join(lastByteArr,".")
zonename := strings.Join(ptrArr,".") + ".in-addr.arpa"
command := fmt.Sprintf(
"Add-DnsServerResourceRecordPtr -name \"%s\" -zonename \"%s\" -allowupdateany -AgeRecord -PtrDomainName \"%s\"",
lastByte, zonename, ptrdomainname,
)
_, stderr, exitCode := c.Execute(command)
log.Printf("generated command for Add PTR: " + command)
log.Printf(stderr)
if exitCode != 0 {
return &WinrmError{exitCode, "Cannot add record PTR.", stderr}
}
return nil
}
func (c *Communicator) RemoveDNSRecordA(zone string, ip net.IP, name string) error {
command := fmt.Sprintf(
"Remove-DnsServerResourceRecord -zonename \"%s\" -RRType A -Name \"%s\" -RecordData \"%s\" -Force",
zone, name, ip.String(),
)
c.Execute(command)
return nil
}
func (c *Communicator) RemoveDNSRecordPTR(ptrArr []string, lastByteArr []string) error {
for i, j := 0, len(ptrArr)-1; i < j; i, j = i+1, j-1 {
ptrArr[i], ptrArr[j] = ptrArr[j], ptrArr[i]
}
for i, j := 0, len(lastByteArr)-1; i < j; i, j = i+1, j-1 {
lastByteArr[i], lastByteArr[j] = lastByteArr[j], lastByteArr[i]
}
name := strings.Join(lastByteArr,".")
zonename := strings.Join(ptrArr,".") + ".in-addr.arpa"
command := fmt.Sprintf(
"Remove-DnsServerResourceRecord -zonename \"%s\" -RRType Ptr -Name \"%s\" -Force",
zonename, name,
)
_, stderr, _ := c.Execute(command)
log.Printf("generated command for Remove PTR: " + command)
log.Printf(stderr)
return nil
}
func (c *Communicator) Execute(command string) (string, string, int) {
stdout, stderr, returnCode, _ := c.client.RunWithString(winrm.Powershell(command), "")
return stdout, stderr, returnCode
}