Go
This page contains a very simple example for using scrapli to connect to a CLI device (telnet/ssh) as well as a NETCONF server -- there are many more examples here, so check those out too.
CLI
package main
import (
"context"
"fmt"
"time"
scrapligocli "github.com/scrapli/scrapligo/v2/cli" // (1)
scrapligooptions "github.com/scrapli/scrapligo/v2/options" // (2)
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // (3)
defer cancel()
opts := []scrapligooptions.Option{ // (4)
scrapligooptions.WithDefinitionFileOrName(scrapligocli.NokiaSrlinux),
scrapligooptions.WithUsername("scrapli"),
scrapligooptions.WithPassword("verysecurepassword"),
}
c, err := scrapligocli.NewCli( // (5)
"myrouter",
opts...,
)
if err != nil {
panic(fmt.Sprintf("failed creating cli object, error: %v", err))
}
_, err = c.Open(ctx) // (6)
if err != nil {
panic(err)
}
defer c.Close(ctx) // (7)
r, err := c.SendInput(ctx, "info") // (8)
if err != nil {
panic(err)
}
fmt.Println(r.Result()) // (9)
}
- The
clipackage of course holds cli related things, including theNewClifunction we'll use a bit later. - We'll also import the
optionspackage -- this holds all the options we'll use when establishing our connection. - Rejoice gophers! scrapligo uses context cancellation like you'd expect any go package to do (this was historically not the case).
- Here we create a slice of options to pass to the
NewClifunction -- in this example we are setting the definition to use theNokiaSrlinuxdefinition, and setting a dummy username/password. - And now we can create our
Cliobject with our given options to connect to "myrouter" in this case. - Now we can open the connection using the appropriately named
Openmethod. - Always make sure to defer closing the connection -- this is especially important for daemons/long running programs as the
Closemethod is where resources will be freed. - We can use
SendInputto... send an input to the device... - Finally, we can access the full output from the result using the
Result()method of theResultobject that we got returned fromSendInput.
NETCONF
package main
import (
"context"
"fmt"
"time"
scrapligonetconf "github.com/scrapli/scrapligo/v2/netconf" // (1)
scrapligooptions "github.com/scrapli/scrapligo/v2/options" // (2)
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // (3)
defer cancel()
opts := []scrapligooptions.Option{ // (4)
scrapligooptions.WithUsername("scrapli"),
scrapligooptions.WithPassword("verysecurepassword"),
}
n, err := scrapligonetconf.NewNetconf( // (5)
"myrouter",
opts...,
)
if err != nil {
panic(fmt.Sprintf("failed creating netconf object, error: %v", err))
}
_, err = n.Open(ctx) // (6)
if err != nil {
panic(err)
}
defer n.Close(ctx) // (7)
r, err := n.GetConfig(ctx) // (8)
if err != nil {
panic(err)
}
fmt.Println(r.Result()) // (9)
}
- The
netconfpackage of course holds NETCONF related things, including theNewNetconffunction we'll use a bit later. - We'll also import the
optionspackage -- this holds all the options we'll use when establishing our connection -- same as with the CLI example. - Again, normal context things here.
- Here we create a slice of options to pass to the
NewNetconffunction -- in this example we are just setting a dummy username/password. - And now we can create our
Netconfobject with our given options to connect to "myrouter" in this case. - Now we can open the connection using the appropriately named
Openmethod. - Always make sure to defer closing the connection -- this is especially important for daemons/long running programs as the
Closemethod is where resources will be freed. - We can use
GetConfigto... send a get-config rpc to the device... - Finally, we can access the full output from the result using the
Result()method of theResultobject that we got returned fromGetConfig.