gnetcli

module
v1.0.21 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT

README

Gnetcli

The ultimate solution for CLI automation in Golang. It provides a universal way to execute arbitrary commands using a CLI, eliminating the need for screen scraping with expect. The project consist of go-library, GRPC server and CLI tool. It is typically used for automating network equipment such as Cisco, Juniper, Huawei, etc.

Feature Overview:

  • Execute commands instead of just reading and writing.
  • Pager, questions and error handling are supported.
    Describe CLI interface in a few expressions and get full power of the project.
  • The project supports several network vendors, including Huawei, Juniper, Cisco, and RouterOS.
  • Netconf is supported.
    Exec netconf in same manner as text command to simplify automation workflow.
  • SSH tunneling is supported.
  • Clean output
    Evaluation of terminal control codes and removal of echoes.
  • CLI and GRPC-server for interacting with non-Go projects and other automations.

Documentation available here.

Quick Start

Go-library

Installation in go project:

go get -u github.com/annetutil/gnetcli

A program which execute display interfaces and return output, error and exit status.

package main

import (
	"context"
	"fmt"
	"time"

	"go.uber.org/zap"

	"github.com/annetutil/gnetcli/pkg/cmd"
	dcreds "github.com/annetutil/gnetcli/pkg/credentials"
	"github.com/annetutil/gnetcli/pkg/device/huawei"
	"github.com/annetutil/gnetcli/pkg/streamer/ssh"
)

func main() {
	host := "somehost"
	password := "mypassword"
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	logger := zap.Must(zap.NewDevelopmentConfig().Build())

	creds := dcreds.NewSimpleCredentials(
		dcreds.WithUsername(dcreds.GetLogin()),
		dcreds.WithSSHAgentSocket(dcreds.GetDefaultAgentSocket()), // try pubkey auth using agent
		dcreds.WithPassword(dcreds.Secret(password)), // and password
		dcreds.WithLogger(logger),
	)
	connector := ssh.NewStreamer(host, creds, ssh.WithLogger(logger))
	dev := huawei.NewDevice(connector) // huawei CLI upon SSH
	err := dev.Connect(ctx)            // connection happens here
	if err != nil{
		panic(err)
    }
	defer dev.Close()
	res, _ := dev.Execute(cmd.NewCmd("display interfaces"))
	if res.Status() == 0 {
		fmt.Printf("Result: %s\n", res.Output())
	} else {
		fmt.Printf("Error: %s\nStatus: %d\n", res.Status(), res.Error())
	}
}
CLI
go install github.com/annetutil/gnetcli/cmd/cli@latest
cli -hostname myhost -devtype huawei -debug -command $'dis clock\ndis ver0' -password $password -json
[
  {
    "output": "2023-10-29 10:00:00\nSunday\nTime Zone(UTC) : UTC\n",
    "error": "",
    "status": 0,
    "cmd": "dis clock"
  },
  {
    "output": "",
    "error": "              ^\nError: Unrecognized command found at '^' position.\n",
    "status": 1,
    "cmd": "dis ver0"
  }
]
GRPC-server

Install and start the server:

go install github.com/annetutil/gnetcli/cmd/server@latest
server -debug -basic-auth mylogin:mysecret

Exec a command on a device using GRPC

TOKEN=$(echo -n "$LOGIN:$PASSWORD" | base64)
grpcurl -H "Authorization: Basic $TOKEN" -plaintext -d '{"host": "hostname", "cmd": "dis clock", "device": "huawei", "string_result": true}' localhost:50051 gnetcli.Gnetcli.Exec

Directories

Path Synopsis
benchmarks module
cmd
cli
examples
internal
pkg
cmd
Package cmd describes command and command result interface and implementations.
Package cmd describes command and command result interface and implementations.
credentials
Package credentials describes credentials.
Package credentials describes credentials.
device
Package device describes high-level interface for interaction with a device.
Package device describes high-level interface for interaction with a device.
device/cisco
Package cisco implements Cisco Catalyst CLI using genericcli.
Package cisco implements Cisco Catalyst CLI using genericcli.
device/genericcli
Package genericcli implements Device interface using regular expressions.
Package genericcli implements Device interface using regular expressions.
device/huawei
Package huawei implements huawei CLI using genericcli.
Package huawei implements huawei CLI using genericcli.
device/juniper
Package juniper implements juniper CLI using genericcli.
Package juniper implements juniper CLI using genericcli.
device/netconf
Package netconf implements netconf.
Package netconf implements netconf.
device/nxos
Package nxos implements Cisco Nexus CLI using genericcli.
Package nxos implements Cisco Nexus CLI using genericcli.
device/pc
Package pc implements upon generic SSH-server with ability to exec like Linux, macOS, etc.
Package pc implements upon generic SSH-server with ability to exec like Linux, macOS, etc.
device/ros
Package ros implements RouterOS CLI using genericcli.
Package ros implements RouterOS CLI using genericcli.
expr
Package expr implements text matching.
Package expr implements text matching.
server
Package server implements GRPC-server upon gnetcli library.
Package server implements GRPC-server upon gnetcli library.
streamer
Package streamer describes interface for interaction on network level.
Package streamer describes interface for interaction on network level.
streamer/ssh
Package ssh implements SSH transport.
Package ssh implements SSH transport.
streamer/telnet
Package telnet implements telnet transport at very basic level.
Package telnet implements telnet transport at very basic level.
terminal
Package terminal implements terminal evaluation functions.
Package terminal implements terminal evaluation functions.
testutils
Package testutils provides functions to use in complex tests.
Package testutils provides functions to use in complex tests.
trace
Package trace provides interface and implementation for collecting communication with an CLI.
Package trace provides interface and implementation for collecting communication with an CLI.

Jump to

Keyboard shortcuts

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