Skip to content

Latest commit

 

History

History
63 lines (53 loc) · 1.64 KB

File metadata and controls

63 lines (53 loc) · 1.64 KB

Module 2 Extra 1: SSH/CLI Automation with Go

Concept:

Using Go to establish SSH connections to network devices and execute CLI commands. Introduction to SSH libraries like golang.org/x/crypto/ssh.

Challenge:

Write a Go program that connects to the SR Linux node (from Day 6's Containerlab setup) via SSH, executes show system information, and prints the output.

Code Example:

// main.go
// main.go
package main

import (
	"fmt"
	"log"
	"time"

	"golang.org/x/crypto/ssh"
)

func main() {
	// SSH client config
	config := &ssh.ClientConfig{
		User: "admin",
		Auth: []ssh.AuthMethod{
			ssh.Password("NokiaSrl1!"), // Default password for SR Linux
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(), // NOT for production!
		Timeout:         5 * time.Second,
	}

	// Connect to the SSH server (replace with your SR Linux management IP/hostname)
	// For Containerlab, typically the node name resolves to its IP in the docker network
	conn, err := ssh.Dial("tcp", "clab-srl-single-srl1:22", config)
	if err != nil {
		log.Fatalf("Failed to dial: %v", err)
	}
	defer conn.Close()

	// Create a new session
	session, err := conn.NewSession()
	if err != nil {
		log.Fatalf("Failed to create session: %v", err)
	}
	defer session.Close()

	// Run the command
	output, err := session.CombinedOutput("show system network-instance")
	if err != nil {
		log.Fatalf("Failed to run command: %v", err)
	}

	fmt.Println(string(output))
}

Instructions:

  1. Ensure your srl_single.clab.yaml from Day 6 is deployed.
  2. Run go mod init day7.
  3. Run go get golang.org/x/crypto/ssh.
  4. Save the code as main.go.
  5. Run go run main.go.