Golang编写判断IP是否能够Ping通

人狠话不多,请看代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main

import (
"time"
"net"
"fmt"
)

func isping(ip string) (bool) {
recvBuf1 := make([]byte, 2048)
payload:=[]byte{0x08,0x00,0x4d,0x4b,0x00,0x01,0x00,0x10,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69}
Time, _ := time.ParseDuration("3s")
conn, err := net.DialTimeout("ip4:icmp", ip,Time)
if err !=nil {
fmt.Println("bibi")
return false
}
_,err=conn.Write(payload)
if err !=nil {
return false
}
conn.SetReadDeadline(time.Now().Add(time.Second * 2))
num, err := conn.Read(recvBuf1[0:])
if err !=nil {
//check 80 3389 443 22 port
Timetcp, _ := time.ParseDuration("1s")
conn1, err := net.DialTimeout("tcp", ip+":80",Timetcp)
if err == nil {
defer conn1.Close()
return true
}

conn2, err := net.DialTimeout("tcp", ip+":443",Timetcp)
if err == nil {
defer conn2.Close()
return true
}

conn3, err := net.DialTimeout("tcp", ip+":3389",Timetcp)
if err == nil {
defer conn3.Close()
return true
}

conn4, err := net.DialTimeout("tcp", ip+":22",Timetcp)
if err == nil {
defer conn4.Close()
return true
}

return false
}
conn.SetReadDeadline(time.Time{})
if string(recvBuf1[0:num]) !="" {
return true
}
return false

}

func main() {
ip := "172.8.47.213"
fmt.Println(isping(ip))
}