iprange

package module
v0.0.0-...-da33fad Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2024 License: Apache-2.0 Imports: 8 Imported by: 1

README

iprange

Go Reference

这是一个通过给定IP范围字符串生成IP集合的工具.

This is a tool for generating IP SETS from a given IP range string.

Use as a library

import: import "github.com/XinRoom/iprange"

Simple to use:

package main

import "github.com/XinRoom/iprange"
import "fmt"

func main() {
	ipSet, err := iprange.GenIpSet("1.1.1.1/30")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Sprintln("1.1.1.1/30 GenIpSet is %s", ipSet)
}

Iterator (Save memory) 迭代器(省内存):

package main

import "github.com/XinRoom/iprange"
import "fmt"

func main() {
	it, startIp, err := iprange.NewIter("1.1.1.1/30")
	if err != nil {
		fmt.Println(err)
		return
	}
	for itn := startIp; it.HasNext(); itn = it.Next() {
		fmt.Println(itn)
	}
}

GetIpByIndex (For random IP and progress) 通过索引获取IP(可用于乱序IP生成和进度恢复):

package main

import "github.com/XinRoom/iprange"
import "fmt"

func main() {
	it, _, err := iprange.NewIter("1.1.1.1/30")
	if err != nil {
		fmt.Println(err)
		return
	}
	for i := uint64(0); i < it.TotalNum(); i++ {
		fmt.Println(it.GetIpByIndex(i))
	}
}

Contains 判断IP是包含于对应IP字符串:

package main

import (
	"github.com/XinRoom/iprange"
	"net"
)
import "fmt"

func main() {
	it, _, err := iprange.NewIter("1.1.1.1/30")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(it.Contains(net.ParseIP("1.1.1.1")))
}

Cmd Build

git clone https://github.com/XinRoom/iprange
cd iprange
go build cmd/iprange.go

Cmd Usage

.\iprange.exe
Gen Ip Set.
Usage: iprange.exe ipStr [ipStr/file] ...
IP format can :
        1.1.1.1
        1.1.1.1-2
        1.1.1-2.0-1
        1.1.1.1/30
        2001::59:63
        2001::59:63-89
        ...
in addition: Support multiple parameters, file and commas

Cmd Feature

1. CidrMode 1.1.1.1/30
.\iprange.exe 1.1.1.1/30     
1.1.1.0
1.1.1.1
1.1.1.2
1.1.1.3
2. WideMode 1.1.1.6-1.1.1.8
.\iprange.exe 1.1.1.6-1.1.1.8                  
1.1.1.6
1.1.1.7
1.1.1.8
3. NarrowMode 1.1-2.1-2.1
.\iprange.exe 1.1-2.1-2.1
1.1.1.1
1.1.2.1
1.2.1.1
1.2.2.1
4. IPv6
.\iprange.exe 2002:2::1-9
2002:2::1
2002:2::2
2002:2::3
2002:2::4
2002:2::5
2002:2::6
2002:2::7
2002:2::8
2002:2::9
5. File
.\iprange.exe .\ips.txt
1.1.2.3
1.1.2.4
1.1.2.5
6. Stdin
echo 1.1.1.2-5 | .\iprange.exe
1.1.1.2
1.1.1.3
1.1.1.4
1.1.1.5

Documentation

Index

Constants

View Source
const (
	CidrMode   = iota // CIDR
	WideMode          // 1.1.1.1-1.1.2.3
	NarrowMode        // 1-3.1-5.4.1-7
)

Variables

This section is empty.

Functions

func GenIpSet

func GenIpSet(ipStr string) (outs []net.IP, err error)

GenIpSet simple generate a set of ip

Types

type Iter

type Iter struct {
	// contains filtered or unexported fields
}

Iter Iterator

func NewIter

func NewIter(ipStr string) (it *Iter, startIp net.IP, err error)

func (*Iter) Contains

func (it *Iter) Contains(ip net.IP) bool

Contains Check whether the IP address is included

func (*Iter) GetIpByIndex

func (it *Iter) GetIpByIndex(index uint64) net.IP

GetIpByIndex ...

func (*Iter) HasNext

func (it *Iter) HasNext() bool

func (*Iter) Next

func (it *Iter) Next() net.IP

func (*Iter) TotalNum

func (it *Iter) TotalNum() uint64

TotalNum Calculating the Total NUMBER of IP addresses

type RangeClassMate

type RangeClassMate []RangeParseMate

RangeClassMate IP Range of every byte

type RangeParseMate

type RangeParseMate struct {
	// contains filtered or unexported fields
}

RangeParseMate Range Info

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL