Created
May 2, 2014 18:48
-
-
Save jbardin/5bc0407d1f70dfc60269 to your computer and use it in GitHub Desktop.
ssh agent auth
This file contains hidden or 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 ( | |
"log" | |
"net" | |
"os" | |
"os/user" | |
"code.google.com/p/go.crypto/ssh" | |
"code.google.com/p/go.crypto/ssh/agent" | |
) | |
func main() { | |
sock, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
agent := agent.NewClient(sock) | |
signers, err := agent.Signers() | |
if err != nil { | |
log.Fatal(err) | |
} | |
auths := []ssh.AuthMethod{ssh.PublicKeys(signers...)} | |
u, err := user.Current() | |
if err != nil { | |
log.Fatal(err) | |
} | |
cfg := &ssh.ClientConfig{ | |
User: u.Username, | |
Auth: auths, | |
} | |
cfg.SetDefaults() | |
client, err := ssh.Dial("tcp", "localhost:22", cfg) | |
if err != nil { | |
log.Fatal(err) | |
} | |
_, err = client.NewSession() | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Println("We've got a live session!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment