func (c *Client) Connect() (e error) { if c.Port == 0 { c.Port = 22 } var auths []ssh.AuthMethod if c.password != "" { auths = append(auths, ssh.Password(c.password)) } else if c.Agent, e = net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); e == nil { auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(c.Agent).Signers)) } config := &ssh.ClientConfig{ User: c.User, Auth: auths, } c.Conn, e = ssh.Dial("tcp", fmt.Sprintf("%s:%d", c.Host, c.Port), config) return e }
func (c *Config) Connection() (*ssh.Client, error) { port := c.Port if port == 0 { port = 22 } var auths []ssh.AuthMethod if c.Password != "" { auths = append(auths, ssh.Password(c.Password)) } else if sshAgent, e := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); e == nil { auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)) } config := &ssh.ClientConfig{ User: c.User, Auth: auths, } return ssh.Dial("tcp", fmt.Sprintf("%s:%d", c.Host, port), config) }
func TestAuth(t *testing.T) { a, b, err := netPipe() if err != nil { t.Fatalf("netPipe: %v", err) } defer a.Close() defer b.Close() agent, _, cleanup := startAgent(t) defer cleanup() if err := agent.Add(testPrivateKeys["rsa"], nil, "comment"); err != nil { t.Errorf("Add: %v", err) } serverConf := ssh.ServerConfig{} serverConf.AddHostKey(testSigners["rsa"]) serverConf.PublicKeyCallback = func(c ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { if bytes.Equal(key.Marshal(), testPublicKeys["rsa"].Marshal()) { return nil, nil } return nil, errors.New("pubkey rejected") } go func() { conn, _, _, err := ssh.NewServerConn(a, &serverConf) if err != nil { t.Fatalf("Server: %v", err) } conn.Close() }() conf := ssh.ClientConfig{} conf.Auth = append(conf.Auth, ssh.PublicKeysCallback(agent.Signers)) conn, _, _, err := ssh.NewClientConn(b, "", &conf) if err != nil { t.Fatalf("NewClientConn: %v", err) } conn.Close() }
func (c *goCryptoCommand) ensureSession() (*ssh.Session, error) { if c.sess != nil { return c.sess, nil } if len(c.signers) == 0 { return nil, fmt.Errorf("no private keys available") } if c.user == "" { currentUser, err := user.Current() if err != nil { return nil, fmt.Errorf("getting current user: %v", err) } c.user = currentUser.Username } config := &ssh.ClientConfig{ User: c.user, Auth: []ssh.AuthMethod{ ssh.PublicKeysCallback(func() ([]ssh.Signer, error) { return c.signers, nil }), }, } client, err := sshDialWithProxy(c.addr, c.proxyCommand, config) if err != nil { return nil, err } sess, err := client.NewSession() if err != nil { client.Close() return nil, err } c.client = client c.sess = sess c.sess.Stdin = c.stdin c.sess.Stdout = c.stdout c.sess.Stderr = c.stderr return sess, nil }
func main() { var auths []ssh.AuthMethod if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil { auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers)) } if *PASS != "" { auths = append(auths, ssh.Password(*PASS)) } config := ssh.ClientConfig{ User: *USER, Auth: auths, } addr := fmt.Sprintf("%s:%d", *HOST, *PORT) conn, err := ssh.Dial("tcp", addr, &config) if err != nil { log.Fatalf("unable to connect to [%s]: %v", addr, err) } defer conn.Close() client, err := sftp.NewClient(conn) if err != nil { log.Fatalf("unable to start sftp subsytem: %v", err) } defer client.Close() switch cmd := flag.Args()[0]; cmd { case "ls": if len(flag.Args()) < 2 { log.Fatalf("%s %s: remote path required", cmd, os.Args[0]) } walker := client.Walk(flag.Args()[1]) for walker.Step() { if err := walker.Err(); err != nil { log.Println(err) continue } fmt.Println(walker.Path()) } case "fetch": if len(flag.Args()) < 2 { log.Fatalf("%s %s: remote path required", cmd, os.Args[0]) } f, err := client.Open(flag.Args()[1]) if err != nil { log.Fatal(err) } defer f.Close() if _, err := io.Copy(os.Stdout, f); err != nil { log.Fatal(err) } case "put": if len(flag.Args()) < 2 { log.Fatalf("%s %s: remote path required", cmd, os.Args[0]) } f, err := client.Create(flag.Args()[1]) if err != nil { log.Fatal(err) } defer f.Close() if _, err := io.Copy(f, os.Stdin); err != nil { log.Fatal(err) } case "stat": if len(flag.Args()) < 2 { log.Fatalf("%s %s: remote path required", cmd, os.Args[0]) } f, err := client.Open(flag.Args()[1]) if err != nil { log.Fatal(err) } defer f.Close() fi, err := f.Stat() if err != nil { log.Fatalf("unable to stat file: %v", err) } fmt.Printf("%s %d %v\n", fi.Name(), fi.Size(), fi.Mode()) case "rm": if len(flag.Args()) < 2 { log.Fatalf("%s %s: remote path required", cmd, os.Args[0]) } if err := client.Remove(flag.Args()[1]); err != nil { log.Fatalf("unable to remove file: %v", err) } case "mv": if len(flag.Args()) < 3 { log.Fatalf("%s %s: old and new name required", cmd, os.Args[0]) } if err := client.Rename(flag.Args()[1], flag.Args()[2]); err != nil { log.Fatalf("unable to rename file: %v", err) } default: log.Fatal("unknown subcommand: %v", cmd) } }