MCPcopy Index your code
hub / github.com/annetutil/gnetcli

github.com/annetutil/gnetcli @v1.3.5

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.3.5 ↗ · + Follow
1,871 symbols 5,289 edges 129 files 310 documented · 17%
What it actually does AI analysis from the code graph — generated when you open this
loading…
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/gnetcli_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", "host_params": {"device": "huawei", "credentials": {"login": "test", "password": "test"}}, "string_result": true}' localhost:50051 gnetcli.Gnetcli.Exec

Start GRPC-server via docker

Clone the repository, build the image and run the container:

git clone https://github.com/annetutil/gnetcli.git
cd gnetcli
docker build -f image/Dockerfile -t gnetcli_server .
docker run -p 50051:50051 gnetcli_server

Extension points exported contracts — how you extend this code

Action (Interface)
(no doc) [13 implementers]
pkg/testutils/mock/action.go
Action (Interface)
(no doc) [13 implementers]
internal/gvendor/gvendor.go
Connector (Interface)
(no doc) [4 implementers]
pkg/streamer/streamer.go
CmdRes (Interface)
CmdRes is an interface for command result. [1 implementers]
pkg/cmd/cmd.go
Device (Interface)
(no doc) [3 implementers]
pkg/device/device.go
ClientSessionCache (Interface)
ClientSessionCache is a cache of ClientSessionState objects that can be used by a client to resume a TLS session with a [1 …
internal/tls_hack/common.go
Credentials (Interface)
(no doc) [2 implementers]
pkg/credentials/credentials.go
Expr (Interface)
(no doc) [2 implementers]
pkg/expr/expr.go

Core symbols most depended-on inside this repo

Error
called by 153
pkg/cmd/cmd.go
FromPattern
called by 124
pkg/expr/builders.go
Write
called by 100
internal/gvendor/gvendor.go
NewSimpleExprLast200
called by 89
pkg/expr/builders.go
sendAlert
called by 84
internal/tls_hack/conn.go
NewCmd
called by 71
pkg/cmd/cmd.go
String
called by 55
pkg/expr/expr.go
ExprTester
called by 53
pkg/testutils/expr.go

Shape

Method 948
Function 609
Struct 195
Class 38
Interface 38
TypeAlias 27
FuncType 16

Languages

Go93%
Python7%

Modules by API surface

pkg/server/proto/server.pb.go160 symbols
pkg/streamer/ssh/ssh.go86 symbols
pkg/server/proto/server_grpc.pb.go74 symbols
pkg/streamer/console/console.go60 symbols
internal/tssh/tssh.go58 symbols
pkg/device/genericcli/genericcli.go56 symbols
internal/tls_hack/handshake_messages.go56 symbols
pkg/cmd/cmd.go55 symbols
internal/tls_hack/cipher_suites.go54 symbols
pkg/streamer/streamer.go50 symbols
pkg/server/server.go49 symbols
internal/tls_hack/conn.go49 symbols

For agents

$ claude mcp add gnetcli \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact