diff --git a/main-mshell.go b/main-mshell.go index 5010b0de..8b5d637e 100644 --- a/main-mshell.go +++ b/main-mshell.go @@ -10,6 +10,7 @@ import ( "bytes" "fmt" "os" + "strconv" "strings" "github.com/scripthaus-dev/mshell/pkg/base" @@ -283,6 +284,21 @@ func tryParseSSHOpt(iter *base.OptsIter, sshOpts *shexec.SSHOpts) (bool, error) sshOpts.SSHUser = iter.Next() return true, nil } + if argStr == "-p" { + if !iter.IsNextPlain() { + return false, fmt.Errorf("-p [port]' missing port") + } + nextArgStr := iter.Next() + portVal, err := strconv.Atoi(nextArgStr) + if err != nil { + return false, fmt.Errorf("-p [port]' invalid port: %v", err) + } + if portVal <= 0 { + return false, fmt.Errorf("-p [port]' invalid port: %d", portVal) + } + sshOpts.SSHPort = portVal + return true, nil + } return false, nil } diff --git a/pkg/shexec/shexec.go b/pkg/shexec/shexec.go index b4fb3e7a..54f2afaf 100644 --- a/pkg/shexec/shexec.go +++ b/pkg/shexec/shexec.go @@ -390,7 +390,9 @@ type SSHOpts struct { SSHOptsStr string SSHIdentity string SSHUser string + SSHPort int SSHErrorsToTty bool + BatchMode bool } type InstallOpts struct { @@ -469,12 +471,20 @@ func (opts SSHOpts) MakeSSHExecCmd(remoteCommand string) *exec.Cmd { userOpt := fmt.Sprintf("-l %s", shellescape.Quote(opts.SSHUser)) moreSSHOpts = append(moreSSHOpts, userOpt) } - // note that SSHOptsStr is *not* escaped - var errFdStr string - if opts.SSHErrorsToTty { - errFdStr = "-E /dev/tty" + if opts.SSHPort != 0 { + portOpt := fmt.Sprintf("-p %d", opts.SSHPort) + moreSSHOpts = append(moreSSHOpts, portOpt) } - sshCmd := fmt.Sprintf("ssh %s %s %s %s %s", errFdStr, strings.Join(moreSSHOpts, " "), opts.SSHOptsStr, shellescape.Quote(opts.SSHHost), shellescape.Quote(remoteCommand)) + if opts.SSHErrorsToTty { + errFdStr := "-E /dev/tty" + moreSSHOpts = append(moreSSHOpts, errFdStr) + } + if opts.BatchMode { + batchOpt := "-o 'BatchMode=yes'" + moreSSHOpts = append(moreSSHOpts, batchOpt) + } + // note that SSHOptsStr is *not* escaped + sshCmd := fmt.Sprintf("ssh %s %s %s %s", strings.Join(moreSSHOpts, " "), opts.SSHOptsStr, shellescape.Quote(opts.SSHHost), shellescape.Quote(remoteCommand)) ecmd := exec.Command("bash", "-c", sshCmd) return ecmd } @@ -490,6 +500,10 @@ func (opts SSHOpts) MakeMShellSSHOpts() string { userOpt := fmt.Sprintf("-l %s", shellescape.Quote(opts.SSHUser)) moreSSHOpts = append(moreSSHOpts, userOpt) } + if opts.SSHPort != 0 { + portOpt := fmt.Sprintf("-p %d", opts.SSHPort) + moreSSHOpts = append(moreSSHOpts, portOpt) + } if opts.SSHOptsStr != "" { optsOpt := fmt.Sprintf("--ssh-opts %s", shellescape.Quote(opts.SSHOptsStr)) moreSSHOpts = append(moreSSHOpts, optsOpt)