I need to run multiple commands in a single ssh session:
// Define the client configuration
config := &ssh.ClientConfig{
User: USERNAME,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(pem),
},
}
// Connect to the machine
client, err := ssh.Dial("tcp", HOSTNAME + ":" + PORT, config)
if err != nil {
panic("Failed to dial: " + err.Error())
}
// Create a session
session, err := client.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
}
defer session.Close()
// Start running commands!
var output bytes.Buffer
session.Stdout = &output
// 1) Login to swarm registry
fmt.Println("Logging into swarm registry...")
if err := session.Run("docker login ..."); err != nil {
panic("Failed to login to swarm registry: " + err.Error())
}
fmt.Println(output.String())
// 2) List all of the docker processes
fmt.Println("List swarm processes...")
if err := session.Run("docker ps"); err != nil { // <-------- FAILS HERE
panic("Failed to list swarm processes: " + err.Error())
}
fmt.Println(output.String())
I read through the source file (session.go
) and for the Session.Run
command and it says:
A Session only accepts one call to Run, Start, Shell, Output, or CombinedOutput.
For my use case I need to issue the first command to log the session in, and then issue subsequent commands once I am logged in.
Is there an alternate way to run multiple commands using the same ssh session?