mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 18:22:27 +08:00
feat: add func GetIps and GetMacAddrs
This commit is contained in:
@@ -47,6 +47,46 @@ func GetPublicIpInfo() (*PublicIpInfo, error) {
|
|||||||
return &ip, nil
|
return &ip, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetIps return all ipv4 of system
|
||||||
|
func GetIps() []string {
|
||||||
|
var ips []string
|
||||||
|
|
||||||
|
addrs, err := net.InterfaceAddrs()
|
||||||
|
if err != nil {
|
||||||
|
return ips
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, addr := range addrs {
|
||||||
|
ipNet, isValid := addr.(*net.IPNet)
|
||||||
|
if isValid && !ipNet.IP.IsLoopback() {
|
||||||
|
if ipNet.IP.To4() != nil {
|
||||||
|
ips = append(ips, ipNet.IP.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ips
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMacAddrs get mac address
|
||||||
|
func GetMacAddrs() []string {
|
||||||
|
var macAddrs []string
|
||||||
|
|
||||||
|
nets, err := net.Interfaces()
|
||||||
|
if err != nil {
|
||||||
|
return macAddrs
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, net := range nets {
|
||||||
|
macAddr := net.HardwareAddr.String()
|
||||||
|
if len(macAddr) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
macAddrs = append(macAddrs, macAddr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return macAddrs
|
||||||
|
}
|
||||||
|
|
||||||
// PublicIpInfo public ip info: country, region, isp, city, lat, lon, ip
|
// PublicIpInfo public ip info: country, region, isp, city, lat, lon, ip
|
||||||
type PublicIpInfo struct {
|
type PublicIpInfo struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
|
|||||||
@@ -42,3 +42,13 @@ func TestIsPublicIP(t *testing.T) {
|
|||||||
assert.Equal(expected[i], actual)
|
assert.Equal(expected[i], actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetIps(t *testing.T) {
|
||||||
|
ips := GetIps()
|
||||||
|
t.Log(ips)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMacAddrs(t *testing.T) {
|
||||||
|
macAddrs := GetMacAddrs()
|
||||||
|
t.Log(macAddrs)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user