-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiflist.go
79 lines (60 loc) · 1.33 KB
/
iflist.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
package masscan
import (
"bytes"
"os/exec"
"regexp"
"strconv"
"strings"
)
type InterfaceList struct {
Interfaces []*Interface `json:"interfaces"`
}
type Interface struct {
Index int
IFace string
Description string
}
func (m *MasscanScanner) GetInterfaceList() (ifaces *InterfaceList, err error) {
var stdout, stderr bytes.Buffer
args := append(m.args, "--iflist")
cmd := exec.Command(m.masscanPath, args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
return nil, err
}
ifaces = parseInterfaces(stdout.Bytes())
return ifaces, nil
}
func parseInterfaces(content []byte) *InterfaceList {
ifaces := InterfaceList{
Interfaces: make([]*Interface, 0),
}
output := string(content)
lines := strings.Split(output, "\n")
for i, line := range lines {
if match, _ := regexp.MatchString("^[0-9]+$", line); match {
for _, li := range lines[i+2:] {
if iface := converInterface(li); iface != nil {
ifaces.Interfaces = append(ifaces.Interfaces, iface)
}
}
}
}
return &ifaces
}
func converInterface(line string) *Interface {
fields := strings.Fields(line)
if len(fields) < 3 {
return nil
}
iface := &Interface{
IFace: fields[1],
Description: fields[2],
}
if value, err := strconv.Atoi(fields[0]); err != nil {
iface.Index = value
}
return iface
}