Skip to content

Instantly share code, notes, and snippets.

@timsonner
Created June 3, 2023 00:24
Show Gist options
  • Select an option

  • Save timsonner/b260b47579fb13445068f9dbca1cbc80 to your computer and use it in GitHub Desktop.

Select an option

Save timsonner/b260b47579fb13445068f9dbca1cbc80 to your computer and use it in GitHub Desktop.
GoLang. Scans a range of IPs for web servers running on ports 80, 8080, and 443.
package main
import (
"fmt"
"net"
"sync"
)
func main() {
var wg sync.WaitGroup
// Define the range of IP addresses to scan
startIP := net.ParseIP("192.168.1.1")
endIP := net.ParseIP("192.168.1.254")
// Convert IP addresses to integers for iteration
start := ipToInt(startIP)
end := ipToInt(endIP)
for ip := start; ip <= end; ip++ {
wg.Add(1)
go func(ip uint32) {
defer wg.Done()
address := intToIP(ip)
scanPorts(address, []int{80, 8080, 443})
}(ip)
}
wg.Wait()
}
func scanPorts(address string, ports []int) {
for _, port := range ports {
target := fmt.Sprintf("%s:%d", address, port)
conn, err := net.Dial("tcp", target)
if err == nil {
defer conn.Close()
fmt.Printf("Webserver found at %s:%d\n", address, port)
}
}
}
func ipToInt(ip net.IP) uint32 {
ip = ip.To4()
return (uint32(ip[0]) << 24) | (uint32(ip[1]) << 16) | (uint32(ip[2]) << 8) | uint32(ip[3])
}
func intToIP(ip uint32) string {
return net.IPv4(byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip)).String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment