cidr

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2018 License: BSD-3-Clause Imports: 4 Imported by: 4

README

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.

GoDoc Build Status Coverage Status Go Report Card GitHub release GitHub license

Using as a library

In order to start, go get this repository:

go get github.com/adedayo/cidr
Example

In your code simply import as usual and enjoy:

package main

import "github.com/adedayo/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.

Installation

Prebuilt binaries may be found for your operating system here: https://github.com/adedayo/cidr/releases

For macOS X, you could install via brew as follows:

brew tap adedayo/tap
brew install cidr
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 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

Jump to

Keyboard shortcuts

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