Skip to content

Commit

Permalink
Make streamer example simpler by passing the output to Stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
elivlo committed Apr 11, 2023
1 parent 7aeda92 commit 0ffaea3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Most pentest tools are currently written using Python and not Go, because it is
- [x] Run a nmap scan asynchronously.
- [x] Scan progress may be piped.
- [x] Write the nmap output to a given file while also parsing it to the struct.
- [x] Stream the nmap output to an `io.Writer` while also parsing it to the struct.
- [x] Stream the nmap output to an `io.Writer` interface while also parsing it to the struct.
- [x] Functionality to show local interfaces and routes.

## Simple example
Expand Down
32 changes: 2 additions & 30 deletions examples/basic_scan_streamer_interface/main.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,14 @@
package main

import (
"bytes"
"context"
"fmt"
"github.com/Ullaakut/nmap/v3"
"io"
"log"
"os"
)

// CustomType is your custom type in code.
// You just have to make it a Streamer.
type CustomType struct {
io.Writer
buf bytes.Buffer
}

// Write is a function that handles the normal nmap stdout.
func (c *CustomType) Write(d []byte) (int, error) {
lines := string(d)
fmt.Print(lines)
return c.buf.Write(d)
}

// Bytes returns scan result bytes.
func (c *CustomType) Bytes() []byte {
return c.buf.Bytes()
}

func main() {
cType := &CustomType{}
scanner, err := nmap.NewScanner(
context.Background(),
nmap.WithTargets("localhost"),
Expand All @@ -43,19 +22,12 @@ func main() {

var result nmap.Run
var warnings []string
err = scanner.Streamer(cType).Run(&result, &warnings)
err = scanner.Streamer(os.Stdout).Run(&result, &warnings)
if err != nil {
log.Fatalf("unable to run nmap scan: %v", err)
}

fmt.Printf("Nmap warnings: %v\n", warnings)

var result2 nmap.Run
err = nmap.Parse(cType.Bytes(), &result2)
if err != nil {
log.Fatalf("unable to parse nmap output: %v", err)
}

fmt.Printf("Nmap done: %d hosts up scanned in %.2f seconds\n", len(result.Hosts), result.Stats.Finished.Elapsed)
fmt.Printf("Streamer done: %d hosts up scanned in %.2f seconds\n", len(result2.Hosts), result.Stats.Finished.Elapsed)
}

0 comments on commit 0ffaea3

Please sign in to comment.