Created
December 5, 2019 13:07
-
-
Save podanypepa/415689ad0646b76b30490411b07bb45d to your computer and use it in GitHub Desktop.
This file contains 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" | |
"os" | |
"strconv" | |
"time" | |
) | |
func IsOpened(host string, port int) bool { | |
timeout := 5 * time.Second | |
target := fmt.Sprintf("%s:%d", host, port) | |
conn, err := net.DialTimeout("tcp", target, timeout) | |
if err != nil { | |
return false | |
} | |
if conn != nil { | |
conn.Close() | |
return true | |
} | |
return false | |
} | |
func main() { | |
if len(os.Args) < 3 { | |
fmt.Printf("use: %s HOST PORT\n", os.Args[0]) | |
os.Exit(0) | |
} | |
host := os.Args[1] | |
port, err := strconv.Atoi(os.Args[2]) | |
if err != nil { | |
fmt.Println("port must be number") | |
os.Exit(1) | |
} | |
fmt.Println(IsOpened(host, port)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment