3 回答

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊
此示例將打印從第一個(gè)地址 62.76.47.12/28 到 62.76.47.9 的地址。
游樂場(chǎng): 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)
}

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊
如果你打印ìpMax
,你會(huì)看到它的底層表示使用了 16 個(gè)字節(jié)。(另請(qǐng)參閱文檔
fmt.Printf("'%#v'\n",ipMax)
'net.IP{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0x3e, 0x4c, 0x2f, 0x9}'
您可以轉(zhuǎn)換ipMax為 IPv4 表示以獲得所需的結(jié)果:
ipMax := net.ParseIP("62.76.47.9").To4()

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊
使用IPAddress Go library,我可以提供兩種方法,一種方法類似于您通過迭代整個(gè)子網(wǎng)的建議,另一種方法是從給定地址開始并遞增直到達(dá)到最大地址。請(qǐng)注意,這些解決方案與 IPv6 的工作方式相同。免責(zé)聲明:我是項(xiàng)目經(jīng)理。
62.76.47.12/28
請(qǐng)注意,和 的封閉 CIDR 子網(wǎng)62.76.47.9/28
是62.76.47.0/28
,因此您實(shí)際上不需要同時(shí)提供62.76.47.12/28
和62.76.47.9
,您可以簡(jiǎn)單地提供單個(gè)字符串62.76.47.9/28
。但是為了堅(jiān)持您提出的示例,我將使用這兩個(gè)字符串。
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
對(duì)于小子網(wǎng),這兩種方法可能同樣有效,但是隨著子網(wǎng)變大,您希望避免第一個(gè)遞增遍歷整個(gè)子網(wǎng)的解決方案。如果需要,使用GetNetIp的方法ipaddr.IPAddress切換到net.IP。
- 3 回答
- 0 關(guān)注
- 230 瀏覽
添加回答
舉報(bào)