Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JSON output #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

var debug bool
var jsonOutput bool
var timeoutMS int = 2000
var parallelism int = 500
var portSelection string
Expand All @@ -28,6 +29,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&versionRequested, "version", "", versionRequested, "Output version information and exit")
rootCmd.PersistentFlags().StringVarP(&scanType, "scan-type", "s", scanType, "Scan type. Must be one of stealth, connect")
rootCmd.PersistentFlags().BoolVarP(&debug, "verbose", "v", debug, "Enable verbose logging")
rootCmd.PersistentFlags().BoolVarP(&jsonOutput, "json", "j", jsonOutput, "Enable JSON output")
rootCmd.PersistentFlags().IntVarP(&timeoutMS, "timeout-ms", "t", timeoutMS, "Scan timeout in MS")
rootCmd.PersistentFlags().IntVarP(&parallelism, "workers", "w", parallelism, "Parallel routines to scan on")
rootCmd.PersistentFlags().StringVarP(&portSelection, "ports", "p", portSelection, "Port to scan. Comma separated, can sue hyphens e.g. 22,80,443,8080-8090")
Expand Down Expand Up @@ -90,8 +92,9 @@ var rootCmd = &cobra.Command{
}()

startTime := time.Now()
fmt.Printf("\nStarting scan at %s\n\n", startTime.String())

if !jsonOutput {
fmt.Printf("\nStarting scan at %s\n\n", startTime.String())
}
for _, target := range args {

targetIterator := scan.NewTargetIterator(target)
Expand All @@ -118,15 +121,20 @@ var rootCmd = &cobra.Command{
}

for _, result := range results {
if jsonOutput {
scanner.OutputResultJSON(result)
continue
}
if !hideUnavailableHosts || result.IsHostUp() {
scanner.OutputResult(result)
}
}

}

fmt.Printf("Scan complete in %s.\n", time.Since(startTime).String())

if !jsonOutput {
fmt.Printf("Scan complete in %s.\n", time.Since(startTime).String())
}
},
}

Expand Down
6 changes: 6 additions & 0 deletions scan/scan-connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scan

import (
"context"
"encoding/json"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -197,3 +198,8 @@ func (s *ConnectScanner) scanPort(target net.IP, port int) (PortState, error) {
func (s *ConnectScanner) OutputResult(result Result) {
fmt.Println(result.String())
}

func (s *ConnectScanner) OutputResultJSON(result Result) {
resultJSON, _ := json.Marshal(&result)
fmt.Println(string(resultJSON))
}
6 changes: 6 additions & 0 deletions scan/scan-device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scan

import (
"context"
"encoding/json"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -181,3 +182,8 @@ func (s *DeviceScanner) OutputResult(result Result) {

fmt.Println("")
}

func (s *DeviceScanner) OutputResultJSON(result Result) {
resultJSON, _ := json.Marshal(&result)
fmt.Println(string(resultJSON))
}
6 changes: 6 additions & 0 deletions scan/scan-syn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scan

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -411,3 +412,8 @@ func (s *SynScanner) scanHost(job hostJob) (Result, error) {
func (s *SynScanner) OutputResult(result Result) {
fmt.Println(result.String())
}

func (s *SynScanner) OutputResultJSON(result Result) {
resultJSON, _ := json.Marshal(&result)
fmt.Println(string(resultJSON))
}
1 change: 1 addition & 0 deletions scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ type Scanner interface {
Start() error
Scan(ctx context.Context, ports []int) ([]Result, error)
OutputResult(result Result)
OutputResultJSON(result Result)
}