Skip to content

Commit 92d2ff3

Browse files
committed
Stop hardcoding the default port for ssh
In case the Port is given in the ssh config already... The ssh command will know the default port, otherwise. Signed-off-by: Anders F Björklund <[email protected]>
1 parent 611af10 commit 92d2ff3

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

cmd/sshocker/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var (
5353
func parseHost(s string) (string, int, error) {
5454
if !strings.Contains(s, ":") {
5555
// FIXME: this check is not valid for IPv6!
56-
return s, 22, nil
56+
return s, 0, nil
5757
}
5858
host, portStr, err := net.SplitHostPort(s)
5959
if err != nil {

pkg/reversesshfs/reversesshfs.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ func (rsf *ReverseSSHFS) Prepare() error {
3333
if !filepath.IsAbs(rsf.RemotePath) {
3434
return errors.Errorf("unexpected relative path: %q", rsf.RemotePath)
3535
}
36-
sshArgs = append(sshArgs, "-p", strconv.Itoa(rsf.Port), rsf.Host, "--", "mkdir", "-p", rsf.RemotePath)
36+
if rsf.Port != 0 {
37+
sshArgs = append(sshArgs, "-p", strconv.Itoa(rsf.Port))
38+
}
39+
sshArgs = append(sshArgs, rsf.Host, "--")
40+
sshArgs = append(sshArgs, "mkdir", "-p", rsf.RemotePath)
3741
sshCmd := exec.Command(sshBinary, sshArgs...)
3842
logrus.Debugf("executing ssh for preparing sshfs: %s %v", sshCmd.Path, sshCmd.Args)
3943
out, err := sshCmd.CombinedOutput()
@@ -52,7 +56,11 @@ func (rsf *ReverseSSHFS) Start() error {
5256
if !filepath.IsAbs(rsf.RemotePath) {
5357
return errors.Errorf("unexpected relative path: %q", rsf.RemotePath)
5458
}
55-
sshArgs = append(sshArgs, "-p", strconv.Itoa(rsf.Port), rsf.Host, "--", "sshfs", ":"+rsf.LocalPath, rsf.RemotePath, "-o", "slave")
59+
if rsf.Port != 0 {
60+
sshArgs = append(sshArgs, "-p", strconv.Itoa(rsf.Port))
61+
}
62+
sshArgs = append(sshArgs, rsf.Host, "--")
63+
sshArgs = append(sshArgs, "sshfs", ":"+rsf.LocalPath, rsf.RemotePath, "-o", "slave")
5664
if rsf.Readonly {
5765
sshArgs = append(sshArgs, "-o", "ro")
5866
}

pkg/ssh/ssh.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ func ExitMaster(host string, port int, c *SSHConfig) error {
4545
return errors.New("got nil SSHConfig")
4646
}
4747
args := c.Args()
48-
args = append(args, "-O", "exit", "-p", strconv.Itoa(port), host)
48+
args = append(args, "-O", "exit")
49+
if port != 0 {
50+
args = append(args, "-p", strconv.Itoa(port))
51+
}
52+
args = append(args, host)
4953
cmd := exec.Command(c.Binary(), args...)
5054
logrus.Debugf("executing ssh for exiting the master: %s %v", cmd.Path, cmd.Args)
5155
out, err := cmd.CombinedOutput()
@@ -90,7 +94,10 @@ func ExecuteScript(host string, port int, c *SSHConfig, script, scriptName strin
9094
}
9195
sshBinary := c.Binary()
9296
sshArgs := c.Args()
93-
sshArgs = append(sshArgs, "-p", strconv.Itoa(port), host, "--", interpreter)
97+
if port != 0 {
98+
sshArgs = append(sshArgs, "-p", strconv.Itoa(port))
99+
}
100+
sshArgs = append(sshArgs, host, "--", interpreter)
94101
sshCmd := exec.Command(sshBinary, sshArgs...)
95102
sshCmd.Stdin = strings.NewReader(script)
96103
var stderr bytes.Buffer

pkg/sshocker/sshocker.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ func (x *Sshocker) Run() error {
3131
for _, l := range x.LForwards {
3232
args = append(args, "-L", l)
3333
}
34-
args = append(args, "-p", strconv.Itoa(x.Port), x.Host, "--")
34+
if x.Port != 0 {
35+
args = append(args, "-p", strconv.Itoa(x.Port))
36+
}
37+
args = append(args, x.Host, "--")
3538
if len(x.Command) > 0 {
3639
args = append(args, x.Command...)
3740
}

0 commit comments

Comments
 (0)