Created
January 25, 2017 22:46
-
-
Save lukehinds/abb1a2496a808098468c7a2f8e692cdd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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