-
Notifications
You must be signed in to change notification settings - Fork 106
/
main.go
57 lines (48 loc) · 1.18 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
package main
import (
"context"
"fmt"
"log"
"github.com/Ullaakut/nmap/v3"
osfamily "github.com/Ullaakut/nmap/v3/pkg/osfamilies"
)
func main() {
// Equivalent to
// nmap -F -O 192.168.0.0/24
scanner, err := nmap.NewScanner(
context.Background(),
nmap.WithTargets("192.168.0.0/24"),
nmap.WithFastMode(),
nmap.WithOSDetection(), // Needs to run with sudo
)
if err != nil {
log.Fatalf("unable to create nmap scanner: %v", err)
}
result, warnings, err := scanner.Run()
if len(*warnings) > 0 {
log.Printf("run finished with warnings: %s\n", *warnings) // Warnings are non-critical errors from nmap.
}
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)
}