forked from jackc/tern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_tunnel.go
More file actions
36 lines (29 loc) · 743 Bytes
/
ssh_tunnel.go
File metadata and controls
36 lines (29 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"net"
"os"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
type SSHConnConfig struct {
Host string
Port string
User string
Password string
}
func NewSSHClient(config *SSHConnConfig) (*ssh.Client, error) {
sshConfig := &ssh.ClientConfig{
User: config.User,
Auth: []ssh.AuthMethod{SSHAgent()},
}
if config.Password != "" {
sshConfig.Auth = append(sshConfig.Auth, ssh.Password(config.Password))
}
return ssh.Dial("tcp", net.JoinHostPort(config.Host, config.Port), sshConfig)
}
func SSHAgent() ssh.AuthMethod {
if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
}
return nil
}