Golang SSH Client

on
//https://godoc.org/golang.org/x/crypto/ssh example
package main
import (
    "bytes"
    "fmt"
    "golang.org/x/crypto/ssh"
)

func main() {
    // An SSH client is represented with a ClientConn. Currently only
    // the "password" authentication method is supported.
    //
    // 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: "sshUser",
        Auth: []ssh.AuthMethod{
            ssh.Password("sshUserPassword"),
        },
    }
    client, err := ssh.Dial("tcp", "ip_addr:22", config)
    if err != nil {
        panic("Failed to dial: " + err.Error())
    }

    // Each ClientConn can support multiple interactive sessions,
    // represented by a Session.
    session, err := client.NewSession()
    if err != nil {
        panic("Failed to create session: " + err.Error())
    }
    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("/usr/bin/whoami"); err != nil {
    if err := session.Run("date"); err != nil {
        panic("Failed to run: " + err.Error())
    }
    fmt.Println(b.String())
}
Advertisements

Subjectivity aside, leave a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s