Skip to content

Instantly share code, notes, and snippets.

Created Aug 22, 2013
Embed
What would you like to do?
Example of using ssh-agent forwarding patch on go.crypto/ssh
package main
import "errors"
import "fmt"
import "io"
import "net"
import "os"
import "code.google.com/p/go.crypto/ssh"
func connectToAgent() (io.ReadWriteCloser, error) {
socketLocation := os.Getenv("SSH_AUTH_SOCK")
if socketLocation == "" {
return nil, errors.New("SSH_AUTH_SOCK not set")
}
// handle XXX no SSH AUTH SOCK
c, err := net.Dial("unix", socketLocation)
if err != nil {
return nil, err
}
return c, nil
}
func connectToSsh() net.Conn {
c, err := net.Dial("tcp", "bastion:22")
if err != nil {
panic(err)
}
return c
}
type Connector struct {
}
func (c *Connector) Connect() (io.ReadWriteCloser, error) {
return connectToAgent()
}
func main() {
c, err := connectToAgent()
if err != nil {
panic(err)
}
agent := ssh.NewAgentClient(c)
config := &ssh.ClientConfig{
User: "jamwt",
Auth: []ssh.ClientAuth{
ssh.ClientAuthAgent(agent),
},
// Comment out to disable key forwarding
AgentConnector: &Connector{},
}
c2 := connectToSsh()
client, err := ssh.Client(c2, config)
if err != nil {
panic(err)
}
ses, err := client.NewSession()
if err != nil {
panic(err)
}
res, err := ses.CombinedOutput("ssh -o PasswordAuthentication=no next-server ls")
if err != nil {
}
fmt.Printf("Remote Said:\n%s", res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment