Skip to content

Commit 81ee40b

Browse files
authored
other: prepend filters for skip-down-hosts (#171)
* other: prepend filters for skip-down-hosts * tests: remove old optios * tests: fix csv formatter tests * tests: fix html formatter tests * tests: fix md formatter tests
1 parent 997fffd commit 81ee40b

19 files changed

+80
-162
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ More examples can be found on [Usage Wiki page](https://github.com/vdjagilev/nma
8686

8787
### Flags
8888

89-
* `-f, --file [filename]` outputs result to the file (by default output goes to STDOUT)
90-
* `--help` display help message
91-
* `--version` display version (also can be used: `./nmap-formatter version`)
89+
- `-f, --file [filename]` outputs result to the file (by default output goes to STDOUT)
90+
- `--help` display help message
91+
- `--version` display version (also can be used: `./nmap-formatter version`)
92+
- `--skip-down-hosts` skip hosts that are down (by default `true`)
9293

9394
It's also possible to change various output options. More examples on [Usage Wiki Page - Flags](https://github.com/vdjagilev/nmap-formatter/wiki/Usage#flags-and-output-options).
9495

cmd/root.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var config = formatter.Config{
4343
},
4444
ShowVersion: false,
4545
CurrentVersion: VERSION,
46+
SkipDownHosts: true,
4647
FilterExpressions: []string{},
4748
}
4849

@@ -83,10 +84,10 @@ func init() {
8384

8485
// Some options related to the output
8586
// Skip hosts that are down, so they won't be listed in the output
86-
rootCmd.Flags().BoolVar(&config.OutputOptions.HTMLOptions.SkipDownHosts, "html-skip-down-hosts", true, "--html-skip-down-hosts=false, would print all hosts that are offline in HTML output")
87-
rootCmd.Flags().BoolVar(&config.OutputOptions.MarkdownOptions.SkipDownHosts, "md-skip-down-hosts", true, "--md-skip-down-hosts=false, would print all hosts that are offline in Markdown output")
88-
rootCmd.Flags().BoolVar(&config.OutputOptions.CSVOptions.SkipDownHosts, "csv-skip-down-hosts", true, "--csv-skip-down-hosts=false, would print all hosts that are offline in CSV output")
89-
rootCmd.Flags().BoolVar(&config.OutputOptions.ExcelOptions.SkipDownHosts, "excel-skip-down-hosts", true, "--excel-skip-down-hosts=false, would print all hosts that are offline in Excel file")
87+
// rootCmd.Flags().BoolVar(&config.OutputOptions.HTMLOptions.SkipDownHosts, "html-skip-down-hosts", true, "--html-skip-down-hosts=false, would print all hosts that are offline in HTML output")
88+
// rootCmd.Flags().BoolVar(&config.OutputOptions.MarkdownOptions.SkipDownHosts, "md-skip-down-hosts", true, "--md-skip-down-hosts=false, would print all hosts that are offline in Markdown output")
89+
// rootCmd.Flags().BoolVar(&config.OutputOptions.CSVOptions.SkipDownHosts, "csv-skip-down-hosts", true, "--csv-skip-down-hosts=false, would print all hosts that are offline in CSV output")
90+
// rootCmd.Flags().BoolVar(&config.OutputOptions.ExcelOptions.SkipDownHosts, "excel-skip-down-hosts", true, "--excel-skip-down-hosts=false, would print all hosts that are offline in Excel file")
9091

9192
// Skip header information (overall meta information from the scan)
9293
rootCmd.Flags().BoolVar(&config.OutputOptions.HTMLOptions.SkipHeader, "html-skip-header", false, "--html-skip-header, skips header in HTML output")
@@ -124,7 +125,8 @@ func init() {
124125
rootCmd.Flags().StringVar(&config.OutputOptions.SqliteOutputOptions.ScanIdentifier, "scan-id", "", "--scan-id abc123")
125126

126127
// Configs related to D2 language
127-
rootCmd.Flags().BoolVar(&config.OutputOptions.D2LangOptions.SkipDownHosts, "d2-skip-down-hosts", true, "--d2-skip-down-hosts=false, would print all hosts that are offline in D2 language output")
128+
// rootCmd.Flags().BoolVar(&config.OutputOptions.D2LangOptions.SkipDownHosts, "d2-skip-down-hosts", true, "--d2-skip-down-hosts=false, would print all hosts that are offline in D2 language output")
129+
rootCmd.Flags().BoolVar(&config.SkipDownHosts, "skip-down-hosts", false, "--skip-down-hosts=true, skips hosts that are offline")
128130

129131
// Multiple filter expressions supported
130132
rootCmd.Flags().StringArrayVar(&config.FilterExpressions, "filter", []string{}, "--filter '.Status.State == \"up\" && any(.Port, { .PortID in [80,443] })'")

cmd/validation.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// validate is checking input from the command line
1111
func validate(config formatter.Config) error {
1212
if !config.OutputFormat.IsValid() {
13-
return fmt.Errorf("not valid format: %s, please choose html/json/md/csv/excel/sqlite", config.OutputFormat)
13+
return fmt.Errorf("not valid format: %s, please choose html/json/md/csv/excel/sqlite/d2", config.OutputFormat)
1414
}
1515

1616
err := validateIOFiles(config)

formatter/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Config struct {
1919
TemplatePath string
2020
CustomOptions []string
2121
CurrentVersion string
22+
SkipDownHosts bool
2223
FilterExpressions []string
2324
}
2425

formatter/formatter_csv.go

-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ func (f *CSVFormatter) convert(td *TemplateData) (data [][]string) {
2020
data = append(data, []string{"IP", "Port", "Protocol", "State", "Service", "Reason", "Product", "Version", "Extra info"})
2121
for i := range td.NMAPRun.Host {
2222
var host *Host = &td.NMAPRun.Host[i]
23-
// shouldSkip := host.ShouldSkipHost(td.OutputOptions.CSVOptions.SkipDownHosts)
24-
if host.ShouldSkipHost(td.OutputOptions.CSVOptions.SkipDownHosts) {
25-
continue
26-
}
2723
address := fmt.Sprintf("%s (%s)", host.JoinedAddresses("/"), host.Status.State)
2824
data = append(data, []string{address, "", "", "", "", "", "", "", ""})
2925
for j := range host.Port {

formatter/formatter_csv_test.go

+36-67
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,24 @@ func TestCSVFormatter_convert(t *testing.T) {
3131
},
3232
},
3333
{
34-
name: "1 Host is down (skip down hosts)",
34+
name: "1 Host is down",
3535
f: &CSVFormatter{},
3636
args: args{
3737
td: &TemplateData{
3838
NMAPRun: NMAPRun{
3939
Host: []Host{
4040
{
41-
StartTime: 0,
42-
EndTime: 0,
43-
Port: []Port{},
44-
HostAddress: []HostAddress{},
45-
HostNames: HostNames{},
41+
StartTime: 0,
42+
EndTime: 0,
43+
Port: []Port{},
44+
HostAddress: []HostAddress{
45+
{
46+
Address: "127.0.0.1",
47+
AddressType: "ipv4",
48+
Vendor: "",
49+
},
50+
},
51+
HostNames: HostNames{},
4652
Status: HostStatus{
4753
State: "down",
4854
},
@@ -57,48 +63,17 @@ func TestCSVFormatter_convert(t *testing.T) {
5763
},
5864
},
5965
OutputOptions: OutputOptions{
60-
CSVOptions: CSVOutputOptions{
61-
SkipDownHosts: true,
62-
},
63-
},
64-
},
65-
},
66-
wantData: [][]string{
67-
header,
68-
},
69-
},
70-
{
71-
name: "2 Hosts are down (skip down hosts)",
72-
f: &CSVFormatter{},
73-
args: args{
74-
td: &TemplateData{
75-
NMAPRun: NMAPRun{
76-
Host: []Host{
77-
{
78-
Status: HostStatus{
79-
State: "down",
80-
},
81-
},
82-
{
83-
Status: HostStatus{
84-
State: "down",
85-
},
86-
},
87-
},
88-
},
89-
OutputOptions: OutputOptions{
90-
CSVOptions: CSVOutputOptions{
91-
SkipDownHosts: true,
92-
},
66+
CSVOptions: CSVOutputOptions{},
9367
},
9468
},
9569
},
9670
wantData: [][]string{
9771
header,
72+
{"127.0.0.1 (down)", "", "", "", "", "", "", "", ""},
9873
},
9974
},
10075
{
101-
name: "1 host is down (do not skip down hosts)",
76+
name: "1 host is down",
10277
f: &CSVFormatter{},
10378
args: args{
10479
td: &TemplateData{
@@ -128,9 +103,7 @@ func TestCSVFormatter_convert(t *testing.T) {
128103
},
129104
},
130105
OutputOptions: OutputOptions{
131-
CSVOptions: CSVOutputOptions{
132-
SkipDownHosts: false,
133-
},
106+
CSVOptions: CSVOutputOptions{},
134107
},
135108
},
136109
},
@@ -185,9 +158,7 @@ func TestCSVFormatter_convert(t *testing.T) {
185158
},
186159
},
187160
OutputOptions: OutputOptions{
188-
CSVOptions: CSVOutputOptions{
189-
SkipDownHosts: true,
190-
},
161+
CSVOptions: CSVOutputOptions{},
191162
},
192163
},
193164
},
@@ -257,9 +228,7 @@ func TestCSVFormatter_convert(t *testing.T) {
257228
},
258229
},
259230
OutputOptions: OutputOptions{
260-
CSVOptions: CSVOutputOptions{
261-
SkipDownHosts: true,
262-
},
231+
CSVOptions: CSVOutputOptions{},
263232
},
264233
},
265234
},
@@ -271,7 +240,7 @@ func TestCSVFormatter_convert(t *testing.T) {
271240
},
272241
},
273242
{
274-
name: "1 host up 2 ports, 1 host down (skip-down-hosts=true)",
243+
name: "1 host up 2 ports, 1 host down",
275244
f: &CSVFormatter{},
276245
args: args{
277246
td: &TemplateData{
@@ -351,9 +320,7 @@ func TestCSVFormatter_convert(t *testing.T) {
351320
},
352321
},
353322
OutputOptions: OutputOptions{
354-
CSVOptions: CSVOutputOptions{
355-
SkipDownHosts: true,
356-
},
323+
CSVOptions: CSVOutputOptions{},
357324
},
358325
},
359326
},
@@ -362,11 +329,14 @@ func TestCSVFormatter_convert(t *testing.T) {
362329
{"127.0.0.1 (up)", "", "", "", "", "", "", "", ""},
363330
{"", "80", "tcp", "open", "http", "syn-ack", "nginx", "1.21.1", ""},
364331
{"", "443", "tcp", "open", "http", "syn-ack", "nginx", "1.21.1", ""},
332+
{"192.168.1.1 (down)", "", "", "", "", "", "", "", ""},
365333
},
366334
},
367335
{
368-
name: "1 host up 2 ports, 1 host down (skip-down-hosts=false)",
369-
f: &CSVFormatter{},
336+
name: "1 host up 2 ports, 1 host down",
337+
f: &CSVFormatter{
338+
config: &Config{},
339+
},
370340
args: args{
371341
td: &TemplateData{
372342
NMAPRun: NMAPRun{
@@ -445,9 +415,7 @@ func TestCSVFormatter_convert(t *testing.T) {
445415
},
446416
},
447417
OutputOptions: OutputOptions{
448-
CSVOptions: CSVOutputOptions{
449-
SkipDownHosts: false,
450-
},
418+
CSVOptions: CSVOutputOptions{},
451419
},
452420
},
453421
},
@@ -460,8 +428,12 @@ func TestCSVFormatter_convert(t *testing.T) {
460428
},
461429
},
462430
{
463-
name: "2 host up (2+1 ports) (skip-down-hosts=true)",
464-
f: &CSVFormatter{},
431+
name: "2 hosts (2+1 ports)",
432+
f: &CSVFormatter{
433+
config: &Config{
434+
SkipDownHosts: true,
435+
},
436+
},
465437
args: args{
466438
td: &TemplateData{
467439
NMAPRun: NMAPRun{
@@ -555,9 +527,7 @@ func TestCSVFormatter_convert(t *testing.T) {
555527
},
556528
},
557529
OutputOptions: OutputOptions{
558-
CSVOptions: CSVOutputOptions{
559-
SkipDownHosts: false,
560-
},
530+
CSVOptions: CSVOutputOptions{},
561531
},
562532
},
563533
},
@@ -596,7 +566,8 @@ func TestCSVFormatter_Format(t *testing.T) {
596566
name: "Successful header write",
597567
f: &CSVFormatter{
598568
config: &Config{
599-
Writer: writer,
569+
Writer: writer,
570+
SkipDownHosts: true,
600571
},
601572
},
602573
args: args{
@@ -605,9 +576,7 @@ func TestCSVFormatter_Format(t *testing.T) {
605576
Host: []Host{},
606577
},
607578
OutputOptions: OutputOptions{
608-
CSVOptions: CSVOutputOptions{
609-
SkipDownHosts: true,
610-
},
579+
CSVOptions: CSVOutputOptions{},
611580
},
612581
},
613582
},

formatter/formatter_d2.go

-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ func (f *D2LangFormatter) Format(td *TemplateData, templateContent string) (err
3737
host := &td.NMAPRun.Host[i]
3838
fnv := fnv.New128()
3939

40-
if host.ShouldSkipHost(td.OutputOptions.D2LangOptions.SkipDownHosts) {
41-
continue
42-
}
43-
4440
address := host.JoinedAddresses("/")
4541
hostnames := host.JoinedHostNames("/")
4642
hostLabel := address

formatter/formatter_dot.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ type DotFormatter struct {
1313
config *Config
1414
}
1515

16-
//go:embed resources/templates/graphviz.tmpl
1716
// DotTemplate variable is used to store contents of graphviz template
17+
//
18+
//go:embed resources/templates/graphviz.tmpl
1819
var DotTemplate string
1920

2021
const (

formatter/formatter_excel.go

-5
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ func (f *ExcelFormatter) writeHostRows(h []Host, cd *CellData) error {
7474

7575
for i := range h {
7676
host := h[i]
77-
78-
if host.ShouldSkipHost(f.config.OutputOptions.ExcelOptions.SkipDownHosts) {
79-
continue
80-
}
81-
8277
joinedAddresses := host.JoinedAddresses("/")
8378
joinedHostnames := host.JoinedHostNames("/")
8479
addressFormat := "%s [%s]"

formatter/formatter_html.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ type HTMLFormatter struct {
1111
config *Config
1212
}
1313

14-
//go:embed resources/templates/simple-html.gohtml
1514
// HTMLSimpleTemplate variable is used to store embedded HTML template content
15+
//
16+
//go:embed resources/templates/simple-html.gohtml
1617
var HTMLSimpleTemplate string
1718

1819
// Format the data and output it to appropriate io.Writer

0 commit comments

Comments
 (0)