1 Star 0 Fork 0

sqos / beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
net.go 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
package common
import (
"fmt"
"net"
)
// LocalIPAddrs finds the IP addresses of the hosts on which
// the shipper currently runs on.
func LocalIPAddrs() ([]net.IP, error) {
var localIPAddrs []net.IP
ipaddrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
for _, addr := range ipaddrs {
var ip net.IP
ok := true
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
default:
ok = false
}
if !ok {
continue
}
localIPAddrs = append(localIPAddrs, ip)
}
return localIPAddrs, nil
}
// LocalIPAddrsAsStrings finds the IP addresses of the hosts on which
// the shipper currently runs on and returns them as an array of
// strings.
func LocalIPAddrsAsStrings(includeLoopbacks bool) ([]string, error) {
var localIPAddrsStrings = []string{}
var err error
ipaddrs, err := LocalIPAddrs()
if err != nil {
return []string{}, err
}
for _, ipaddr := range ipaddrs {
if includeLoopbacks || !ipaddr.IsLoopback() {
localIPAddrsStrings = append(localIPAddrsStrings, ipaddr.String())
}
}
return localIPAddrsStrings, err
}
// IsLoopback check if a particular IP notation corresponds
// to a loopback interface.
func IsLoopback(ipStr string) (bool, error) {
ip := net.ParseIP(ipStr)
if ip == nil {
return false, fmt.Errorf("Wrong IP format %s", ipStr)
}
return ip.IsLoopback(), nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v5.3.0

搜索帮助

344bd9b3 5694891 D2dac590 5694891