Using a Go NETCONF library (e.g., github.com/Juniper/go-netconf/netconf) to connect to SR Linux and perform <get-config> operations.
Connect to one of your SR Linux nodes via NETCONF and retrieve the entire running configuration. Parse the XML response.
// main.go
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/nemith/netconf"
ncssh "github.com/nemith/netconf/transport/ssh"
"golang.org/x/crypto/ssh"
)
const sshAddr = "clab-srl-2-nodes-srl1:830"
func main() {
config := &ssh.ClientConfig{
User: "admin",
Auth: []ssh.AuthMethod{
ssh.Password("NokiaSrl1!"),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
transport, err := ncssh.Dial(ctx, "tcp", sshAddr, config)
if err != nil {
panic(err)
}
defer transport.Close()
session, err := netconf.Open(transport)
if err != nil {
panic(err)
}
// timeout for the call itself.
ctx, cancel = context.WithTimeout(ctx, 5*time.Second)
defer cancel()
deviceConfig, err := session.GetConfig(ctx, "running")
if err != nil {
log.Fatalf("failed to get config: %v", err)
}
fmt.Println("--- Running Configuration ---")
fmt.Printf("Config:\n%s\n", deviceConfig)
if err := session.Close(context.Background()); err != nil {
log.Print(err)
}
}Instructions:
- Ensure your
srl_2_nodes.clab.yamlis deployed. - Run
go mod init extra1. - Run
go get github.com/Juniper/go-netconf/netconf. - Save the code as
main.go. - Run
go run main.go.