Using the Get RPC to fetch specific operational state or configuration. Understanding gNMI Paths. Retrieving interface status.
// gnmi_get_name.go
package main
import (
"context"
"fmt"
"os"
"github.com/openconfig/gnmic/pkg/api"
)
const (
targetAddress = "172.20.20.2:57400" // Replace with your cEOS container IP and gNMI port
username = "admin"
password = "NokiaSrl1!"
)
func main() {
target, err := api.NewTarget(
api.Address(targetAddress),
api.Username(username),
api.Password(password),
api.SkipVerify(true),
api.Insecure(false),
)
if err != nil {
fmt.Println(err, "failed to create gnmi client")
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create a gNMI client
err = target.CreateGNMIClient(ctx)
if err != nil {
fmt.Println(err, "failed to create gnmi client")
os.Exit(1)
}
defer target.Close()
// send the created gNMI GetRequest to the created target
// create a GetRequest
getReq, err := api.NewGetRequest(
api.Path("/system/name/host-name"),
api.Encoding("json_ietf"),
)
if err != nil {
fmt.Println(err, "failed to execute get request")
os.Exit(1)
}
// send the created gNMI GetRequest to the created target
getResp, err := target.Get(ctx, getReq)
if err != nil {
fmt.Println(err, "failed to execute get request")
os.Exit(1)
}
for _, notif := range getResp.Notification {
for _, update := range notif.Update {
fmt.Printf("Path: %s\n", update.Path)
fmt.Printf("Return value: %s\n", update.Val.GetJsonIetfVal())
}
}
}// gnmi_get_interface.go
package main
import (
"context"
"fmt"
"os"
"github.com/openconfig/gnmic/pkg/api"
)
const (
targetAddress = "172.20.20.2:6030" // Replace with your cEOS container IP and gNMI port
username = "admin"
password = "admin"
)
func main() {
target, err := api.NewTarget(
api.Address(targetAddress),
api.Username(username),
api.Password(password),
api.SkipVerify(false),
api.Insecure(true),
)
if err != nil {
fmt.Println(err, "failed to create gnmi client")
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create a gNMI client
err = target.CreateGNMIClient(ctx)
if err != nil {
fmt.Println(err, "failed to create gnmi client")
os.Exit(1)
}
defer target.Close()
// send the created gNMI GetRequest to the created target
// create a GetRequest
getReq, err := api.NewGetRequest(
api.Path("/interfaces/interface[name=Ethernet1]/state"),
api.Encoding("json_ietf"),
)
if err != nil {
fmt.Println(err, "failed to execute get request")
os.Exit(1)
}
// send the created gNMI GetRequest to the created target
getResp, err := target.Get(ctx, getReq)
if err != nil {
fmt.Println(err, "failed to execute get request")
os.Exit(1)
}
for _, notif := range getResp.Notification {
for _, update := range notif.Update {
fmt.Printf("Path: %s\n", update.Path)
fmt.Printf("Return value: %s\n", update.Val.GetJsonIetfVal())
}
}
}- Ensure your cEOS lab is deployed with
Ethernet1configured and up (e.g., from Day 7'sinitial_config.clab.yaml). - Modify
gnmi_get_interface.goto fetch the operational state forLoopback0. - Print the
Loopback0IP address and its operational status (up/down) from the gNMI response. You might need to adjust the gNMI path and how you extract the values based on the exact JSON structure returned by cEOS forLoopback0state.