Using NETCONF to modify the configuration of SR Linux, focusing on <edit-config> and <commit> operations.
Write a Go program to change the name of the router and put a description in interface ethernet1-1 using NETCONF.
// 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()
configXML := `
<system xmlns="http://openconfig.net/yang/system">
<config>
<hostname>srl12</hostname>
</config>
</system>
<interface xmlns="urn:nokia.com:srlinux:chassis:interfaces">
<name>ethernet-1/1</name>
<admin-state>enable</admin-state>
<description>Configured by NetAuto</description>
<subinterface>
<index>0</index>
<admin-state>enable</admin-state>
<ipv4>
<address>
<ip-prefix>10.0.0.1/24</ip-prefix>
</address>
</ipv4>
</subinterface>
</interface>`
// Edit the configuration
err = session.EditConfig(ctx, "candidate", configXML)
if err != nil {
log.Fatalf("Failed to edit config: %v", err)
}
fmt.Println("Configuration applied to candidate datastore.")
// Commit the changes
err = session.Commit(ctx)
if err != nil {
log.Fatalf("Failed to commit config: %v", err)
}
fmt.Println("Configuration committed successfully.")
// Verify by getting the config again (optional)
deviceConfig, err := session.GetConfig(ctx, "running")
if err != nil {
log.Fatalf("failed to get config aber commit: %v", err)
}
fmt.Println("--- Running Configuration ---")
fmt.Printf("Config:\n%s\n", deviceConfig)
if err := session.Close(context.Background()); err != nil {
log.Print(err)
}
}- Ensure your
srl_2_nodes.clab.yamlis deployed. - Run
go mod tidy. - Save the code as
main.go. - Run
go run main.go.