A weekend project to scratch an itch: I wanted a port sweeper with sharp jagged edges removed and readable output, written in Go.
Design goals
- Fully concurrent with bounded worker pools
- No external dependencies - standard library only
How it works
scanner, err := NewPortSweeper(cfg)
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(2)
}
ctx, stop := signal.NotifyContext(
context.Background(),
os.Interrupt,
syscall.SIGTERM,
)
defer stop()
fmt.Printf(
"PortSweeper | host=%s | ports=%d | workers=%d | timeout=%s\n\n",
cfg.Host,
len(cfg.Ports),
cfg.Workers,
cfg.Timeout,
)
var results []PortResult
for result := range scanner.Scan(ctx) {
results = append(results, result)
if result.Status == StatusOpen || *showClosed {
fmt.Printf(
"%-16s %-6d %-18s %s\n",
result.Host,
result.Port,
result.Status,
result.Duration,
)
}
}
sort.Slice(results, func(i, j int) bool {
return results[i].Port < results[j].Port
})
openCount, closedCount, filteredCount := 0, 0, 0
for _, result := range results {
switch result.Status {
case StatusOpen:
openCount++
case StatusClosed:
closedCount++
default:
filteredCount++
}
}
fmt.Println("\n-- Summary--")
fmt.Printf("Completed: %d / %d\n", len(results), len(cfg.Ports))
fmt.Printf("Open: %d\n", openCount)
fmt.Printf("Closed: %d\n", closedCount)
fmt.Printf("Filtered/Unknown: %d\n", filteredCount)
if ctx.Err() != nil {
fmt.Println("Scan interrupted.")
}
What’s next
- Service banners on open ports?
- Export to CSV/JSON?
- Pull requests welcomed!