-
Notifications
You must be signed in to change notification settings - Fork 0
/
extended-ping.go
142 lines (119 loc) · 2.86 KB
/
extended-ping.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"fmt"
"io"
"net"
"os"
"time"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
)
func main() {
args := os.Args
if len(args) != 2 {
fmt.Println("Usage: eping <host>")
os.Exit(1)
}
host := args[1]
ip, err := net.ResolveIPAddr("ip4", host)
if err != nil {
fmt.Println("Cannot resolve IP address to host.")
os.Exit(1)
}
var timeout time.Duration = time.Second * 3 // Seconds
common_ports_tcp := [...]string{"20", "21", "22", "23", "25", "53", "80", "110", "143", "443", "3389", "8080"}
common_ports_udp := [...]string{"53", "67", "68", "69", "123", "161", "500", "514", "1812", "1813"}
icmp_ping(ip)
for _, port := range common_ports_tcp {
tcp_ping(ip, port, timeout)
}
for _, port := range common_ports_udp {
udp_ping(ip, port, timeout)
}
}
func tcp_ping(host *net.IPAddr, port string, timeout time.Duration) {
d := net.Dialer{Timeout: timeout}
conn, err := d.Dial("tcp", fmt.Sprintf("%v:%v", host, port))
if err != nil {
fmt.Printf("TCP ping to %s:%s unsuccessful.\n", host, port)
return
}
defer conn.Close()
fmt.Printf("TCP ping to %s:%s successful.\n", host, port)
}
func udp_ping(host *net.IPAddr, port string, timeout time.Duration) {
conn, err := net.DialTimeout("udp", fmt.Sprintf("%v:%v", host, port), timeout)
if err != nil {
fmt.Printf("1 UDP ping to %s:%s unsuccessful.\n", host, port)
return
}
defer conn.Close()
message := []byte("ping")
_, err = conn.Write(message)
if err != nil {
fmt.Printf("2 UDP ping to %s:%s unsuccessful.\n", host, port)
return
}
conn.SetReadDeadline(time.Now().Add(timeout))
buffer := make([]byte, 65535)
_, err = conn.Read(buffer)
if err != nil {
if err != io.EOF {
fmt.Printf("UDP ping to %s:%s unsuccessful.\n", host, port)
return
}
}
fmt.Printf("UDP ping to %s:%s successful.\n", host, port)
}
func icmp_ping(host *net.IPAddr) {
conn, err := icmp.ListenPacket("ip4:icmp", "0.0.0.0")
if err != nil {
fmt.Println("Error2: ", err)
return
}
defer conn.Close()
msg := icmp.Message{
Type: ipv4.ICMPTypeEcho,
Code: 0,
Body: &icmp.Echo{
ID: os.Getpid() & 0xffff,
Seq: 1,
Data: []byte(""),
},
}
msg_bytes, err := msg.Marshal(nil)
if err != nil {
fmt.Println(err)
return
}
if _, err := conn.WriteTo(msg_bytes, host); err != nil {
fmt.Println(err)
panic(err)
}
err = conn.SetReadDeadline(time.Now().Add(time.Second * 1))
if err != nil {
fmt.Println(err)
return
}
reply := make([]byte, 644)
n, _, err := conn.ReadFrom(reply)
if err != nil {
fmt.Println(err)
return
}
parsed_reply, err := icmp.ParseMessage(1, reply[:n])
if err != nil {
fmt.Println(err)
return
}
switch parsed_reply.Code {
case 0:
fmt.Printf("ICMP ping to %s successful.\n", host)
case 3:
fmt.Printf("Host %s is unreachable\n", host)
case 11:
fmt.Printf("Host %s is slow\n", host)
default:
fmt.Printf("Host %s is unreachable\n", host)
}
}