亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 232 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號