commands

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CommandCheck = &cli.Command{
		Name:  "check",
		Usage: "check IP against range of CIDR blocks",
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:     "ips",
				Aliases:  []string{"i"},
				Usage:    "CSV list of IPs with masks (csv)",
				Required: true,
			},
			&cli.StringFlag{
				Name:     "blocks",
				Aliases:  []string{"b"},
				Usage:    "CIDR blocks to be checked (csv)",
				Required: true,
			},
		},
		Action: func(c *cli.Context) error {
			kl := k.Extend("check")

			// Get CIDR blocks
			var blocks []*net.IPNet
			for _, cidr := range strings.Split(c.String("blocks"), ",") {
				_, ipv4Net, err := net.ParseCIDR(cidr)
				if err != nil {
					return err
				}
				blocks = append(blocks, ipv4Net)
			}
			kl.Extend("blocks").Printf("total CIDR Blocks %d", len(blocks))

			// Get IPs
			var ips []*net.IPNet
			for _, ip := range strings.Split(c.String("ips"), ",") {
				_, ipv4Net, err := net.ParseCIDR(ip)
				if err != nil {
					return err
				}
				ips = append(ips, ipv4Net)
			}

			kl.Extend("ips").Printf("total IPs %d", len(ips))

			for _, ip := range ips {
				if ip.String() == "0.0.0.0/0" {
					fmt.Printf("%s is in ALL CIDR sets\n", ip.String())
				} else if ok, cidr := isCiderIn(ip, blocks); ok {
					fmt.Printf("%s is in CIDR %s\n", ip.String(), cidr)
				} else {
					fmt.Printf("%s is NOT in CIDR set\n", ip.String())
				}
			}

			return nil
		},
	}
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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