-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (52 loc) · 1.21 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
)
func main() {
osType := runtime.GOOS
var cmd *exec.Cmd
flag.Parse()
args := flag.Args()
if len(args) == 0 {
fmt.Println("Error: No Address provided!")
os.Exit(1)
}
target := string(args[0])
target = strings.Replace(target, "https://", "", -1)
target = strings.Replace(target, "http://", "", -1)
target = replaceSpecialCharacter(target)
switch osType {
case "windows":
cmd = exec.Command("ping", "-n", "4", target)
case "linux", "darwin":
cmd = exec.Command("ping", "-c", "4", target)
default:
fmt.Println("Unsupported operating system.")
return
}
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Error executing command:", err)
return
}
fmt.Println(string(output))
}
// 函数定义确定了返回值也会执行到return再返回最后结果
func replaceSpecialCharacter(Address string) (target string) {
target = Address // 默认返回值
ColonIndex := strings.Index(Address, ":")
HyphenIndex := strings.Index(Address, "/")
// 首先处理: 因为端口号在路径之前
if ColonIndex != -1 {
return Address[:ColonIndex]
}
if HyphenIndex != -1 {
return Address[:HyphenIndex]
}
return
}