-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnmap.go
More file actions
83 lines (72 loc) · 1.93 KB
/
nmap.go
File metadata and controls
83 lines (72 loc) · 1.93 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"math"
"strings"
"time"
"log"
"io"
"github.com/Ullaakut/nmap"
)
func main() {
// Start the scan clock
start:= time.Now()
// Setup log file
logfile, err := os.OpenFile("scan.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
fmt.Println(err)
return
}
defer logfile.Close()
mw := io.MultiWriter(os.Stdout, logfile)
log.SetOutput(mw)
// Open the CSV file
file, err := os.Open("ips.csv")
if err != nil {
log.Fatalf("Failure to open CSV: %v", err)
return
}
defer file.Close()
// Read the CSV file
reader := csv.NewReader(file)
records, err := reader.ReadAll()
if err != nil {
log.Println(err)
return
}
// Loop through the records and scan each IP address
for _, record := range records {
// Create a new Nmap scan
scan, _ := nmap.NewScanner(
nmap.WithTargets(record[0]),
nmap.WithPorts("1-65535"),
nmap.WithServiceInfo(),
)
// Run the scan
result, warning, err := scan.Run()
if err != nil {
log.Fatalf("Failed to run scan: %v", err)
continue
}
if warning != nil {
log.Printf("Warnings: \n %v", warning)
}
// Get output JSON
jsonOutput, _ := json.MarshalIndent(result, "", " ")
elapsed := math.Round(time.Since(start).Minutes())
ip := strings.Replace(record[0], "/", "-", -1)
log.Printf("Scan on %s took %.0f minutes\n", record[0], elapsed)
// Write to a file
fileName := fmt.Sprintf("%s-results.json", ip)
f, _ := os.Create(fileName)
if err != nil {
log.Fatalf("Failed to create results: %v", err)
return
}
defer f.Close()
f.Write(jsonOutput)
}
}