Forum Discussion

acorreia's avatar
acorreia
Icon for Nimbostratus rankNimbostratus
Nov 03, 2021

Golang SSH script for F5?

I am testing a small program written in go to SSH into an F5 do some work but still getting a failure to connect . Has anyone else ran into a similar issue? Code is below:

 

package main

 

import (

"bytes"

"fmt"

"golang.org/x/crypto/ssh"

"log"

)

 

 

func main() {

devices := make([]string, 0)

 

devices = append(devices,

"xxxxxx:22")

 

// An SSH client is represented with a ClientConn.

//

// To authenticate with the remote server you must pass at least one

// implementation of AuthMethod via the Auth field in ClientConfig.

 

config := &ssh.ClientConfig{

User: "xxxxxxx",

Auth: []ssh.AuthMethod{

ssh.Password("xxxxxxxx"),

},

HostKeyCallback: ssh.InsecureIgnoreHostKey(),

}

 

 

for _, d := range devices {

client, err := ssh.Dial("tcp", d, config)

if err != nil {

log.Fatal("Failed to dial: ", err)

}

 

// Each ClientConn can support multiple interactive sessions,

// represented by a Session.

session, err := client.NewSession()

if err != nil {

log.Fatal("Failed to create session: ", err)

}

 

// Once a Session is created, you can execute a single command on

// the remote side using the Run method.

var b bytes.Buffer

session.Stdout = &b

 

if err := session.Run("ls -l"); err != nil {

log.Fatal("Failed to run: " + err.Error())

}

fmt.Println(b.String())

 

err = session.Close()

if err != nil {

fmt.Printf("Failed to close session for %v\n", d)

}

}

}

 

 

  • Hi,

    Using inspiration from the golang tmsh library GitHub - yukirii/go-tmsh: Golang wrapper library & CLI tool for BIG-IP Traffic Management Shell (TMSH). You just need to handle the keyboard interactions. 

    By creating challenge response struct and function. 

     

    type keyboardInteractive map[string]string
    
    func (ki keyboardInteractive) Challenge(user, instruction string, questions []string, echos []bool) ([]string, error) {
    	var answers []string
    
    	for _, q := range questions {
    		answers = append(answers, ki[q])
    	}
    
    	return answers, nil
    }
    
    answers := keyboardInteractive(map[string]string{
    		"Password: ": "*****",
    	})

     

    Then your config will look like so now

     

    config := &ssh.ClientConfig{
    		User: "******",
    		Auth: []ssh.AuthMethod{
    			ssh.Password("*******"),
    			ssh.KeyboardInteractive(
    				answers.Challenge,
    			),
    		},
    		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    	}​