本文整理汇总了Golang中net.Interfaces函数的典型用法代码示例。如果您正苦于以下问题:Golang Interfaces函数的具体用法?Golang Interfaces怎么用?Golang Interfaces使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Interfaces函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getInt
func (i *Intface) getInt() error {
if i.Interface != "" && i.iface == nil {
ints, err := net.Interfaces()
if err != nil {
return e.New(err)
}
for _, in := range ints {
if in.Name == i.Interface {
i.iface = &in
return nil
}
}
return e.New("none interface with this name")
} else if i.Interface == "" && i.iface == nil {
ints, err := net.Interfaces()
if err != nil {
return e.New(err)
}
var intName string
for _, in := range ints {
if in.Flags&net.FlagMulticast == net.FlagMulticast || in.Flags&net.FlagBroadcast == net.FlagBroadcast || in.Flags&net.FlagBroadcast == net.FlagBroadcast {
_, intName = getInterface(in)
if intName == "" {
continue
}
i.Interface = intName
i.iface = &in
break
}
}
}
return nil
}
示例2: TestHelper
func TestHelper(t *testing.T) {
g := Goblin(t)
g.Describe(`PrivateInterface`, func() {
g.Describe(`with a valid interface`, func() {
g.It(`returns the correct interface`, func() {
ifaces, _ := net.Interfaces()
addrs, _ := ifaces[0].Addrs()
localAddr := addrs[0]
ip := localAddr.String()
switch v := localAddr.(type) {
case *net.IPAddr:
ip = v.IP.String()
case *net.IPNet:
ip = v.IP.String()
}
ifaceName, err := PrivateInterface(ifaces, ip)
g.Assert(ifaceName).Equal(ifaces[0].Name)
g.Assert(err).Equal(nil)
})
})
g.It(`with an invalid IP`, func() {
ifaces, _ := net.Interfaces()
ifaceName, err := PrivateInterface(ifaces, `somethingBad`)
g.Assert(ifaceName).Equal(``)
g.Assert(err).Equal(errors.New(`local interface could not be found`))
})
})
g.Describe(`LocalAddress`, func() {
g.Describe(`with a private interface`, func() {
g.Describe(`with an ipv4 address`, func() {
g.It(`returns the ip address`, func() {
data := decodeMetadata(`{"interfaces": {"private": [{"ipv4": {"ip_address": "privateIP"}}]}}`)
addr, _ := LocalAddress(data)
g.Assert(addr).Equal(`privateIP`)
})
})
g.Describe(`without an ipv4 address`, func() {
g.It(`returns an error`, func() {
data := decodeMetadata(`{"interfaces": {"public": [{"ipv4": {"ip_address": "publicIP"}}]}}`)
_, err := LocalAddress(data)
g.Assert(err).Equal(errors.New(`no private interfaces`))
})
})
})
g.Describe(`without a private interface`, func() {
g.It(`returns an error`, func() {
data := &metadata.Metadata{}
_, err := LocalAddress(data)
g.Assert(err).Equal(errors.New(`no private interfaces`))
})
})
})
}
示例3: main
func main() {
flag.Parse()
interfaces, err := net.Interfaces()
if err == nil {
for _, intf := range interfaces {
addrs, err := intf.Addrs()
if err != nil {
continue
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
if len(*uri) > 0 {
httpHealthCheck(ipnet.IP.String())
} else {
portHealthCheck(ipnet.IP.String())
}
}
}
}
}
}
fmt.Println("healthcheck failed")
os.Exit(1)
}
示例4: New
// New TUN device
func New() (Tun, error) {
var tun Tun
var name string
ifaces, err := net.Interfaces()
if err != nil {
return tun, nil
}
for i := 0; ; i += 1 {
name = "tun" + strconv.Itoa(i)
iface_exist := false
for _, iface := range ifaces {
if strings.Contains(iface.Name, name) {
iface_exist = true
}
}
if !iface_exist {
break
}
}
log.WithField("name", name).Info("Allocating TUN interface")
switch runtime.GOOS {
case "darwin":
tun = &UTun{}
case "linux":
tun = &LinuxTun{}
default:
return nil, fmt.Errorf("Tun not supported in %v", runtime.GOOS)
}
err = tun.Create(name)
return tun, err
}
示例5: init
// init — we just want to make sure that the default values are good values.
// As the zero values don't make sense, this make the zero values acceptable values.
// If a network interface to use is not provided, we use the first device that is
// ip and able to Broadcast. If not devices are found we return an error of NoInterfaces.
func (m *Mxio) init() error {
if m == nil {
return errors.New("Mxio can not be nil!")
}
if m.DeviceType == DeviceType(0) {
m.DeviceType = ALL_DEVICES
}
if m.IF == nil {
interfaces, err := net.Interfaces()
if len(interfaces) == 0 {
return NoInterfaces
}
if err != nil {
return err
}
for _, ifc := range interfaces {
if (ifc.Flags & (net.FlagUp | net.FlagBroadcast)) != 0 {
m.IF = &ifc
break
}
}
if m.IF == nil {
return NoInterfaces
}
}
if m.Timeout == 0 {
// we want to default the timeout to 5 seconds
m.Timeout = 5 * time.Second
}
if m.Retry == 0 {
// we want the default the retry to 3
m.Retry = 3
}
return nil
}
示例6: uploadUsage
func (m *Manager) uploadUsage() {
id := "anon"
ifaces, err := net.Interfaces()
if err == nil {
for _, iface := range ifaces {
if iface.Name != "lo" {
hw := iface.HardwareAddr.String()
id = strings.Replace(hw, ":", "", -1)
break
}
}
}
info := m.clusterManager.ClusterInfo()
usage := &shipyard.Usage{
ID: id,
Version: m.version,
NumOfEngines: info.EngineCount,
NumOfImages: info.ImageCount,
NumOfContainers: info.ContainerCount,
TotalCpus: info.Cpus,
TotalMemory: info.Memory,
}
b, err := json.Marshal(usage)
if err != nil {
logger.Warnf("error serializing usage info: %s", err)
}
buf := bytes.NewBuffer(b)
if _, err := http.Post(fmt.Sprintf("%s/update", trackerHost), "application/json", buf); err != nil {
logger.Warnf("error sending usage info: %s", err)
}
}
示例7: getIP
func getIP() string {
ifaces, err := net.Interfaces()
if err != nil {
logger.WithField("_block", "getIP").Error(err)
return "127.0.0.1"
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
logger.WithField("_block", "getIP").Error(err)
return "127.0.0.1"
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPAddr:
ip = v.IP
case *net.IPNet:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String()
}
}
return "127.0.0.1"
}
示例8: mynames
/*
* Generate a list of names for which the certificate will be valid.
* This will include the hostname and ip address
*/
func mynames() ([]string, error) {
h, err := os.Hostname()
if err != nil {
return nil, err
}
ret := []string{h}
ifs, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, iface := range ifs {
if IsLoopback(&iface) {
continue
}
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
ret = append(ret, addr.String())
}
}
return ret, nil
}
示例9: GetIPs
func GetIPs(ifaceWanted string, familyWanted int) ([]string, error) {
ips := make([]string, 0)
ifaces, err := net.Interfaces()
if err != nil {
return ips, err
}
for _, iface := range ifaces {
if iface.Name != ifaceWanted {
continue
}
addrs, _ := iface.Addrs()
for _, addr := range addrs {
addrString := addr.String()
ip, _, err := net.ParseCIDR(addrString)
if err != nil {
return ips, err
}
if strings.Contains(addrString, ".") && familyWanted == netlink.FAMILY_V4 ||
strings.Contains(addrString, ":") && familyWanted == netlink.FAMILY_V6 {
ips = append(ips, ip.String())
}
}
}
return ips, err
}
示例10: ResolveIP
func (resolver *addressResolver) ResolveIP(host string) (net.IP, error) {
if host == "localhost" || host == "127.0.0.1" {
if resolver.local != nil {
return resolver.local, nil
}
if !resolver.checked {
resolver.checked = true
devices, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, dev := range devices {
if (dev.Flags&net.FlagUp != 0) && (dev.Flags&net.FlagLoopback == 0) {
addrs, err := dev.Addrs()
if err != nil {
continue
}
for i := range addrs {
if ip, ok := addrs[i].(*net.IPNet); ok {
log.Printf("Using %v for %s", ip, host)
resolver.local = ip.IP
return resolver.local, nil
}
}
}
}
}
}
addr, err := net.ResolveIPAddr("ip", host)
if err != nil {
return nil, err
}
return addr.IP, nil
}
示例11: chooseInterface
func chooseInterface() string {
interfaces, err := net.Interfaces()
if err != nil {
log.Fatalf("net.Interfaces: %s", err)
}
for _, iface := range interfaces {
// Skip loopback
if iface.Name == "lo" {
continue
}
addrs, err := iface.Addrs()
// Skip if error getting addresses
if err != nil {
log.Println("Error get addresses for interfaces %s. %s", iface.Name, err)
continue
}
if len(addrs) > 0 {
// This one will do
return iface.Name
}
}
return ""
}
示例12: Get
func (s *systemInterfaceAddrs) Get() ([]InterfaceAddress, error) {
ifaces, err := net.Interfaces()
if err != nil {
return []InterfaceAddress{}, bosherr.WrapError(err, "Getting network interfaces")
}
interfaceAddrs := []InterfaceAddress{}
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
return []InterfaceAddress{}, bosherr.WrapErrorf(err, "Getting addresses of interface '%s'", iface.Name)
}
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if err != nil {
return []InterfaceAddress{}, bosherr.WrapErrorf(err, "Parsing addresses of interface '%s'", iface.Name)
}
if ipv4 := ip.To4(); ipv4 != nil {
interfaceAddrs = append(interfaceAddrs, NewSimpleInterfaceAddress(iface.Name, ipv4.String()))
}
}
}
return interfaceAddrs, nil
}
示例13: startLocalIPv6Multicasts
func (d *Discoverer) startLocalIPv6Multicasts(localMCAddr string) {
intfs, err := net.Interfaces()
if err != nil {
if debug {
l.Debugln("discover: interfaces:", err)
}
l.Infoln("Local discovery over IPv6 unavailable")
return
}
v6Intfs := 0
for _, intf := range intfs {
// Interface flags seem to always be 0 on Windows
if runtime.GOOS != "windows" && (intf.Flags&net.FlagUp == 0 || intf.Flags&net.FlagMulticast == 0) {
continue
}
mb, err := beacon.NewMulticast(localMCAddr, intf.Name)
if err != nil {
if debug {
l.Debugln("discover: Start local v6:", err)
}
continue
}
d.beacons = append(d.beacons, mb)
go d.recvAnnouncements(mb)
v6Intfs++
}
if v6Intfs == 0 {
l.Infoln("Local discovery over IPv6 unavailable")
}
}
示例14: GetLocalAddrList
// GetLocalAddrList returns a list of local IP addresses
func GetLocalAddrList() ([]string, error) {
var addrList []string
// get the link list
intfList, err := net.Interfaces()
if err != nil {
return addrList, err
}
log.Debugf("Got address list(%d): %+v", len(intfList), intfList)
// Loop thru each interface and add its ip addr to list
for _, intf := range intfList {
if strings.HasPrefix(intf.Name, "docker") || strings.HasPrefix(intf.Name, "veth") ||
strings.HasPrefix(intf.Name, "vport") || strings.HasPrefix(intf.Name, "lo") {
continue
}
addrs, err := intf.Addrs()
if err != nil {
return addrList, err
}
for _, addr := range addrs {
addrList = append(addrList, addr.String())
}
}
return addrList, err
}
示例15: GetNonLoIfaceWithAddrs
func GetNonLoIfaceWithAddrs(ipFamily int) (iface net.Interface, addrs []string, err error) {
ifaces, err := net.Interfaces()
if err != nil {
return iface, nil, err
}
for _, i := range ifaces {
if i.Flags&net.FlagLoopback == 0 {
addrs, err = GetIPs(i.Name, ipFamily)
if err != nil {
return iface, addrs, fmt.Errorf("Cannot get IP address for interface %v: %v", i.Name, err)
}
if len(addrs) == 0 {
continue
}
iface = i
ifaceNameLower := strings.ToLower(i.Name)
// Don't use rkt's interfaces
if strings.Contains(ifaceNameLower, "cni") ||
strings.Contains(ifaceNameLower, "veth") {
continue
}
break
}
}
return iface, addrs, err
}