Skip to content

Commit 952d4b9

Browse files
committed
Support of '--gateway=login@host' (Fix #110)
1 parent ab01d9b commit 952d4b9

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
10411041

10421042
#### Features
10431043

1044+
* Support of `--gateway=login@host` ([#110](https://github.com/scaleway/scaleway-cli/issues/110))
10441045
* Upload local ssh key to scaleway account on `scw login` ([#100](https://github.com/scaleway/scaleway-cli/issues/100))
10451046
* Add a 'running indicator' for `scw run`, can be disabled with the new flag `--quiet`
10461047
* Support of `scw -V/--verbose` option ([#83](https://github.com/scaleway/scaleway-cli/issues/83))

pkg/commands/cp.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TarFromSource(ctx CommandContext, source string, gateway string) (*io.ReadC
9090
}
9191

9292
// execCmd contains the ssh connection + the remoteCommand
93-
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, nil, remoteCommand, gateway))
93+
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, nil, remoteCommand, gateway, "root"))
9494
logrus.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
9595
spawnSrc := exec.Command("ssh", execCmd...)
9696

@@ -188,7 +188,7 @@ func UntarToDest(ctx CommandContext, sourceStream *io.ReadCloser, destination st
188188
}
189189

190190
// execCmd contains the ssh connection + the remoteCommand
191-
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, nil, remoteCommand, gateway))
191+
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, false, nil, remoteCommand, gateway, "root"))
192192
logrus.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
193193
spawnDst := exec.Command("ssh", execCmd...)
194194

pkg/commands/kill.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func RunKill(ctx CommandContext, args KillArgs) error {
4343
}
4444
}
4545

46-
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, gateway))
46+
execCmd := append(utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, gateway, "root"))
4747

4848
logrus.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
4949

pkg/commands/top.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func RunTop(ctx CommandContext, args TopArgs) error {
4343
}
4444
}
4545

46-
execCmd := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, gateway)
46+
execCmd := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, nil, []string{command}, gateway, "root")
4747
logrus.Debugf("Executing: ssh %s", strings.Join(execCmd, " "))
4848
out, err := exec.Command("ssh", execCmd...).CombinedOutput()
4949
fmt.Printf("%s", out)

pkg/utils/utils.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ func quoteShellArgs(args []string) string {
3535

3636
// SSHExec executes a command over SSH and redirects file-descriptors
3737
func SSHExec(publicIPAddress string, privateIPAddress string, command []string, checkConnection bool, gatewayIPAddress string) error {
38+
gatewayUser := "root"
39+
if strings.Contains(gatewayIPAddress, "@") {
40+
parts := strings.Split(gatewayIPAddress, "@")
41+
gatewayUser = parts[0]
42+
gatewayIPAddress = parts[1]
43+
}
44+
3845
if publicIPAddress == "" && gatewayIPAddress == "" {
3946
return errors.New("server does not have public IP")
4047
}
@@ -52,7 +59,7 @@ func SSHExec(publicIPAddress string, privateIPAddress string, command []string,
5259
}
5360
}
5461

55-
execCmd := append(NewSSHExecCmd(publicIPAddress, privateIPAddress, true, nil, command, gatewayIPAddress))
62+
execCmd := append(NewSSHExecCmd(publicIPAddress, privateIPAddress, true, nil, command, gatewayUser+"@"+gatewayIPAddress, "root"))
5663

5764
log.Debugf("Executing: ssh %s", quoteShellArgs(execCmd))
5865

@@ -64,8 +71,15 @@ func SSHExec(publicIPAddress string, privateIPAddress string, command []string,
6471
}
6572

6673
// NewSSHExecCmd computes execve compatible arguments to run a command via ssh
67-
func NewSSHExecCmd(publicIPAddress string, privateIPAddress string, allocateTTY bool, sshOptions []string, command []string, gatewayIPAddress string) []string {
74+
func NewSSHExecCmd(publicIPAddress string, privateIPAddress string, allocateTTY bool, sshOptions []string, command []string, gatewayIPAddress string, sshUser string) []string {
6875
useGateway := gatewayIPAddress != ""
76+
gatewayUser := "root"
77+
if useGateway && strings.Contains(gatewayIPAddress, "@") {
78+
parts := strings.Split(gatewayIPAddress, "@")
79+
gatewayUser = parts[0]
80+
gatewayIPAddress = parts[1]
81+
}
82+
6983
execCmd := []string{}
7084

7185
if os.Getenv("DEBUG") != "1" {
@@ -80,9 +94,9 @@ func NewSSHExecCmd(publicIPAddress string, privateIPAddress string, allocateTTY
8094
execCmd = append(execCmd, strings.Join(sshOptions, " "))
8195
}
8296

83-
execCmd = append(execCmd, "-l", "root")
97+
execCmd = append(execCmd, "-l", sshUser)
8498
if useGateway {
85-
proxyCommand := NewSSHExecCmd(gatewayIPAddress, "", allocateTTY, []string{"-W", "%h:%p"}, nil, "")
99+
proxyCommand := NewSSHExecCmd(gatewayIPAddress, "", allocateTTY, []string{"-W", "%h:%p"}, nil, "", gatewayUser)
86100
execCmd = append(execCmd, privateIPAddress, "-o", "ProxyCommand=ssh "+strings.Join(proxyCommand, " "))
87101
} else {
88102
execCmd = append(execCmd, publicIPAddress)

0 commit comments

Comments
 (0)