猿问

IP 地址从 maxAddress 开始

我需要在给定 CIDR 和一些缓存地址的情况下开始生成 IP 地址。我在这里有一些代码,我正在做字节。与存储的地址进行比较,只选择那些更大的。

https://play.golang.org/p/yT_Mj4fR_jK

这里出了什么问题?基本上我需要“62.76.47.12/28”中“62.76.47.9”的所有地址。在给定的 CIDR 范围内生成 IP 是众所周知的。

谢谢。


FFIVE
浏览 152回答 3
3回答

慕村9548890

此示例将打印从第一个地址 62.76.47.12/28 到 62.76.47.9 的地址。游乐场: https:&nbsp;//play.golang.org/p/MUtbiKaQ_3-package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "net")func main() {&nbsp; &nbsp; cidr := "62.76.47.12/28"&nbsp; &nbsp; _, ipnet, _ := net.ParseCIDR(cidr)&nbsp; &nbsp; ipFirst := ipnet.IP&nbsp; &nbsp; ipFirstValue := toHost(ipFirst)&nbsp; &nbsp; ipLast := net.ParseIP("62.76.47.9")&nbsp; &nbsp; ipLastValue := toHost(ipLast)&nbsp; &nbsp; fmt.Println("cidr:&nbsp; ", cidr)&nbsp; &nbsp; fmt.Println("first: ", ipFirst)&nbsp; &nbsp; fmt.Println("last:&nbsp; ", ipLast)&nbsp; &nbsp; if ipLastValue < ipFirstValue {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("ugh")&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; for i := ipFirstValue; i < ipLastValue; i++ {&nbsp; &nbsp; &nbsp; &nbsp; addr := toIP(i)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(addr)&nbsp; &nbsp; }}func toHost(ip net.IP) uint32 {&nbsp; &nbsp; i := ip.To4()&nbsp; &nbsp; return uint32(i[0])<<24 + uint32(i[1])<<16 + uint32(i[2])<<8 + uint32(i[3])}func toIP(v uint32) net.IP {&nbsp; &nbsp; v3 := byte(v & 0xFF)&nbsp; &nbsp; v2 := byte((v >> 8) & 0xFF)&nbsp; &nbsp; v1 := byte((v >> 16) & 0xFF)&nbsp; &nbsp; v0 := byte((v >> 24) & 0xFF)&nbsp; &nbsp; return net.IPv4(v0, v1, v2, v3)}

富国沪深

如果你打印ì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()

皈依舞

使用IPAddress Go library,我可以提供两种方法,一种方法类似于您通过迭代整个子网的建议,另一种方法是从给定地址开始并递增直到达到最大地址。请注意,这些解决方案与 IPv6 的工作方式相同。免责声明:我是项目经理。62.76.47.12/28请注意,和 的封闭 CIDR 子网62.76.47.9/28是62.76.47.0/28,因此您实际上不需要同时提供62.76.47.12/28和62.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.9compare 62.76.47.1 with 62.76.47.9: -1compare 62.76.47.2 with 62.76.47.9: -1compare 62.76.47.3 with 62.76.47.9: -1compare 62.76.47.4 with 62.76.47.9: -1compare 62.76.47.5 with 62.76.47.9: -1compare 62.76.47.6 with 62.76.47.9: -1compare 62.76.47.7 with 62.76.47.9: -1compare 62.76.47.8 with 62.76.47.9: -1compare 62.76.47.9 with 62.76.47.9: 0compare 62.76.47.10 with 62.76.47.9: 1compare 62.76.47.11 with 62.76.47.9: 1compare 62.76.47.12 with 62.76.47.9: 1compare 62.76.47.13 with 62.76.47.9: 1compare 62.76.47.14 with 62.76.47.9: 1compare 62.76.47.15 with 62.76.47.9: 1starting with block 62.76.47.0/28 and address 62.76.47.9,incrementing to block max 62.76.47.15/2862.76.47.962.76.47.1062.76.47.1162.76.47.1262.76.47.1362.76.47.1462.76.47.15对于小子网,这两种方法可能同样有效,但是随着子网变大,您希望避免第一个递增遍历整个子网的解决方案。如果需要,使用GetNetIp的方法ipaddr.IPAddress切换到net.IP。
随时随地看视频慕课网APP

相关分类

Go
我要回答