Esempio n. 1
0
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
}
Esempio n. 2
0
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)
}
Esempio n. 3
0
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(AddedKey{PrivateKey: testPrivateKeys["rsa"], Comment: "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()
}