Created
May 12, 2018 04:21
-
-
Save picatz/fbc7eb3b0fbff709ab8a514135ae93db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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