-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathmain.go
More file actions
55 lines (46 loc) · 1.19 KB
/
main.go
File metadata and controls
55 lines (46 loc) · 1.19 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
package main
import (
"context"
"log"
"github.com/Ullaakut/nmap/v4"
)
func main() {
// Equivalent to
// nmap -sV -T4 scanme.nmap.org with a filter to remove hosts without open ports.
scanner, err := nmap.NewScanner(
nmap.WithTargets("scanme.nmap.org"),
nmap.WithPorts("22", "80", "443"),
nmap.WithServiceInfo(),
nmap.WithTimingTemplate(nmap.TimingAggressive),
// Filter out hosts that don't have any open ports
nmap.WithFilterHost(func(h nmap.Host) bool {
// Filter out hosts with no open ports.
for idx := range h.Ports {
if h.Ports[idx].Status() == "open" {
return true
}
}
return false
}),
)
if err != nil {
log.Fatalf("creating nmap scanner: %v", err)
}
result, err := scanner.Run(context.Background())
if err != nil {
log.Fatalf("running network scan: %v", err)
}
warnings := result.Warnings()
if len(warnings) > 0 {
log.Printf("warning: %v\n", warnings) // Warnings are non-critical errors from nmap.
}
for _, host := range result.Hosts {
log.Printf("Host %s\n", host.Addresses[0])
for _, port := range host.Ports {
if port.Status() != "open" {
continue
}
log.Printf("\tPort %d open (%s)\n", port.ID, port.Service.Name)
}
}
}