/
ssh.go
91 lines (67 loc) · 2.37 KB
/
ssh.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package commands
import (
"fmt"
"github.com/ibejohn818/awssh/api"
"github.com/ibejohn818/awssh/config"
"github.com/ibejohn818/awssh/shell"
"github.com/spf13/cobra"
)
func AddSshCmd(cmd *cobra.Command, conf *config.AwsshConf) {
sshOpts := &shell.SSHOpts{}
useBastion := false
sshCmd := &cobra.Command{
Use: "ssh",
Short: "SSH into an instance",
Run: func(cmd *cobra.Command, args []string) {
var instance api.Ec2Instance
var bastion api.Ec2Instance
showPrivate := false
ec2Coll := api.GetServers(conf)
ec2Res := ec2Coll.Filtered(args)
agent, err := shell.AgentAuth()
if err != nil {
fmt.Println("SSH Agent not detected/started in env SSH_AUTH_SOCK")
sshOpts.IdentityFile = sshOpts.DefaultIdentityFile()
} else {
sshOpts.AuthMethods = append(sshOpts.AuthMethods, agent)
}
if len(sshOpts.IdentityFile) > 0 {
sshKey, err := shell.NewSSHKey(sshOpts.IdentityFile)
if err != nil {
panic(err)
}
key, err := shell.SSHKeyAuth(sshKey)
if err != nil {
panic(err)
}
sshOpts.AuthMethods = append(sshOpts.AuthMethods, key)
}
if useBastion {
bastion = selectInstance(ec2Res, "Select a bastion host", false)
}
if useBastion || sshOpts.UsePrivate {
showPrivate = true
}
instance = selectInstance(ec2Res, "Select an instance", showPrivate)
sshClient := shell.NewSSHClient(instance, sshOpts)
if len(bastion.Ip) > 0 {
sshBastionClient := shell.NewSSHClient(bastion, sshOpts)
shell.SSHBastionLogin(sshBastionClient, sshClient)
} else {
shell.SSHLogin(sshClient)
}
},
}
flags := sshCmd.Flags()
flags.StringVarP(&sshOpts.User, "username", "u", "ec2-user", "SSH Username")
conf.VpConf.BindPFlag("username", flags.Lookup("username"))
// flags.StringVar(&sshSess.Command, "cmd", "", "Send command to the server")
flags.BoolVarP(&sshOpts.Tty, "tty", "t", false, "TTY Terminal")
flags.BoolVarP(&sshOpts.ForwardAuthSock, "auth", "A", false, "Pass SSH AUTH SOCK")
flags.BoolVarP(&useBastion, "bastion", "b", false, "Connect via bastion host")
flags.StringVarP(&sshOpts.IdentityFile, "identity", "i", "", "Path to ssh key file")
flags.StringVarP(&sshOpts.Command, "cmd", "c", "", "Send command")
flags.StringVarP(&sshOpts.Port, "port", "p", "22", "SSH Port")
flags.BoolVar(&sshOpts.UsePrivate, "private", false, "Connect using instance private ip")
cmd.AddCommand(sshCmd)
}