猿问

在golang中将网络掩码数转换为32位

我正在尝试通过以下方式获取 IP 地址和子网掩码


ifaces, err := net.Interfaces()

for _, iface := range ifaces{

        localip, _ = iface.Addrs()

}

但是,我期待将子网作为类似255.255.255.0而不是/24. 我怎样才能得到那个?如果没有模块可以做到这一点,那么作为程序来做到这一点的逻辑是什么?


白板的微信
浏览 358回答 3
3回答

不负相思意

func main() {    ifaces, err := net.Interfaces()    if err != nil {        panic(err)    }    for _, iface := range ifaces {        addrs, _ := iface.Addrs()        for _, addr := range addrs {            cidr := addr.String()            mask, err := mask(cidr)            if err != nil {                fmt.Println("extracting mask failed:", err)            }            i, err := mtoi(mask)            if err != nil {                fmt.Println("creating uint16 from mask failed:", err)            }            fmt.Printf("CIDR: %s\tMask: %d\n", cidr, i)        }    }}// Extracts IP mask from CIDR address.func mask(cidr string) (net.IPMask, error) {    _, ip, err := net.ParseCIDR(cidr)    return ip.Mask, err}// Converts IP mask to 16 bit unsigned integer.func mtoi(mask net.IPMask) (uint16, error) {    var i uint16    buf := bytes.NewReader(mask)    err := binary.Read(buf, binary.BigEndian, &i)    return i, err}

慕容森

借用一些 stackoverflow 帖子本身,使用逻辑运算符。mask = (0xFFFFFFFF << (32 - 24)) & 0xFFFFFFFF; //24 is the netmaskvar dmask uint64dmask = 32localmask := make([]uint64, 0, 4)for i := 1; i <= 4; i++{&nbsp; &nbsp; tmp := mask >> (dmask - 8) & 0xFF&nbsp; &nbsp; localmask = append(localmask, tmp)&nbsp; &nbsp; dmask -= 8}fmt.Println(localmask)

万千封印

您可以实现如下功能:func parseMask(ipaddr string) (mask string, err error) {&nbsp; &nbsp; removeExtra := regexp.MustCompile("^(.*[\\/])")&nbsp; &nbsp; asd := ipaddr[len(ipaddr)-3:]&nbsp; &nbsp; findSubnet := removeExtra.ReplaceAll([]byte(asd), []byte(""))&nbsp; &nbsp; subnet, err := strconv.ParseInt(string(findSubnet), 10, 64)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return "", errors.New("Parse Mask: Error parsing mask")&nbsp; &nbsp; }&nbsp; &nbsp; var buff bytes.Buffer&nbsp; &nbsp; for i := 0; i < int(subnet); i++ {&nbsp; &nbsp; &nbsp; &nbsp; buff.WriteString("1")&nbsp; &nbsp; }&nbsp; &nbsp; for i := subnet; i < 32; i++ {&nbsp; &nbsp; &nbsp; &nbsp; buff.WriteString("0")&nbsp; &nbsp; }&nbsp; &nbsp; masker := buff.String()&nbsp; &nbsp; a, _ := strconv.ParseUint(masker[:8], 2, 64)&nbsp; &nbsp; b, _ := strconv.ParseUint(masker[8:16], 2, 64)&nbsp; &nbsp; c, _ := strconv.ParseUint(masker[16:24], 2, 64)&nbsp; &nbsp; d, _ := strconv.ParseUint(masker[24:32], 2, 64)&nbsp; &nbsp; resultMask := fmt.Sprintf("%v.%v.%v.%v", a, b, c, d)&nbsp; &nbsp; return resultMask, nil}然后调用它:func main() {&nbsp; &nbsp; ifaces, _ := net.Interfaces()&nbsp; &nbsp; for _, iface := range ifaces {&nbsp; &nbsp; &nbsp; &nbsp; localip, _ := iface.Addrs()&nbsp; &nbsp; &nbsp; &nbsp; for _, ip := range localip {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; done, _ := parseMask(ip.String())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println(done) // 255.255.255.0&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Go
我要回答