Skip to content

Latest commit

 

History

History
71 lines (58 loc) · 1.65 KB

File metadata and controls

71 lines (58 loc) · 1.65 KB

** Module 3 Extra 01: NETCONF with Go - Basic Get Operations**

Concept:

Using a Go NETCONF library (e.g., github.com/Juniper/go-netconf/netconf) to connect to SR Linux and perform <get-config> operations.

Challenge:

Connect to one of your SR Linux nodes via NETCONF and retrieve the entire running configuration. Parse the XML response.

Code Example:

// 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:

  1. Ensure your srl_2_nodes.clab.yaml is deployed.
  2. Run go mod init extra1.
  3. Run go get github.com/Juniper/go-netconf/netconf.
  4. Save the code as main.go.
  5. Run go run main.go.