scrapli / scrapligo

scrapli, but in go!
MIT License
258 stars 42 forks source link

Go Report License: MIT


Source Code: https://github.com/scrapli/scrapligo

Examples: https://github.com/scrapli/scrapligo/tree/main/examples

Go Docs: https://pkg.go.dev/github.com/scrapli/scrapligo


scrapligo -- scrap(e c)li (but in go!) -- is a Go library focused on connecting to devices, specifically network devices (routers/switches/firewalls/etc.) via SSH and NETCONF.

Key Features:

Running the Examples

You need Go 1.16+ installed. Clone the repo and go run any of the examples in the examples folder. Below are a few example outputs.

Executing a number of commands (from a file)

$  go run examples/base_driver/main.go
found prompt: 
csr1000v-1#

sent command 'show version', output received:
 Cisco IOS XE Software, Version 16.09.03
Cisco IOS Software [Fuji], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.9.3, RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2019 by Cisco Systems, Inc.
Compiled Wed 20-Mar-19 07:56 by mcpre
...

Parsing a command output

For more details, check out Network automation options in Go with scrapligo.

$  go run examples/network_driver/textfsm/main.go
Hostname: csr1000v-1
SW Version: 16.9.3
Uptime: 18 minutes

Code Example

package main

import (
  "fmt"
  "github.com/scrapli/scrapligo/driver/options"
  "github.com/scrapli/scrapligo/platform"
)

func main() {
    p, err := platform.NewPlatform(
        // cisco_iosxe refers to the included cisco iosxe platform definition
        "cisco_iosxe",
        "sandbox-iosxe-latest-1.cisco.com",
        options.WithAuthNoStrictKey(),
        options.WithAuthUsername("developer"),
        options.WithAuthPassword("C1sco12345"),
    )
    if err != nil {
        fmt.Printf("failed to create platform; error: %+v\n", err)

        return
    }

    d, err := p.GetNetworkDriver()
    if err != nil {
        fmt.Printf("failed to fetch network driver from the platform; error: %+v\n", err)

        return
    }

    err = d.Open()
    if err != nil {
        fmt.Printf("failed to open driver; error: %+v\n", err)

        return
    }

    defer d.Close()

    r, err := d.SendCommand("show version")
    if err != nil {
        fmt.Printf("failed to send command; error: %+v\n", err)
        return
    }

    fmt.Printf(
        "sent command '%s', output received (SendCommand):\n %s\n\n\n",
        r.Input,
        r.Result,
    )
}

Migrating From Pre 1.0.0

Scrapligo has had a very significant overhaul from v0.x.x versions to the v1.0.0 version, while the user facing API stayed similar (with the very notable exception to actual import paths), here are some (maybe not fully inclusive) list of changes to take note of:

* gopher artwork by @egonelbre