-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathformatter_csv.go
52 lines (47 loc) · 1.43 KB
/
formatter_csv.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 formatter
import (
"encoding/csv"
"fmt"
)
// CSVFormatter is struct defined for CSV Output use-case
type CSVFormatter struct {
config *Config
}
// Format the data to CSV and output it to appropriate io.Writer
func (f *CSVFormatter) Format(td *TemplateData, templateContent string) (err error) {
return csv.NewWriter(f.config.Writer).WriteAll(f.convert(td))
}
// convert uses NMAPRun struct to convert all data to [][]string type
func (f *CSVFormatter) convert(td *TemplateData) (data [][]string) {
data = append(data, []string{"IP", "Port", "Protocol", "State", "Service", "Reason", "Product", "Version", "Extra info"})
for i := range td.NMAPRun.Host {
var host *Host = &td.NMAPRun.Host[i]
// shouldSkip := host.ShouldSkipHost(td.OutputOptions.CSVOptions.SkipDownHosts)
if host.ShouldSkipHost(td.OutputOptions.CSVOptions.SkipDownHosts) {
continue
}
address := fmt.Sprintf("%s (%s)", host.JoinedAddresses("/"), host.Status.State)
data = append(data, []string{address, "", "", "", "", "", "", "", ""})
for j := range host.Port {
var port *Port = &host.Port[j]
data = append(
data,
[]string{
"",
fmt.Sprint(port.PortID),
port.Protocol,
port.State.State,
port.Service.Name,
port.State.Reason,
port.Service.Product,
port.Service.Version,
port.Service.ExtraInfo,
},
)
}
}
return
}
func (f *CSVFormatter) defaultTemplateContent() string {
return HTMLSimpleTemplate
}