-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweeper.go
55 lines (48 loc) · 1.02 KB
/
sweeper.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
package ilo
import (
"net"
"sort"
"time"
"github.com/jonstacks/goutils/netutils"
)
// Sweeper is a construct for sweeping a subnet for ILO Devices
type Sweeper struct {
Subnet string // Subnet CIDR
Network *net.IPNet // IP mask
}
// NewSweeper creates & returns a pointer to a new Sweeper
func NewSweeper(subnet string) (*Sweeper, error) {
_, ipNet, err := net.ParseCIDR(subnet)
if err != nil {
return nil, err
}
return &Sweeper{subnet, ipNet}, nil
}
// Sweep performs the sweep on the Sweeper's subnet
func (s Sweeper) Sweep(timeout time.Duration) []Info {
var infos []Info
reply := make(chan *Info)
quit := time.After(timeout)
// Spawn all the clients
for ip := range netutils.IPNetwork(s.Network) {
go func(x net.IP) {
c := NewClient(x.String())
info, err := c.GetInfo()
if err == nil {
reply <- info
}
}(ip)
}
Gather:
for {
select {
case <-quit:
break Gather
case i := <-reply:
// Got a new ilo result
infos = append(infos, *i)
}
}
sort.Sort(ByHost(infos))
return infos
}