Skip to content

Instantly share code, notes, and snippets.

@lukehinds
Created January 25, 2017 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukehinds/abb1a2496a808098468c7a2f8e692cdd to your computer and use it in GitHub Desktop.
Save lukehinds/abb1a2496a808098468c7a2f8e692cdd to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"golang.org/x/crypto/ssh"
"log"
"os"
)
func main() {
cmd := os.Args[1]
host := os.Args[2:]
conn_string := fmt.Sprintf("%s:22", host)
// 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: "username",
Auth: []ssh.AuthMethod{
ssh.Password("password"),
},
}
client, err := ssh.Dial("tcp", conn_string, 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)
}
defer session.Close()
// 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(cmd); err != nil {
log.Fatal("Failed to run: " + err.Error())
}
fmt.Println(b.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment