-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
329 lines (292 loc) · 6.74 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
"crypto/tls"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/cheggaaa/pb"
"github.com/olekukonko/tablewriter"
"github.com/parnurzeal/gorequest"
)
const (
iloPort = 17988
notAvailable = "N/A"
)
var (
ipNetParsed []string
)
// ILOInfo ...
type ILOInfo struct {
IP string
HW string
Model string
FW string
Serial string
ServerName string
IloName string
}
// ILOSorter ...
type ILOSorter struct {
ilo []ILOInfo
by func(i1, i2 *ILOInfo) bool
}
// By is the type of a "less" function that defines the ordering of its Planet arguments.
type By func(i1, i2 *ILOInfo) bool
// Sort is a method on the function type, By, that sorts the argument slice according to the function.
func (by By) Sort(ilo []ILOInfo) {
ps := &ILOSorter{
ilo: ilo,
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
}
sort.Sort(ps)
}
// Len is part of sort.Interface.
func (s *ILOSorter) Len() int {
return len(s.ilo)
}
// Swap is part of sort.Interface.
func (s *ILOSorter) Swap(i, j int) {
s.ilo[i], s.ilo[j] = s.ilo[j], s.ilo[i]
}
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s *ILOSorter) Less(i, j int) bool {
return s.by(&s.ilo[i], &s.ilo[j])
}
// RIMP ...
type RIMP struct {
XMLName xml.Name `xml:"RIMP"`
SBSN string `xml:"HSI>SBSN"`
SPN string `xml:"HSI>SPN"`
PN string `xml:"MP>PN"`
FWRI string `xml:"MP>FWRI"`
HWRI string `xml:"MP>HWRI"`
}
// ServerName ...
type ServerName struct {
Name string `json:"server_name"`
Cn string `json:"cn"`
}
// HW ...
func (r *RIMP) HW() string {
var iloRevision = regexp.MustCompile(`\((.*)\)`)
if len(r.PN) == 0 {
return notAvailable
}
return strings.TrimSpace(iloRevision.FindAllStringSubmatch(r.PN, 1)[0][1])
}
// Model ...
func (r *RIMP) Model() string {
if len(r.SPN) == 0 {
return notAvailable
}
return strings.TrimSpace(r.SPN)
}
// FW ...
func (r *RIMP) FW() string {
if len(r.FWRI) == 0 {
return notAvailable
}
return strings.TrimSpace(r.FWRI)
}
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
// IsOpen ...
func IsOpen(host string, port int) bool {
tcpAddr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%s:%d", host, port))
if err != nil {
return false
}
conn, err := net.DialTimeout("tcp", tcpAddr.String(), 250*time.Millisecond)
if err != nil {
return false
}
defer conn.Close()
return true
}
func requestServerNameV2(ip string) (string, string, error) {
request := gorequest.New()
_, body, err := request.Get(fmt.Sprintf("http://%s/", ip)).End()
if err != nil {
return "", "", fmt.Errorf("%v", err)
}
reSrv := regexp.MustCompile(`serverName\="([\w-]+)"`)
reIlo := regexp.MustCompile(`nicName\="([\w-]+)"`)
matchSrv := reSrv.FindStringSubmatch(string(body))
matchIlo := reIlo.FindStringSubmatch(string(body))
serverName := ""
iloName := ""
if len(matchSrv) > 0 {
serverName = matchSrv[1]
}
if len(matchIlo) > 0 {
iloName = matchIlo[1]
}
return serverName, iloName, nil
}
func requestServerName(ip string) (string, string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/json/login_session?null", ip), nil)
req.Header.Set("Content-Type", "application/json")
if err != nil {
return "", "", err
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Do(req)
if err != nil {
return "", "", err
}
raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", "", err
}
srvinfo := &ServerName{}
if err := json.Unmarshal(raw, srvinfo); err != nil {
return "", "", err
}
//fmt.Println(srvinfo.ServerName)
return srvinfo.Name, srvinfo.Cn, nil
}
func requestInfo(ip string) (*ILOInfo, error) {
request := gorequest.New()
rinfo := &RIMP{}
_, body, err := request.Get(fmt.Sprintf("http://%s/xmldata?item=all", ip)).End()
if err != nil {
return nil, fmt.Errorf("%v", err)
}
if err := xml.Unmarshal([]byte(body), rinfo); err != nil {
return nil, err
}
return &ILOInfo{
IP: ip,
HW: rinfo.HW(),
FW: rinfo.FW(),
Model: rinfo.Model(),
Serial: strings.TrimSpace(rinfo.SBSN),
}, nil
}
func makeJobs(ar []string, count int) [][]string {
chunk := len(ar) / count
start := 0
end := count
res := [][]string{}
for end < len(ar) {
res = append(res, ar[start:end])
start = end
end += chunk
}
res = append(res, ar[start:len(ar)])
return res
}
func tableRender(ilo []ILOInfo) {
data := [][]string{}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"IP", "HW", "FW", "S/N", "Model", "ServerName", "Name"})
table.SetBorder(false) // Set Border to false
version := func(i1, i2 *ILOInfo) bool {
i1s := strings.Split(i1.HW, " ")
i2s := strings.Split(i2.HW, " ")
i1v := 1
i2v := 1
if len(i1s) > 1 {
i1v, _ = strconv.Atoi(i1s[1])
}
if len(i2s) > 1 {
i2v, _ = strconv.Atoi(i2s[1])
}
return i1v < i2v
}
By(version).Sort(ilo)
for _, info := range ilo {
data = append(data, []string{
info.IP,
info.HW,
info.FW,
info.Serial,
info.Model,
info.ServerName,
info.IloName,
})
}
table.AppendBulk(data) // Add Bulk Data
fmt.Println("")
table.Render()
}
func scan(ips []string, out chan ILOInfo, bar *pb.ProgressBar, wg *sync.WaitGroup) {
for _, host := range ips {
if IsOpen(host, iloPort) {
srvName := ""
iloName := ""
info, err := requestInfo(host)
if err != nil {
fmt.Println(err)
}
if match, _ := regexp.MatchString("iLO (3|4|5)", info.HW); match {
srvName, iloName, _ = requestServerName(host)
} else {
srvName, iloName, _ = requestServerNameV2(host)
}
info.ServerName = srvName
info.IloName = iloName
out <- *info
}
bar.Increment()
}
wg.Done()
}
func main() {
if len(os.Args) == 1 {
fmt.Printf("Usage: findilo <networks>, Format 10.0.0.0/24\n")
os.Exit(1)
}
var ips []string
for _, ipNetwork := range os.Args[1:] {
ip, ipnet, err := net.ParseCIDR(ipNetwork)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
ips = append(ips, ip.String())
}
}
ipNetParsed = ips
jobs := makeJobs(ipNetParsed, 100)
out := make(chan ILOInfo, 100)
ipNetLen := len(ipNetParsed)
scanbar := pb.StartNew(ipNetLen)
scanbar = scanbar.Prefix("Scan net")
scanbar.ShowTimeLeft = false
wg := new(sync.WaitGroup)
//Запуск воркеров
for _, job := range jobs {
wg.Add(1)
go scan(job, out, scanbar, wg)
}
wg.Wait()
close(out)
ilo := []ILOInfo{}
for info := range out {
ilo = append(ilo, info)
}
scanbar.Finish()
tableRender(ilo)
fmt.Println("")
}