Giter Site home page Giter Site logo

gnetcli's Introduction

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

gnetcli's People

Contributors

gescheit avatar kglushen avatar raydent avatar kerhoff avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.