1 Star 0 Fork 0

sqos / beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
net.go 1.24 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 []net.IP{}, err
}
for _, ipaddr := range ipaddrs {
if ipnet, ok := ipaddr.(*net.IPNet); ok {
localIPAddrs = append(localIPAddrs, ipnet.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(include_loopbacks bool) ([]string, error) {
var localIPAddrsStrings = []string{}
var err error
ipaddrs, err := LocalIpAddrs()
if err != nil {
return []string{}, err
}
for _, ipaddr := range ipaddrs {
if include_loopbacks || !ipaddr.IsLoopback() {
localIPAddrsStrings = append(localIPAddrsStrings, ipaddr.String())
}
}
return localIPAddrsStrings, err
}
// IsLoopback check if a particular IP notation corresponds
// to a loopback interface.
func IsLoopback(ip_str string) (bool, error) {
ip := net.ParseIP(ip_str)
if ip == nil {
return false, fmt.Errorf("Wrong IP format %s", ip_str)
}
return ip.IsLoopback(), nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v5.0.0-alpha4

搜索帮助

344bd9b3 5694891 D2dac590 5694891