duan0708676887
2015-07-02 17:48
浏览 36
已采纳

如何获得下一个IP地址?

I'm considering to call net.IP.String(), strings.Split(ip, "."), some code to calculate all the corner cases and finally net.ParseIP(s). Is there a better way to this?. Below is the code of my current implementation (no special case handled).

package main

import (
    "fmt"
    "net"
    "strconv"
    "strings"
)

func main() {
    ip := net.ParseIP("127.1.0.0")
    next, err := NextIP(ip)
    if err != nil {
        panic(err)
    }
    fmt.Println(ip, next)
}

func NextIP(ip net.IP) (net.IP, error) {
    s := ip.String()
    sa := strings.Split(s, ".")
    i, err := strconv.Atoi(sa[2])
    if err != nil {
        return nil, err
    }
    i++
    sa[3] = strconv.Itoa(i)
    s = strings.Join(sa, ".")
    return net.ParseIP(s), nil
}

图片转代码服务由CSDN问答提供 功能建议

我正在考虑调用 net.IP.String() strings.Split(ip,“。”),一些用于计算所有极端情况的代码,最后是 net.ParseIP(s)。 有更好的方法吗? 下面是我当前实现的代码(未处理任何特殊情况)。

 包main 
 
import(
“ fmt” 
“ net” 
“ strconv  “ 
” strings“ 
)
 
func main(){
 ip:= net.ParseIP(” 127.1.0.0“)
接下来,错误:= NextIP(ip)
如果错误!=无 {
 panic(err)
} 
 fmt.Println(ip,next)
} 
 
func NextIP(ip net.IP)(net.IP,错误){
s:= ip.String(  )
 sa:= strings.Split(s,“。”)
i,err:= strconv.Atoi(sa [2])
如果err!= nil {
 return nil,err 
} 
  i ++ 
 sa [3] = strconv.Itoa(i)
s =字符串。Join(sa,“。”)
返回net.ParseIP(s),nil 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dty98339 2015-07-02 18:14
    已采纳

    Just increment the last octet in the IP address

    ip := net.ParseIP("127.1.0.0")
    // make sure it's only 4 bytes
    ip = ip.To4()
    // check ip != nil
    ip[3]++  // check for rollover
    fmt.Println(ip)
    //127.1.0.1
    

    That however is technically incorrect, since the first address in the 127.1.0.1/8 subnet is 127.0.0.1. To get the true "first" address, you will also need an IPMask. Since you didn't specify one, you could use DefaultMask for IPv4 addresses (for IPv6 you can't assume a mask, and you must provide it).

    http://play.golang.org/p/P_QWwRIBIm

    ip := net.IP{192, 168, 1, 10}
    ip = ip.To4()
    if ip == nil {
        log.Fatal("non ipv4 address")
    }
    
    ip = ip.Mask(ip.DefaultMask())
    ip[3]++
    
    fmt.Println(ip)
    //192.168.1.1
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsa2c2255888 2015-11-25 20:35

    I would test against CIDR after the IP has been incremented, so overflows don't change the expected subnet.

    func incrementIP(origIP, cidr string) (string, error) {
        ip := net.ParseIP(origIP)
        _, ipNet, err := net.ParseCIDR(cidr)
        if err != nil {
            return origIP, err
        }
        for i := len(ip) - 1; i >= 0; i-- {
            ip[i]++
            if ip[i] != 0 {
                break
            }
        }
        if !ipNet.Contains(ip) {
            return origIP, errors.New("overflowed CIDR while incrementing IP")
        }
        return ip.String(), nil
    }
    
    评论
    解决 无用
    打赏 举报
  • dongzhuo1733 2018-03-01 19:50

    If all you need is to compute the next IP address, the function nextIP() below will do the trick.

    Usage:

    // output is 1.0.1.0
    fmt.Println(nextIP(net.ParseIP("1.0.0.255"), 1))
    

    nextIP():

    func nextIP(ip net.IP, inc uint) net.IP {
        i := ip.To4()
        v := uint(i[0])<<24 + uint(i[1])<<16 + uint(i[2])<<8 + uint(i[3])
        v += inc
        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)
    }
    

    Playground: https://play.golang.org/p/vHrmftkVjn2

    Gist: https://gist.github.com/udhos/b468fbfd376aa0b655b6b0c539a88c03

    评论
    解决 无用
    打赏 举报
  • dsuxcxqep31023992 2018-04-15 23:03

    I've encountered this problem just now, and I want to share my solution. It's not that efficient but solves the problem in a few lines.

    func nextIP(ip net.IP) net.IP {
        // Convert to big.Int and increment
        ipb := big.NewInt(0).SetBytes([]byte(ip))
        ipb.Add(ipb, big.NewInt(1))
    
        // Add leading zeros
        b := ipb.Bytes()
        b = append(make([]byte, len(ip)-len(b)), b...)
        return net.IP(b)
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题