forked from Ullaakut/nmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (43 loc) · 1002 Bytes
/
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
package main
import (
"fmt"
"log"
"github.com/Ullaakut/nmap/v2"
osfamily "github.com/Ullaakut/nmap/v2/pkg/osfamilies"
)
func main() {
// Equivalent to
// nmap -F -O 192.168.0.0/24
scanner, err := nmap.NewScanner(
nmap.WithTargets("192.168.0.0/24"),
nmap.WithFastMode(),
nmap.WithOSDetection(),
)
if err != nil {
log.Fatalf("unable to create nmap scanner: %v", err)
}
result, _, err := scanner.Run()
if err != nil {
log.Fatalf("nmap scan failed: %v", err)
}
countByOS(result)
}
func countByOS(result *nmap.Run) {
var (
linux, windows int
)
// Count the number of each OS for all hosts.
for _, host := range result.Hosts {
for _, match := range host.OS.Matches {
for _, class := range match.Classes {
switch class.OSFamily() {
case osfamily.Linux:
linux++
case osfamily.Windows:
windows++
}
}
}
}
fmt.Printf("Discovered %d linux hosts and %d windows hosts out of %d total up hosts.\n", linux, windows, result.Stats.Hosts.Up)
}