forked from hootrhino/rulex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_hostname_test.go
52 lines (48 loc) · 1008 Bytes
/
get_hostname_test.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
package test
import (
"fmt"
"net"
"testing"
)
func Test_get_hostname(t *testing.T) {
//ss, err := GetLocalIP()
// if err != nil {
// t.Error(err)
// }
//t.Log(ss)
localAddresses()
}
func localAddresses() {
ifaces, err := net.Interfaces()
if err != nil {
fmt.Print(fmt.Errorf("localAddresses: %+v\n", err.Error()))
return
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
fmt.Print(fmt.Errorf("localAddresses: %+v\n", err.Error()))
continue
}
for _, a := range addrs {
fmt.Println(a.String())
}
}
}
// GetLocalIP returns the non loopback local IP of the host
func GetLocalIP() ([]string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return []string{}, err
}
ss := make([]string, len(addrs)-1)
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
// ip+net 172.30.128.1/20 172.30.128.1
ss = append(ss, ipnet.IP.String())
}
}
}
return ss, nil
}