netaddrs

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2022 License: MPL-2.0 Imports: 6 Imported by: 4

README

GoDoc

Summary

Inspired by go-discover, go-netaddrs is a Go (golang) library and command line tool to discover ip addresses of nodes in a customizable fashion suitable for any environment. It returns IP addresses (IPv4 or IPv6) given a

  1. DNS name, OR
  2. custom executable with optional args which (refer to examples under the folder sample_scripts/):
    • on success - exits with 0 and prints whitespace delimited IP (v4/v6) addresses to stdout.
    • on failure - exits with a non-zero code and optionally prints an error message of up to 1024 bytes to stderr.

Command Line Tool Usage

Install the command line tool with:

go get -u github.com/hashicorp/go-netaddrs/cmd/netaddrs

Example usage

$ netaddrs -q ip "exec=/usr/local/bin/query_ec2.sh"

# Output
172.25.16.77 172.25.42.80 fe80::1ff:fe23:4567:890a%3
$ netaddrs -q ip "exec=discover -q addrs provider=aws region=us-west-2 tag_key=consul-server tag_value=true"

# Output
172.25.19.221 172.25.24.182 172.25.21.52
$ netaddrs -q ip "consul-cluster.private.consul.11eb5b95-2882-0215-b2c7-0242ac11000d.aws.hcp.dev"

# Output
172.25.19.221 172.25.24.182 172.25.21.52

Library Usage

Install the library with:

go get -u github.com/hashicorp/go-netaddrs

Usage sample:

import netaddrs "github.com/hashicorp/go-netaddrs"

func ServerAddresses(server_addresses_cfg string, logger hclog.Logger) ([]string, error) {


   // Example `server_addresses_cfg` values:
   // consul-cluster.private.consul.11eb5b95-2882-0215-b2c7-0242ac11000d.aws.hcp.dev
   // exec=query_ec2.sh
   // exec=discover -q addrs provider=aws region=us-west-2 tag_key=consul-server tag_value=true

   addresses, err := netaddrs.IPAddrs(context.Background(), server_addresses_cfg, logger)
   if err != nil {
       logger.Error("Error retrieving server addresses", err)
       return nil, err
   }


   logger.Info("Server addresses", addresses)
   return addresses, err
}

Testing

$ go test

Future Enhancements

  • Return TCP addresses (host:port) given a DNS name or custom executable with optional args

Documentation

Overview

Package netaddrs provides a function to get IP addresses from a DNS request or by executing a binary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IPAddrs

func IPAddrs(ctx context.Context, cfg string, l Logger) ([]net.IPAddr, error)

IPAddrs looks up and returns IP addresses using the method described by cfg.

If cfg is a DNS name IP addresses are looked up by querying the default DNS resolver for A and AAAA records associated with the DNS name.

If cfg has an exec= prefix, IP addresses are looked up by executing the command after exec=. The command may include optional arguments. Command arguments must be space separated (spaces in argument values can not be escaped). The command may output IPv4 or IPv6 addresses, and IPv6 addresses can optionally include a zone index. The executable must follow these rules:

on success - exit 0 and print whitespace delimited IP addresses to stdout.
on failure - exits with a non-zero code, and should print an error message
             of up to 1024 bytes to stderr.

Use ctx to cancel the operation or set a deadline.

Types

type Logger

type Logger interface {
	// Debug should print the message to the appropriate location, args is a
	// list of structured key/value pairs.
	Debug(msg string, args ...interface{})
}

Logger used by IPAddrs to print debug messages.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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