为了账号安全,请及时绑定邮箱和手机立即绑定

IP 地址从 maxAddress 开始

IP 地址从 maxAddress 开始

Go
FFIVE 2023-05-15 14:28:59
我需要在给定 CIDR 和一些缓存地址的情况下开始生成 IP 地址。我在这里有一些代码,我正在做字节。与存储的地址进行比较,只选择那些更大的。https://play.golang.org/p/yT_Mj4fR_jK这里出了什么问题?基本上我需要“62.76.47.12/28”中“62.76.47.9”的所有地址。在给定的 CIDR 范围内生成 IP 是众所周知的。谢谢。
查看完整描述

3 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

此示例将打印从第一个地址 62.76.47.12/28 到 62.76.47.9 的地址。

游乐场: https: //play.golang.org/p/MUtbiKaQ_3-

package main


import (

    "fmt"

    "net"

)


func main() {

    cidr := "62.76.47.12/28"

    _, ipnet, _ := net.ParseCIDR(cidr)

    ipFirst := ipnet.IP

    ipFirstValue := toHost(ipFirst)


    ipLast := net.ParseIP("62.76.47.9")

    ipLastValue := toHost(ipLast)


    fmt.Println("cidr:  ", cidr)

    fmt.Println("first: ", ipFirst)

    fmt.Println("last:  ", ipLast)


    if ipLastValue < ipFirstValue {

        fmt.Println("ugh")

        return

    }


    for i := ipFirstValue; i < ipLastValue; i++ {

        addr := toIP(i)

        fmt.Println(addr)

    }

}


func toHost(ip net.IP) uint32 {

    i := ip.To4()

    return uint32(i[0])<<24 + uint32(i[1])<<16 + uint32(i[2])<<8 + uint32(i[3])

}


func toIP(v uint32) net.IP {

    v3 := byte(v & 0xFF)

    v2 := byte((v >> 8) & 0xFF)

    v1 := byte((v >> 16) & 0xFF)

    v0 := byte((v >> 24) & 0xFF)

    return net.IPv4(v0, v1, v2, v3)

}


查看完整回答
反对 回复 2023-05-15
?
富国沪深

TA贡献1790条经验 获得超9个赞

如果你打印ìpMax,你会看到它的底层表示使用了 16 个字节。(另请参阅文档

fmt.Printf("'%#v'\n",ipMax)


'net.IP{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0x3e, 0x4c, 0x2f, 0x9}'

您可以转换ipMax为 IPv4 表示以获得所需的结果:


ipMax := net.ParseIP("62.76.47.9").To4()


查看完整回答
反对 回复 2023-05-15
?
皈依舞

TA贡献1851条经验 获得超3个赞

使用IPAddress Go library,我可以提供两种方法,一种方法类似于您通过迭代整个子网的建议,另一种方法是从给定地址开始并递增直到达到最大地址。请注意,这些解决方案与 IPv6 的工作方式相同。免责声明:我是项目经理。

62.76.47.12/28请注意,和 的封闭 CIDR 子网62.76.47.9/2862.76.47.0/28,因此您实际上不需要同时提供62.76.47.12/2862.76.47.9,您可以简单地提供单个字符串62.76.47.9/28。但是为了坚持您提出的示例,我将使用这两个字符串。

import (

    "fmt"

    "github.com/seancfoley/ipaddress-go/ipaddr"

)


func main() {

    cidrString, addrString := "62.76.47.12/28", "62.76.47.9"

    addr := ipaddr.NewIPAddressString(addrString).GetAddress()

    cidrAddr := ipaddr.NewIPAddressString(cidrString).GetAddress()

    iterateThroughSubnet(addr, cidrAddr) // solution 1

    incrementAddressToMax(addr, cidrAddr) // solution 2

}


func iterateThroughSubnet(addr, cidrAddr *ipaddr.IPAddress) {

    block := cidrAddr.ToPrefixBlock().WithoutPrefixLen()

    fmt.Printf("\nstarting with block %s, equivalent to %v,\nand address %s\n",

        cidrAddr.ToPrefixBlock(), block, addr)

    iterator := block.Iterator()

    for next := iterator.Next(); next != nil; next = iterator.Next() {

        fmt.Printf("compare %s with %s: %d\n", next, addr, next.Compare(addr))

    }

}


func incrementAddressToMax(addr, cidrAddr *ipaddr.IPAddress) {

    block := cidrAddr.ToPrefixBlock()

    max := block.GetUpper()

    fmt.Printf("\nstarting with block %s and address %s,\n"+

        "incrementing to block max %s\n", block, addr, max)

    for ; addr.Compare(max) <= 0; addr = addr.Increment(1) {

        fmt.Println(addr)

    }

}

输出:


starting with block 62.76.47.0/28, equivalent to 62.76.47.0-15,

and address 62.76.47.9

compare 62.76.47.1 with 62.76.47.9: -1

compare 62.76.47.2 with 62.76.47.9: -1

compare 62.76.47.3 with 62.76.47.9: -1

compare 62.76.47.4 with 62.76.47.9: -1

compare 62.76.47.5 with 62.76.47.9: -1

compare 62.76.47.6 with 62.76.47.9: -1

compare 62.76.47.7 with 62.76.47.9: -1

compare 62.76.47.8 with 62.76.47.9: -1

compare 62.76.47.9 with 62.76.47.9: 0

compare 62.76.47.10 with 62.76.47.9: 1

compare 62.76.47.11 with 62.76.47.9: 1

compare 62.76.47.12 with 62.76.47.9: 1

compare 62.76.47.13 with 62.76.47.9: 1

compare 62.76.47.14 with 62.76.47.9: 1

compare 62.76.47.15 with 62.76.47.9: 1


starting with block 62.76.47.0/28 and address 62.76.47.9,

incrementing to block max 62.76.47.15/28

62.76.47.9

62.76.47.10

62.76.47.11

62.76.47.12

62.76.47.13

62.76.47.14

62.76.47.15

对于小子网,这两种方法可能同样有效,但是随着子网变大,您希望避免第一个递增遍历整个子网的解决方案。如果需要,使用GetNetIp的方法ipaddr.IPAddress切换到net.IP。


查看完整回答
反对 回复 2023-05-15
  • 3 回答
  • 0 关注
  • 151 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信