Skip to content

Instantly share code, notes, and snippets.

@xandersavvy
Created February 11, 2025 16:49
Show Gist options
  • Save xandersavvy/ab420b11b153844aa3613def8d239b51 to your computer and use it in GitHub Desktop.
Save xandersavvy/ab420b11b153844aa3613def8d239b51 to your computer and use it in GitHub Desktop.
Scan ports of an IP using Golang
package main
import (
"fmt"
"net"
"sync"
"time"
)
func scanPort(host string, port int, wg *sync.WaitGroup) {
defer wg.Done()
address := fmt.Sprintf("%s:%d", host, port)
conn, err := net.DialTimeout("tcp", address, 1*time.Second)
if err == nil {
fmt.Printf("[+] Port %d is open\n", port)
conn.Close()
}
}
func main() {
var host string
var startPort, endPort int
fmt.Print("Enter target host (IP or domain): ")
fmt.Scanln(&host)
fmt.Print("Enter start port: ")
fmt.Scanln(&startPort)
fmt.Print("Enter end port: ")
fmt.Scanln(&endPort)
var wg sync.WaitGroup
for port := startPort; port <= endPort; port++ {
wg.Add(1)
go scanPort(host, port, &wg)
}
wg.Wait()
}
// Things to learn not from GPT 😂
/****
*printf just prints but Sprintf print and also return the formatted string which can be stored later
*Waitgroup to add go routine so main function does not exit unless every go routine is resolved
*net.DialTImeout to establish connection
******/
/***
*Hey if you are here and have some development project please feel free to contact me on [email protected]
****/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment