cidr

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2018 License: BSD-3-Clause Imports: 3 Imported by: 3 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Travis Build Status Coverage Status Go Report Card GitHub release

CIDR

CIDR is a simple utility to generate the IPv4 addresses in a CIDR range. It could also be used to check the membership of an IP v4 address in a CIDR range.

Using as a library

In order to start, go get this repository:

go get github.com/adedayo/net/cidr
Example

In your code simply import as usual and enjoy:

package main

import "github.com/adedayo/net/cidr"

func main() {
	ips := cidr.Expand("192.168.2.5/30")

	for _, ip := range ips {
		println(ip)
	}
}

This should print the set of IPs described by the CIDR range 192.168.2.5/30:

192.168.2.4
192.168.2.5
192.168.2.6
192.168.2.7

Using it as a command-line tool

CIDR is also available as a command-line tool

Generating IPs in a CIDR range
cidr 192.168.2.5/30 10.11.12.13/31

This should generate a simply formatted output:

192.168.2.5/30: 192.168.2.4 192.168.2.5 192.168.2.6 192.168.2.7

10.11.12.13/31: 10.11.12.12 10.11.12.13

For a JSON-formatted result, use the JSON -j or --json flag:

cidr --json 192.168.2.5/30 10.11.12.13/31

This should produce:

{
"192.168.2.5/30": ["192.168.2.4", "192.168.2.5", "192.168.2.6", "192.168.2.7"],

"10.11.12.13/31": ["10.11.12.12", "10.11.12.13"]
}
Checking CIDR range membership

The structure of the checking command is as follows

cidr [flag] check <space separated list of CIDR ranges> <delimiter> <space-separated list of IP addresses to check>

The delimiter can be any of contains, c or simply ,

Examples

To check the membership of the IP addresses 192.168.10.3 and 192.168.10.9 in the CIDR ranges 192.168.10.1/30 and 192.168.10.1/28 run

cidr check 192.168.10.1/30 192.168.10.1/28 contains 192.168.10.3 192.168.10.9

The result is

192.168.10.1/30: 192.168.10.3,true 192.168.10.9,false
192.168.10.1/28: 192.168.10.3,true 192.168.10.9,true

For a JSON output the above is equivalent to

cidr --json check 192.168.10.1/30 192.168.10.1/28 , 192.168.10.3 192.168.10.9

This produces

{
"192.168.10.1/30": [{"ip":"192.168.10.3","belongs":true},{"ip":"192.168.10.9","belongs":false}],
"192.168.10.1/28": [{"ip":"192.168.10.3","belongs":true},{"ip":"192.168.10.9","belongs":true}]
}

License

BSD 3-Clause License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expand

func Expand(cidr string) []string

Expand takes a CIDR formatted strings and expands them to corresponding Ipv4 instances including the network and broadcast addresses as first and last elements respectively. Any error returns an empty slice

Types

type Membership

type Membership struct {
	CIDR    string
	IP      string
	Belongs bool
}

Membership describes membership status of an IP in a CIDR range

func Contains

func Contains(cidr string, ips ...string) (mem []Membership)

Contains returns the membership of a set of IPs in a CIDR range

Directories

Path Synopsis
cmd
cidr command

Jump to

Keyboard shortcuts

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