zoukankan      html  css  js  c++  java
  • golang ---网卡信息

    package main
    
    import (
    	"fmt"
    	"log"
    	"net"
    	"strings"
    )
    
    type Network struct {
    	Name       string
    	IP         string
    	MACAddress string
    }
    
    type intfInfo struct {
    	Name       string
    	MacAddress string
    	Ipv4       []string
    }
    
    func getNetworkInfo() error {
    	intf, err := net.Interfaces()
    	if err != nil {
    		log.Fatal("get network info failed: %v", err)
    		return err
    	}
    	var is = make([]intfInfo, len(intf))
    	for i, v := range intf {
    		ips, err := v.Addrs()
    		if err != nil {
    			log.Fatal("get network addr failed: %v", err)
    			return err
    		}
    		//此处过滤loopback(本地回环)和isatap(isatap隧道)
    		if !strings.Contains(v.Name, "Loopback") && !strings.Contains(v.Name, "isatap") {
    			var network Network
    			is[i].Name = v.Name
    			is[i].MacAddress = v.HardwareAddr.String()
    			for _, ip := range ips {
    				if strings.Contains(ip.String(), ".") {
    					is[i].Ipv4 = append(is[i].Ipv4, ip.String())
    				}
    			}
    			network.Name = is[i].Name
    			network.MACAddress = is[i].MacAddress
    			if len(is[i].Ipv4) > 0 {
    				network.IP = is[i].Ipv4[0]
    			}
    
    			fmt.Printf("network:=", network)
    		}
    
    	}
    
    	return nil
    }
    
    func main() {
    	getNetworkInfo()
    }
    

      

  • 相关阅读:
    关于递归的理解
    every few days
    相见恨晚
    VC中自定义消息处理函数的步骤
    wparam和lparam的区别
    VC中新建的类不见了的解决方法
    接连遇到大牛
    老总NB的 romman.sys
    [恢]hdu 1159
    [恢]hdu 1996
  • 原文地址:https://www.cnblogs.com/saryli/p/11113921.html
Copyright © 2011-2022 走看看