A command-line network port scanner built in Go that demonstrates fundamental networking concepts and sequential programming patterns. This tool scans TCP ports on specified IP addresses without using goroutines, making it an excellent showcase of basic networking I/O and loop structures.
This project was designed as a sequential implementation to focus on:
- Core networking concepts using Go's
netpackage - Proper error handling patterns
- Clean command-line interface design
- Structured code organization
Why Sequential? This implementation intentionally avoids concurrency (goroutines) to highlight fundamental networking concepts without the complexity of concurrent programming. While this makes it slower than concurrent alternatives, it provides a clear, educational demonstration of network I/O operations.
- TCP Port Scanning: Uses
net.DialTimeoutfor reliable TCP connection testing - Flexible Input: Support for multiple IP addresses and port ranges
- Configurable Timeouts: Prevent hanging on unresponsive ports
- Progress Tracking: Real-time feedback during long scans
- Clear Output: Structured, easy-to-read results
- Input Validation: Comprehensive validation of IPs and ports
- Cross-platform: Compiles to single binary for different operating systems
- Go 1.16 or higher
# Clone or download the source code
# Navigate to the project directory
make all# Windows
GOOS=windows GOARCH=amd64 go build -o port-scanner.exe main.go
# Linux
GOOS=linux GOARCH=amd64 go build -o port-scanner-linux main.go
# macOS
GOOS=darwin GOARCH=amd64 go build -o port-scanner-macos main.go./port-scanner -ips="IP1,IP2,IP3" -ports="PORT_RANGE" -timeout=SECONDS| Flag | Description | Default | Example |
|---|---|---|---|
-ips |
Comma-separated list of IP addresses | Required | "192.168.1.1,8.8.8.8" |
-ports |
Port range or specific ports | "1-1000" |
"22-80" or "22,80,443" |
-timeout |
Connection timeout in seconds | 2 |
3 |
-help |
Show help message | - | - |
./port-scanner -ips="192.168.1.1" -ports="22,23,53,80,443,8080"./port-scanner -ips="8.8.8.8,1.1.1.1" -ports="53-80" -timeout=3./port-scanner -ips="8.8.8.8,8.8.4.4,1.1.1.1" -ports="53"./port-scanner -ips="example.com,google.com" -ports="80,443,8080,8443"Configuration:
IPs: 8.8.8.8, 1.1.1.1
Port range: 53-80
Timeout: 2s
Scan type: Sequential (no concurrency)
Starting sequential port scan...
Scanning 2 IPs, ports 53-80 (total: 56 scans)
Scanning 8.8.8.8...
[OPEN] Port 53
Completed 8.8.8.8: 1 open ports found
Scanning 1.1.1.1...
[OPEN] Port 53
Completed 1.1.1.1: 1 open ports found
=== SCAN RESULTS ===
8.8.8.8:
Open ports (1): 53
1.1.1.1:
Open ports (1): 53
=== SCAN SUMMARY ===
Total scan time: 12.34s
Total open ports found: 2
IPs scanned: 2
Ports per IP: 28
- PortScanner Struct: Main scanner configuration and methods
- ScanResult Type: Structured result representation
- Input Validation: Comprehensive validation for IPs and ports
- Error Handling: Go's explicit error handling throughout
ScanPort(): Core scanning logic usingnet.DialTimeoutScanAll(): Sequential scanning with progress trackingparsePortRange(): Flexible port range parsingvalidateIP()/validatePort(): Input validation
- Uses Go's standard
netpackage for TCP connections - Implements proper timeout handling with
net.DialTimeout - Graceful connection closure to prevent resource leaks
for _, ip := range ps.IPs {
for port := ps.MinPort; port <= ps.MaxPort; port++ {
result := ps.ScanPort(ip, port)
// Process result...
}
}This nested loop structure ensures:
- Complete scan of each IP before moving to the next
- Predictable resource usage
- Clear debugging and error tracking
- Educational clarity of the scanning process
- Speed: Sequential scanning is slower than concurrent approaches
- Resource Usage: Low memory and CPU usage
- Network Load: Controlled, non-aggressive scanning
- Reliability: Predictable behavior, easier debugging
- ~500-1000 ports per minute (depending on network latency)
- Suitable for small to medium scan ranges
- Memory usage: minimal (~1-5MB)
port-scanner/
βββ README.md
βββ go.mod
βββ go.sum (will be created automatically)
βββ main.go
βββ cmd/
β βββ port-scanner/
β βββ main.go (alternative structure)
βββ pkg/
β βββ scanner/
β βββ scanner.go
β βββ scanner_test.go
βββ internal/
β βββ validator/
β βββ validator.go
βββ examples/
β βββ basic_usage.md
βββ scripts/
β βββ build.sh
βββ .gitignore
βββ Makefile
- Comprehensive error handling
- Input validation
- Clean separation of concerns
- Descriptive variable names
- Proper documentation
This tool is intended for:
- Educational purposes: Learning network programming
- Network administration: Checking your own infrastructure
- Security auditing: Testing systems you own or have permission to test
Important: Only scan networks and systems you own or have explicit permission to test. Unauthorized port scanning may violate terms of service or local laws.
This is a learning project, but suggestions for improvements are welcome:
- Better error messages
- Additional output formats (JSON, CSV)
- Service detection capabilities
- IPv6 support
This project is provided as-is for educational purposes. Feel free to use and modify as needed.
This project demonstrates:
- Go Fundamentals: Structs, methods, error handling
- Networking: TCP connections, timeouts, address parsing
- CLI Development: Flag parsing, user input validation