2 回答
TA贡献1860条经验 获得超8个赞
要检查端口,您可以检查连接是否成功。例如:
func raw_connect(host string, ports []string) {
for _, port := range ports {
timeout := time.Second
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), timeout)
if err != nil {
fmt.Println("Connecting error:", err)
}
if conn != nil {
defer conn.Close()
fmt.Println("Opened", net.JoinHostPort(host, port))
}
}
}
TA贡献1828条经验 获得超6个赞
检查多个端口示例
func tcpGather(ip string, ports []string) map[string]string {
// check emqx 1883, 8083 port
results := make(map[string]string)
for _, port := range ports {
address := net.JoinHostPort(ip, port)
// 3 second timeout
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
if err != nil {
results[port] = "failed"
// todo log handler
} else {
if conn != nil {
results[port] = "success"
_ = conn.Close()
} else {
results[port] = "failed"
}
}
}
return results
}
- 2 回答
- 0 关注
- 198 浏览
添加回答
举报