-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.go
More file actions
91 lines (75 loc) · 1.87 KB
/
device.go
File metadata and controls
91 lines (75 loc) · 1.87 KB
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
package zteScanner
import (
"context"
"encoding/xml"
"time"
probing "github.com/prometheus-community/pro-bing"
)
type Device struct {
MacAddress string
IPAddress string
Hostname string
}
type Devices []Device
type DevicesInfo struct {
XMLName xml.Name `xml:"ajax_response_xml_root"`
Text string `xml:",chardata"`
WlanID struct {
Text string `xml:",chardata"`
Instance []struct {
Text string `xml:",chardata"`
ParaName []string `xml:"ParaName"`
ParaValue []string `xml:"ParaValue"`
} `xml:"Instance"`
} `xml:"OBJ_WLAN_AD_ID"`
LanID struct {
Text string `xml:",chardata"`
Instance []struct {
Text string `xml:",chardata"`
ParaName []string `xml:"ParaName"`
ParaValue []string `xml:"ParaValue"`
} `xml:"Instance"`
} `xml:"OBJ_WLANAP_ID"`
}
func parseDevicesInfo(devicesInfo DevicesInfo) (Devices, error) {
var devices Devices
var device Device
requiredDataCount := 3
for _, instance := range devicesInfo.WlanID.Instance {
extractedDataCount := 0
for i := range instance.ParaName {
if extractedDataCount == requiredDataCount {
devices = append(devices, device)
break
}
if instance.ParaName[i] == "MACAddress" {
extractedDataCount += 1
device.MacAddress = instance.ParaValue[i]
}
if instance.ParaName[i] == "HostName" {
extractedDataCount += 1
device.Hostname = instance.ParaValue[i]
}
if instance.ParaName[i] == "IPAddress" {
extractedDataCount += 1
device.IPAddress = instance.ParaValue[i]
}
}
}
return devices, nil
}
// Ping th e device for 1 second
func (d Device) IsAlive() bool {
pinger, err := probing.NewPinger(d.IPAddress)
if err != nil {
return false
}
pinger.Count = 1
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err = pinger.RunWithContext(ctx)
if err != nil {
return false
}
return true
}