devcon

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2025 License: MIT Imports: 10 Imported by: 0

README

Devcon

Overview

A module to ssh into devices and run commands. It supports running a single command in a remote session or running several commands in an interactive session.

Juniper Package

There is also a Juniper package that wraps around the base devcon package that has some logic built in to get structured data back from the devices. It also allows you to perform a "dry run" by logging in, applying a config, printing a diff, and then rolling it back before logging out of the device.

Authentication Methods

This package supports the following authentication methods:

  • via username/password
  • via an SSH key file

Usage

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/montybeatnik/devcon"
)

// Example with Password
func main() {
	devIP := "10.0.0.60"
	client := devcon.NewClient(
		os.Getenv("SSH_USER"),
		devIP,
		devcon.Password(os.Getenv("SSH_PASSWORD")),
	)
	out, err := client.Run("show version")
	if err != nil {
		log.Fatalf("command failed: %v", err)
	}
	fmt.Println(out)
}

// Example with Private Key
func main() {
	homeDir, err := os.UserHomeDir()
	if err != nil {
		log.Fatal(err)
	}
	keyFile := filepath.Join(homeDir, ".ssh/id_rsa")
	if err != nil {
		log.Fatal(err)
	}
	devIP := "10.0.0.60"
	client := devcon.NewClient(
		os.Getenv("SSH_USER"),
		devIP,
		devcon.PrivateKey(keyFile),
	)
	out, err := client.Run("show version")
	if err != nil {
		log.Fatalf("command failed: %v", err)
	}
	fmt.Println(out)
}

// Simple concurrency example with wait group

TODO

  • Add support for public key auth.
  • Add Juniper package
    • Config Differ
    • Apply Configs
    • First Operational Command Example

Profile

  • go test -v -run Run -cpuprofile cpu.prof -memprofile mem.prof -bench .

Testing

Unit tests
All tests

go test -v

A specific test

go test -v -run RunCommand

Specific tests matching a pattern

go test -v -run Run

Benchmarks

go test -run RunCommand -bench=.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAuthFailure = errors.New("failed authentication")
	ErrTimeout     = errors.New("timeout")
)

Functions

This section is empty.

Types

type Option added in v0.4.0

type Option func(*SSHClient)

func WithHostKeyCallback added in v0.3.1

func WithHostKeyCallback(knownHostsFile string) Option

WithHostKeyCallback sets the SSHClient's initializes the client with an allow list of known trusted hosts.

func WithPassword added in v0.3.1

func WithPassword(pw string) Option

WithPassword sets SSHClient's password.

func WithPort added in v0.3.1

func WithPort(port string) Option

WithPort sets SSHClient's listening port.

func WithPrivateKey added in v0.3.1

func WithPrivateKey(keyfile string) Option

WithPrivateKey sets SSHClient's private key.

func WithTimeout added in v0.3.1

func WithTimeout(seconds time.Duration) Option

WithTimeout sets SSHClient's timeout value.

type SSHClient added in v0.2.1

type SSHClient struct {
	// contains filtered or unexported fields
}

SSHClient holds the elements to setup an SSH client

func NewClient

func NewClient(user, target string, opts ...Option) *SSHClient

NewClient is a factory function that takes in SSH parameters and returns a new client

func (*SSHClient) Run added in v0.3.0

func (c *SSHClient) Run(cmd string) (string, error)

Run takes in a command and attempts to establishe a remote session and run the command.

func (*SSHClient) RunAll added in v0.3.0

func (c *SSHClient) RunAll(cmds ...string) (string, error)

RunAll takes in one or more commands. It establishes a remote session with the target IP and attempts to run all of the commands supplied. You must remember to exit as this method does establish an interactive session.

Jump to

Keyboard shortcuts

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