-
Notifications
You must be signed in to change notification settings - Fork 20
/
ssh.go
176 lines (141 loc) · 3.42 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package cmd
import (
"fmt"
"net"
"os"
"os/exec"
"strings"
"github.com/exoscale/egoscale"
"github.com/kballard/go-shellquote"
"github.com/spf13/cobra"
)
var sshCmd = &cobra.Command{
Use: "ssh INSTANCE-NAME|ID",
Short: "SSH into a Compute instance",
ValidArgsFunction: completeVMNames,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
ipv6, err := cmd.Flags().GetBool("ipv6")
if err != nil {
return err
}
printInfo, err := cmd.Flags().GetBool("info")
if err != nil {
return err
}
printCmd, err := cmd.Flags().GetBool("print")
if err != nil {
return err
}
sshInfo, err := getSSHInfo(args[0], ipv6)
if err != nil {
return err
}
sshOpts, err := cmd.Flags().GetString("ssh-options")
if err != nil {
return err
}
sshInfo.opts = sshOpts
if printInfo {
printSSHInfo(sshInfo)
return nil
}
sshCmd := buildSSHCommand(sshInfo)
if printCmd {
fmt.Println(strings.Join(sshCmd, " "))
return nil
}
return connectSSH(sshCmd[1:])
},
}
type sshInfo struct {
sshKeys string
username string
opts string
ip net.IP
vmName string
vmID *egoscale.UUID
}
func getSSHInfo(name string, isIpv6 bool) (*sshInfo, error) {
var info sshInfo
vm, err := getVirtualMachineByNameOrID(name)
if err != nil {
return nil, err
}
info.vmID = vm.ID
info.vmName = vm.Name
info.sshKeys = getKeyPairPath(vm.ID.String())
nic := vm.DefaultNic()
if nic == nil {
return nil, fmt.Errorf("this instance %q has no default NIC", vm.ID)
}
info.ip = *vm.IP()
if isIpv6 {
if nic.IP6Address == nil {
return nil, fmt.Errorf("missing IPv6 address on the instance %q", vm.ID)
}
info.ip = nic.IP6Address
}
if info.ip == nil {
return nil, fmt.Errorf("no valid IP address found")
}
query := &egoscale.Template{
ID: vm.TemplateID,
IsFeatured: true,
ZoneID: vm.ZoneID,
}
resp, err := cs.GetWithContext(gContext, query)
if err != nil {
return nil, fmt.Errorf("unable to retrieve Compute instance template: %v", err)
}
template := resp.(*egoscale.Template)
username, ok := template.Details["username"]
if ok {
info.username = username
}
return &info, nil
}
func buildSSHCommand(info *sshInfo) []string {
cmd := []string{"ssh"}
if _, err := os.Stat(info.sshKeys); err == nil {
cmd = append(cmd, "-i", info.sshKeys)
}
if info.opts != "" {
opts, err := shellquote.Split(info.opts)
if err == nil {
cmd = append(cmd, opts...)
}
}
if info.username != "" {
cmd = append(cmd, "-l", info.username)
}
cmd = append(cmd, info.ip.String())
return cmd
}
func printSSHInfo(info *sshInfo) {
fmt.Println("Host", info.vmName)
fmt.Println("\tHostName", info.ip.String())
if info.username != "" {
fmt.Println("\tUser", info.username)
}
if _, err := os.Stat(info.sshKeys); err == nil {
fmt.Println("\tIdentityFile", info.sshKeys)
}
}
func connectSSH(args []string) error {
cmd := exec.Command("ssh", args...)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}
func init() {
sshCmd.Flags().BoolP("info", "i", false, "Print SSH config information")
sshCmd.Flags().StringP("ssh-options", "o", "",
"Additional options to pass to the `ssh` command (e.g. -o \"-l my-user -p 2222\"`)")
sshCmd.Flags().BoolP("print", "p", false, "Print SSH command")
sshCmd.Flags().BoolP("ipv6", "6", false, "Use IPv6 for SSH")
RootCmd.AddCommand(sshCmd)
}