-
Notifications
You must be signed in to change notification settings - Fork 20
/
ssh.go
161 lines (129 loc) · 3.16 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
package cmd
import (
"fmt"
"log"
"net"
"os"
"os/exec"
"path"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// sshCmd represents the ssh command
var sshCmd = &cobra.Command{
Use: "ssh <vm name | id>",
Short: "SSH into a virtual machine instance",
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
}
isInfo, err := cmd.Flags().GetBool("info")
if err != nil {
return err
}
isConnectionSTR, err := cmd.Flags().GetBool("print")
if err != nil {
return err
}
info, err := getSSHInfo(args[0], ipv6)
if err != nil {
return err
}
if isConnectionSTR {
printSSHConnectSTR(info)
return nil
}
if isInfo {
printSSHInfo(info)
return nil
}
return connectSSH(info)
},
}
type sshInfo struct {
sshKeys string
userName string
ip net.IP
vmName string
vmID *egoscale.UUID
}
func getSSHInfo(name string, isIpv6 bool) (*sshInfo, error) {
vm, err := getVMWithNameOrID(name)
if err != nil {
return nil, err
}
sshKeyPath := path.Join(gConfigFolder, "instances", vm.ID.String(), "id_rsa")
nic := vm.DefaultNic()
if nic == nil {
return nil, fmt.Errorf("this instance %q has no default NIC", vm.ID)
}
vmIP := vm.IP()
if isIpv6 {
if nic.IP6Address == nil {
return nil, fmt.Errorf("missing IPv6 address on the instance %q", vm.ID)
}
vmIP = &nic.IP6Address
}
if vmIP == nil {
return nil, fmt.Errorf("no valid IP address found")
}
template := &egoscale.Template{
ID: vm.TemplateID,
IsFeatured: true,
ZoneID: vm.ZoneID,
}
if err := cs.GetWithContext(gContext, template); err != nil {
return nil, err
}
tempUser, ok := template.Details["username"]
if !ok {
return nil, fmt.Errorf("missing username information in Template %q", template.ID)
}
return &sshInfo{
sshKeys: sshKeyPath,
userName: tempUser,
ip: *vmIP,
vmName: vm.Name,
vmID: vm.ID,
}, nil
}
func printSSHConnectSTR(info *sshInfo) {
sshArgs := ""
if _, err := os.Stat(info.sshKeys); err == nil {
sshArgs = fmt.Sprintf("-i %q ", info.sshKeys)
}
fmt.Printf("ssh %s%s@%s\n", sshArgs, info.userName, info.ip)
}
func printSSHInfo(info *sshInfo) {
fmt.Println("Host", info.vmName)
fmt.Println("\tHostName", info.ip.String())
fmt.Println("\tUser", info.userName)
if _, err := os.Stat(info.sshKeys); err == nil {
fmt.Println("\tIdentityFile", info.sshKeys)
}
}
func connectSSH(info *sshInfo) error {
args := make([]string, 0, 3)
if _, err := os.Stat(info.sshKeys); os.IsNotExist(err) {
log.Printf("Warning: Identity file %s not found or not accessible.", info.sshKeys)
} else {
args = append(args, "-i")
args = append(args, info.sshKeys)
}
args = append(args, info.userName+"@"+info.ip.String())
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().BoolP("print", "p", false, "Print SSH command")
sshCmd.Flags().BoolP("ipv6", "6", false, "Use IPv6 for SSH")
RootCmd.AddCommand(sshCmd)
}