Skip to content

Instantly share code, notes, and snippets.

@picatz
Created May 12, 2018 04:21
Show Gist options
  • Save picatz/fbc7eb3b0fbff709ab8a514135ae93db to your computer and use it in GitHub Desktop.
Save picatz/fbc7eb3b0fbff709ab8a514135ae93db to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net"
"time"
)
type TargetHost struct {
ip string
}
func checkPortOpen(ip string, port int, timeout time.Duration) bool {
conn, _ := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), timeout)
if conn == nil {
return false
}
conn.Close()
return true
}
func (t *TargetHost) FindOpenPorts(start, stop int) (ports []int) {
seconds := 1
timeout := (time.Duration(seconds) * time.Second)
for port := start; port <= stop; port++ {
if checkPortOpen(t.ip, port, timeout) {
ports = append(ports, port)
}
}
return
}
func main() {
t := TargetHost{"127.0.0.1"}
p := t.FindOpenPorts(1, 1024)
fmt.Println(p)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment