-
-
Save anonymous/ea32f3df09bd7b5eb98d to your computer and use it in GitHub Desktop.
Example of using ssh-agent forwarding patch on go.crypto/ssh
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 "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