Skip to content

Commit 6a34bbc

Browse files
authored
Merge pull request #11 from AkihiroSuda/dev
support specifying port
2 parents 1a32387 + 1fb06e0 commit 6a34bbc

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

cmd/sshocker/run.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"net"
45
"os/user"
56
"path/filepath"
7+
"strconv"
68
"strings"
79

810
"github.com/AkihiroSuda/sshocker/pkg/mount"
@@ -43,6 +45,23 @@ var (
4345
}
4446
)
4547

48+
func parseHost(s string) (string, int, error) {
49+
if !strings.Contains(s, ":") {
50+
// FIXME: this check is not valid for IPv6!
51+
return s, 22, nil
52+
}
53+
host, portStr, err := net.SplitHostPort(s)
54+
if err != nil {
55+
return "", 0, err
56+
}
57+
port, err := strconv.Atoi(portStr)
58+
if err != nil {
59+
return "", 0, err
60+
}
61+
// host may contain "user@" prefix.
62+
return host, port, nil
63+
}
64+
4665
func runAction(clicontext *cli.Context) error {
4766
if clicontext.NArg() < 1 {
4867
return errors.New("no host specified")
@@ -51,9 +70,14 @@ func runAction(clicontext *cli.Context) error {
5170
ConfigFile: clicontext.String("ssh-config"),
5271
Persist: clicontext.Bool("ssh-persist"),
5372
}
73+
host, port, err := parseHost(clicontext.Args().First())
74+
if err != nil {
75+
return err
76+
}
5477
x := &sshocker.Sshocker{
5578
SSHConfig: sshConfig,
56-
Host: clicontext.Args().First(),
79+
Host: host,
80+
Port: port,
5781
Command: clicontext.Args().Tail(),
5882
}
5983
if len(x.Command) > 0 && x.Command[0] == "--" {

pkg/reversesshfs/reversesshfs.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/exec"
99
"path/filepath"
10+
"strconv"
1011

1112
"github.com/AkihiroSuda/sshocker/pkg/ssh"
1213
"github.com/AkihiroSuda/sshocker/pkg/util"
@@ -19,6 +20,7 @@ type ReverseSSHFS struct {
1920
*ssh.SSHConfig
2021
LocalPath string
2122
Host string
23+
Port int
2224
RemotePath string
2325
Readonly bool
2426
sshCmd *exec.Cmd
@@ -30,7 +32,7 @@ func (rsf *ReverseSSHFS) Prepare() error {
3032
if !filepath.IsAbs(rsf.RemotePath) {
3133
return errors.Errorf("unexpected relative path: %q", rsf.RemotePath)
3234
}
33-
sshArgs = append(sshArgs, rsf.Host, "--", "mkdir", "-p", rsf.RemotePath)
35+
sshArgs = append(sshArgs, "-p", strconv.Itoa(rsf.Port), rsf.Host, "--", "mkdir", "-p", rsf.RemotePath)
3436
sshCmd := exec.Command(sshBinary, sshArgs...)
3537
logrus.Debugf("executing ssh for preparing sshfs: %s %v", sshCmd.Path, sshCmd.Args)
3638
out, err := sshCmd.CombinedOutput()
@@ -49,7 +51,7 @@ func (rsf *ReverseSSHFS) Start() error {
4951
if !filepath.IsAbs(rsf.RemotePath) {
5052
return errors.Errorf("unexpected relative path: %q", rsf.RemotePath)
5153
}
52-
sshArgs = append(sshArgs, rsf.Host, "--", "sshfs", ":"+rsf.LocalPath, rsf.RemotePath, "-o", "slave")
54+
sshArgs = append(sshArgs, "-p", strconv.Itoa(rsf.Port), rsf.Host, "--", "sshfs", ":"+rsf.LocalPath, rsf.RemotePath, "-o", "slave")
5355
if rsf.Readonly {
5456
sshArgs = append(sshArgs, "-o", "ro")
5557
}
@@ -139,7 +141,7 @@ done
139141
}
140142
script := b.String()
141143
logrus.Debugf("generated script %q with map %v: %q", scriptName, m, script)
142-
stdout, stderr, err := ssh.ExecuteScript(rsf.Host, rsf.SSHConfig, script, scriptName)
144+
stdout, stderr, err := ssh.ExecuteScript(rsf.Host, rsf.Port, rsf.SSHConfig, script, scriptName)
143145
logrus.Debugf("executed script %q, stdout=%q, stderr=%q, err=%v", scriptName, stdout, stderr, err)
144146
return err
145147
}

pkg/ssh/ssh.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ func (c *SSHConfig) Args() []string {
3838
}
3939

4040
// ExitMaster executes `ssh -O exit`
41-
func ExitMaster(host string, c *SSHConfig) error {
41+
func ExitMaster(host string, port int, c *SSHConfig) error {
4242
if c == nil {
4343
return errors.New("got nil SSHConfig")
4444
}
4545
args := c.Args()
46-
args = append(args, "-O", "exit", host)
46+
args = append(args, "-O", "exit", "-p", strconv.Itoa(port), host)
4747
cmd := exec.Command(c.Binary(), args...)
4848
logrus.Debugf("executing ssh for exiting the master: %s %v", cmd.Path, cmd.Args)
4949
out, err := cmd.CombinedOutput()
5050
if err != nil {
51-
return errors.Wrapf(err, "failed to execute `%s -O exit %s`, out=%q", c.Binary(), host, string(out))
51+
return errors.Wrapf(err, "failed to execute `%s -O exit -p %d %s`, out=%q", c.Binary(), port, host, string(out))
5252
}
5353
return nil
5454
}
@@ -78,7 +78,7 @@ func parseScriptInterpreter(script string) (string, error) {
7878
// Returns stdout and stderr.
7979
//
8080
// scriptName is used only for readability of error strings.
81-
func ExecuteScript(host string, c *SSHConfig, script, scriptName string) (string, string, error) {
81+
func ExecuteScript(host string, port int, c *SSHConfig, script, scriptName string) (string, string, error) {
8282
if c == nil {
8383
return "", "", errors.New("got nil SSHConfig")
8484
}
@@ -88,7 +88,7 @@ func ExecuteScript(host string, c *SSHConfig, script, scriptName string) (string
8888
}
8989
sshBinary := c.Binary()
9090
sshArgs := c.Args()
91-
sshArgs = append(sshArgs, host, "--", interpreter)
91+
sshArgs = append(sshArgs, "-p", strconv.Itoa(port), host, "--", interpreter)
9292
sshCmd := exec.Command(sshBinary, sshArgs...)
9393
sshCmd.Stdin = strings.NewReader(script)
9494
var stderr bytes.Buffer

pkg/sshocker/sshocker.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sshocker
33
import (
44
"os"
55
"os/exec"
6+
"strconv"
67

78
"github.com/AkihiroSuda/sshocker/pkg/mount"
89
"github.com/AkihiroSuda/sshocker/pkg/reversesshfs"
@@ -14,6 +15,7 @@ import (
1415
type Sshocker struct {
1516
*ssh.SSHConfig
1617
Host string // Required
18+
Port int // Required
1719
Command []string // Optional
1820
Mounts []mount.Mount
1921
LForwards []string
@@ -28,7 +30,7 @@ func (x *Sshocker) Run() error {
2830
for _, l := range x.LForwards {
2931
args = append(args, "-L", l)
3032
}
31-
args = append(args, x.Host, "--")
33+
args = append(args, "-p", strconv.Itoa(x.Port), x.Host, "--")
3234
if len(x.Command) > 0 {
3335
args = append(args, x.Command...)
3436
}
@@ -44,6 +46,7 @@ func (x *Sshocker) Run() error {
4446
SSHConfig: x.SSHConfig,
4547
LocalPath: m.Source,
4648
Host: x.Host,
49+
Port: x.Port,
4750
RemotePath: m.Destination,
4851
Readonly: m.Readonly,
4952
}
@@ -66,7 +69,7 @@ func (x *Sshocker) Run() error {
6669
}
6770
defer func() {
6871
if x.SSHConfig.Persist {
69-
if emErr := ssh.ExitMaster(x.Host, x.SSHConfig); emErr != nil {
72+
if emErr := ssh.ExitMaster(x.Host, x.Port, x.SSHConfig); emErr != nil {
7073
logrus.WithError(emErr).Error("failed to exit the master")
7174
}
7275
}

0 commit comments

Comments
 (0)