2021-10-24 21:15:38 +00:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2021-10-25 08:06:16 +00:00
|
|
|
"os/user"
|
2021-10-25 22:33:18 +00:00
|
|
|
"time"
|
2021-10-24 21:15:38 +00:00
|
|
|
|
2021-10-25 08:06:16 +00:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
2021-10-24 21:15:38 +00:00
|
|
|
"github.com/kevinburke/ssh_config"
|
2021-10-25 08:06:16 +00:00
|
|
|
"github.com/sfreiberg/simplessh"
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-10-24 21:15:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HostConfig is a SSH host config.
|
|
|
|
type HostConfig struct {
|
|
|
|
Host string
|
|
|
|
IdentityFile string
|
|
|
|
Port string
|
|
|
|
User string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHostConfig retrieves a ~/.ssh/config config for a host.
|
2021-10-25 09:13:41 +00:00
|
|
|
func GetHostConfig(hostname, username, port string) (HostConfig, error) {
|
2021-10-24 21:15:38 +00:00
|
|
|
var hostConfig HostConfig
|
|
|
|
|
2021-10-25 08:42:39 +00:00
|
|
|
var host, idf string
|
2021-10-24 21:15:38 +00:00
|
|
|
|
|
|
|
if host = ssh_config.Get(hostname, "Hostname"); host == "" {
|
2021-10-25 08:06:16 +00:00
|
|
|
logrus.Debugf("no hostname found in SSH config, assuming %s", hostname)
|
|
|
|
host = hostname
|
2021-10-24 21:15:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 08:42:39 +00:00
|
|
|
if username == "" {
|
|
|
|
if username = ssh_config.Get(hostname, "User"); username == "" {
|
|
|
|
systemUser, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return hostConfig, err
|
|
|
|
}
|
|
|
|
logrus.Debugf("no username found in SSH config or passed on command-line, assuming %s", username)
|
|
|
|
username = systemUser.Username
|
2021-10-25 08:06:16 +00:00
|
|
|
}
|
2021-10-24 21:15:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 08:42:39 +00:00
|
|
|
if port == "" {
|
|
|
|
if port = ssh_config.Get(hostname, "Port"); port == "" {
|
|
|
|
logrus.Debugf("no port found in SSH config or passed on command-line, assuming 22")
|
|
|
|
port = "22"
|
|
|
|
}
|
2021-10-24 21:15:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 09:13:41 +00:00
|
|
|
idf = ssh_config.Get(hostname, "IdentityFile")
|
2021-10-24 21:15:38 +00:00
|
|
|
|
|
|
|
hostConfig.Host = host
|
2021-10-25 09:13:41 +00:00
|
|
|
if idf != "" {
|
|
|
|
hostConfig.IdentityFile = idf
|
|
|
|
}
|
2021-10-24 21:15:38 +00:00
|
|
|
hostConfig.Port = port
|
2021-10-25 08:42:39 +00:00
|
|
|
hostConfig.User = username
|
2021-10-25 08:06:16 +00:00
|
|
|
|
|
|
|
logrus.Debugf("constructed SSH config %s for %s", hostConfig, hostname)
|
2021-10-24 21:15:38 +00:00
|
|
|
|
|
|
|
return hostConfig, nil
|
|
|
|
}
|
2021-10-25 08:06:16 +00:00
|
|
|
|
|
|
|
// New creates a new SSH client connection.
|
2021-10-25 08:42:39 +00:00
|
|
|
func New(domainName, sshAuth, username, port string) (*simplessh.Client, error) {
|
2021-10-25 08:06:16 +00:00
|
|
|
var client *simplessh.Client
|
|
|
|
|
2021-10-25 09:13:41 +00:00
|
|
|
hostConfig, err := GetHostConfig(domainName, username, port)
|
2021-10-25 08:06:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return client, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sshAuth == "identity-file" {
|
|
|
|
var err error
|
2021-10-25 22:33:18 +00:00
|
|
|
client, err = simplessh.ConnectWithAgentTimeout(hostConfig.Host, hostConfig.User, 5*time.Second)
|
2021-10-25 08:06:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return client, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
password := ""
|
|
|
|
prompt := &survey.Password{
|
|
|
|
Message: "SSH password?",
|
|
|
|
}
|
|
|
|
if err := survey.AskOne(prompt, &password); err != nil {
|
|
|
|
return client, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2021-10-25 22:33:18 +00:00
|
|
|
client, err = simplessh.ConnectWithPasswordTimeout(hostConfig.Host, hostConfig.User, password, 5*time.Second)
|
2021-10-25 08:06:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return client, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|