netrasp

package
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2024 License: AGPL-3.0, Apache-2.0 Imports: 11 Imported by: 0

README

netrasp

Netrasp is a package that communicates to network devices over SSH. It takes care of handling the pty terminal of network devices giving you an API with common actions such as executing commands and configuring devices.

Warning

Netrasp is in pre release mode so some parts of the API might change before the initial version is released.

Example

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/networklore/netrasp/pkg/netrasp"
)

func main() {
	device, err := netrasp.New("switch1",
		netrasp.WithUsernamePassword("my_user", "my_password123"),
		netrasp.WithDriver("ios"),
	)
	if err != nil {
		log.Fatalf("unable to create client: %v", err)
	}

	ctx, cancelOpen := context.WithTimeout(context.Background(), 2000*time.Millisecond)
	defer cancelOpen()
	err = device.Dial(ctx)
	if err != nil {
		fmt.Printf("unable to connect: %v\n", err)

		return
	}
	defer device.Close(context.Background())

	ctx, cancelRun := context.WithTimeout(context.Background(), 300*time.Millisecond)
	defer cancelRun()
	output, err := device.Run(ctx, "show running")
	if err != nil {
		fmt.Printf("unable to run command: %v\n", err)

		return
	}
	fmt.Println(output)
}

Network Device Support

The initial release of Netrasp comes with support for the following platforms:

  • Cisco IOS: netrasp.WithDriver("ios")
  • Cisco NXOS: netrasp.WithDriver("nxos")
  • Cisco ASA: netrasp.WithDriver("asa")
  • Nokia SR OS: netrasp.WithDriver("sros")

Use cases

You can use Netrasp as a package as in the example above of combine it with something like Gornir to get the same type of experience you'd have from using Netmiko and Nornir in the Python world.

Blog Posts

Credits

Netrasp was created by Patrick Ogenstad. Special thanks to David Barroso for providing feedback and recommendations of the structure and code.

Documentation

Overview

Package netrasp provides an easy way to communicate with network devices

Using an SSH connection it lets you send commands and configure devices that only supports screen scraping.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigCommand

type ConfigCommand struct {
	// The command that was sent to the device
	Command string
	// The output seen after entering the command
	Output string
}

ConfigCommand contains a configuration command together with the output from that command.

type ConfigOpt

type ConfigOpt interface {
	// contains filtered or unexported methods
}

ConfigOpt configures a Netrasp connection and platform.

func WithDialTimeout

func WithDialTimeout(t time.Duration) ConfigOpt

WithDialTimeout allows you to configure timeout for dialing SSH server.

func WithDriver

func WithDriver(name string) ConfigOpt

WithDriver tells Netrasp which network driver to use for the connection for example "asa", "ios", "nxos".

func WithInsecureIgnoreHostKey

func WithInsecureIgnoreHostKey() ConfigOpt

WithInsecureIgnoreHostKey allows you to ignore the validation of the public SSH key of a device against a reference in a known_hosts file. Using this option should be considered a security risk.

func WithSSHCipher

func WithSSHCipher(name string) ConfigOpt

WithSSHCipher allows you to configure additional SSH Ciphers that the connection will use. The parameter can be useful if your device doesn't support the default ciphers.

func WithSSHKeyExchange

func WithSSHKeyExchange(name string) ConfigOpt

WithSSHKeyExchange allows you to configure additional SSH key exchange algorithms. The parameter can be useful if your device doesn't support the default algorithms.

func WithSSHPort

func WithSSHPort(port int) ConfigOpt

WithSSHPort allows you to specify an alternate SSH port, defaults to 22.

func WithUsernamePassword

func WithUsernamePassword(username string, password string) ConfigOpt

func WithUsernamePasswordEnableSecret

func WithUsernamePasswordEnableSecret(username string, password string, enableSecret string) ConfigOpt

type ConfigResult

type ConfigResult struct {
	ConfigCommands []ConfigCommand
}

ConfigResult is returned by a Configure operation and contains output and results.

type Platform

type Platform interface {
	// Disconnect from a device
	Close(context.Context) error
	// Configure device with the provided commands
	Configure(context.Context, []string) (ConfigResult, error)
	// Open a connection to a device
	Dial(context.Context) error
	// Elevate privileges on device
	Enable(context.Context) error
	// Run a command against a device
	Run(context.Context, string) (string, error)
	// Run a command against a device and search for a specific prompt
	RunUntil(context.Context, string, *regexp.Regexp) (string, error)
}

Platform defines an interface for network drivers.

func New

func New(hostname string, opts ...ConfigOpt) (Platform, error)

New creates a new SSH connection to the device.

Jump to

Keyboard shortcuts

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