Skip to content

Instantly share code, notes, and snippets.

@philips
Last active May 24, 2016 11:29
Show Gist options
  • Save philips/8324564 to your computer and use it in GitHub Desktop.
Save philips/8324564 to your computer and use it in GitHub Desktop.
I love go. SSH client using the agent in under 50 lines of code.
// example: go run ssh.go core myhost:22 "ps aux | head -n 5"
package main
import (
"log"
"net"
"os"
"code.google.com/p/go.crypto/ssh"
)
func main() {
if len(os.Args) != 4 {
log.Fatalln("usage: go-ssh <username> <host>:<port> <cmd>")
}
agent, e := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if e != nil {
panic(e)
}
defer agent.Close()
auths := []ssh.ClientAuth{
ssh.ClientAuthAgent(ssh.NewAgentClient(agent)),
}
clientConfig := &ssh.ClientConfig{
User: os.Args[1],
Auth: auths,
}
client, err := ssh.Dial("tcp", os.Args[2], clientConfig)
if err != nil {
panic("Failed to dial: " + err.Error())
}
session, err := client.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
}
defer session.Close()
session.Stdout = os.Stdout
session.Stderr = os.Stderr
if err := session.Run(os.Args[3]); err != nil {
panic("Failed to run: " + err.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment