-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys.go
135 lines (120 loc) · 2.58 KB
/
sys.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
package sys
import (
"bytes"
"errors"
"io"
"net"
"os"
"os/exec"
"strings"
)
// ExecCommand 执行系统命令
func ExecCommand(name string, arg ...string) string {
cmd := exec.Command(name, arg...)
var buf bytes.Buffer
cmd.Stdout = &buf
_ = cmd.Run()
return buf.String()
}
// ExecCommandGrep 执行系统pipe命令
// ps := exec.Command("ps", "-ef")
// grep := exec.Command("grep", "-i", "php-fpm")
func ExecCommandGrep(command *exec.Cmd, grep *exec.Cmd) string {
r, w := io.Pipe() // 创建一个管道
defer r.Close()
defer w.Close()
command.Stdout = w // command向管道的一端写
grep.Stdin = r // grep从管道的一端读
var buffer bytes.Buffer
grep.Stdout = &buffer // grep的输出为buffer
_ = command.Start()
_ = grep.Start()
command.Wait()
w.Close()
grep.Wait()
return buffer.String()
}
// ExternalIP 获得请求IP
func ExternalIP() (string, error) {
iFaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iFace := range iFaces {
if iFace.Flags&net.FlagUp == 0 {
continue // interface down
}
if iFace.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iFace.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
}
return "", errors.New("are you connected to the network?")
}
// LocalIP 获得本机IP
func LocalIP() (string, error) {
addr, err := net.ResolveUDPAddr("udp", "1.2.3.4:1")
if err != nil {
return "", err
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
return "", err
}
defer conn.Close()
host, _, err := net.SplitHostPort(conn.LocalAddr().String())
if err != nil {
return "", err
}
return host, nil
}
// HostName 获得本机名
func HostName() (string, error) {
hostNamePrefix := ""
host, err := os.Hostname()
if err != nil {
return "", err
}
if err == nil {
parts := strings.SplitN(host, ".", 2)
if len(parts) > 0 {
hostNamePrefix = parts[0]
}
}
return hostNamePrefix, nil
}
// GetInternalIP 获得请求IP
func GetInternalIP() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}
return "", errors.New("cannot get internal ip")
}