/
goserver.go
68 lines (57 loc) · 1.29 KB
/
goserver.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
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
* File Name : goserver.go
* Purpose :
* Creation Date : 07-24-2014
* Last Modified : Tue 29 Jul 2014 12:19:38 AM UTC
* Created By : Kiyor
_._._._._._._._._._._._._._._._._._._._._.*/
package goserver
import (
"encoding/json"
"errors"
"github.com/kiyor/subnettool"
"io/ioutil"
"net"
// "os"
"strings"
)
func (this *IpInfo) getIP() {
ips, err := net.InterfaceAddrs()
if err != nil {
panic(err)
}
this.Ip = Ip{}
for _, v := range ips {
ipstr := strings.Split(v.String(), "/")[0]
ip := net.ParseIP(ipstr)
if ip.To4() != nil {
token := subnettool.ParseIPInt(ip.To4())
if token[0] == 127 {
continue
} else if token[0] == 10 || token[0] == 192 || token[0] == 176 {
this.Ip.PrivIps = append(this.Ip.PrivIps, ip)
} else {
this.Ip.PubIps = append(this.Ip.PubIps, ip)
}
}
}
}
func GetHostname() (string, error) {
b, err := ioutil.ReadFile("/etc/sysconfig/network")
if err != nil {
return "", err
}
lines := strings.Split(string(b), "\n")
for _, line := range lines {
if strings.Contains(line, "HOSTNAME") {
return strings.Split(line, "=")[1], nil
}
}
return "", errors.New("not found")
}
func (this *IpInfo) Info() string {
// this.getHostname()
this.getIP()
j, _ := json.Marshal(this)
return string(j)
}