| 1 | package ssh |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | |
| 13 | "github.com/sirupsen/logrus" |
| 14 | ) |
| 15 | |
| 16 | type SSHConfig struct { |
| 17 | ConfigFile string |
| 18 | Persist bool |
| 19 | AdditionalArgs []string |
| 20 | } |
| 21 | |
| 22 | func (c *SSHConfig) Binary() string { |
| 23 | return "ssh" |
| 24 | } |
| 25 | |
| 26 | func (c *SSHConfig) Args() []string { |
| 27 | var args []string |
| 28 | if c.ConfigFile != "" { |
| 29 | args = append(args, "-F", c.ConfigFile) |
| 30 | } |
| 31 | if c.Persist { |
| 32 | args = append(args, |
| 33 | "-o", "ControlMaster=auto", |
| 34 | TODO: |
| 35 | "-o", "ControlPath=~/.ssh/sshocker-%r@%h:%p-"+strconv.Itoa(os.Getpid()), |
| 36 | "-o", "ControlPersist=yes", |
| 37 | ) |
| 38 | } |
| 39 | args = append(args, c.AdditionalArgs...) |
| 40 | return args |
| 41 | } |
| 42 | |
| 43 | |
| 44 | func ExitMaster(host string, port int, c *SSHConfig) error { |
| 45 | if c == nil { |
| 46 | return errors.New("got nil SSHConfig") |
| 47 | } |
| 48 | args := c.Args() |
| 49 | args = append(args, "-O", "exit") |
| 50 | if port != 0 { |
| 51 | args = append(args, "-p", strconv.Itoa(port)) |
| 52 | } |
| 53 | args = append(args, host) |
| 54 | cmd := exec.Command(c.Binary(), args...) |
| 55 | logrus.Debugf("executing ssh for exiting the master: %s %v", cmd.Path, cmd.Args) |
| 56 | out, err := cmd.CombinedOutput() |
| 57 | if err != nil { |
| 58 | return fmt.Errorf("failed to execute `%s -O exit -p %d %s`, out=%q: %w", c.Binary(), port, host, string(out), err) |
| 59 | } |
| 60 | return nil |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | func ParseScriptInterpreter(script string) (string, error) { |
| 66 | r := bufio.NewReader(strings.NewReader(script)) |
| 67 | firstLine, partial, err := r.ReadLine() |
| 68 | if err != nil { |
| 69 | return "", fmt.Errorf("cannot determine interpreter from script %q: %w", script, err) |
| 70 | } |
| 71 | if partial { |
| 72 | return "", fmt.Errorf("cannot determine interpreter from script %q: cannot read the first line", script) |
| 73 | } |
| 74 | if !strings.HasPrefix(string(firstLine), "#!") { |
| 75 | return "", fmt.Errorf("cannot determine interpreter from script %q: the first line lacks `#!`", script) |
| 76 | } |
| 77 | interp := strings.TrimPrefix(string(firstLine), "#!") |
| 78 | if interp == "" { |
| 79 | return "", fmt.Errorf("cannot determine interpreter from script %q: empty?", script) |
| 80 | } |
| 81 | return interp, nil |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | func ExecuteScript(host string, port int, c *SSHConfig, script, scriptName string) (string, string, error) { |
| 89 | if c == nil { |
| 90 | return "", "", errors.New("got nil SSHConfig") |
| 91 | } |
| 92 | interpreter, err := ParseScriptInterpreter(script) |
| 93 | if err != nil { |
| 94 | return "", "", err |
| 95 | } |
| 96 | sshBinary := c.Binary() |
| 97 | sshArgs := c.Args() |
| 98 | if port != 0 { |
| 99 | sshArgs = append(sshArgs, "-p", strconv.Itoa(port)) |
| 100 | } |
| 101 | sshArgs = append(sshArgs, host, "--", interpreter) |
| 102 | sshCmd := exec.Command(sshBinary, sshArgs...) |
| 103 | sshCmd.Stdin = strings.NewReader(script) |
| 104 | var stderr bytes.Buffer |
| 105 | sshCmd.Stderr = &stderr |
| 106 | logrus.Debugf("executing ssh for script %q: %s %v", scriptName, sshCmd.Path, sshCmd.Args) |
| 107 | out, err := sshCmd.Output() |
| 108 | if err != nil { |
| 109 | return string(out), stderr.String(), fmt.Errorf("failed to execute script %q: stdout=%q, stderr=%q: %w", scriptName, string(out), stderr.String(), err) |
| 110 | } |
| 111 | return string(out), stderr.String(), nil |
| 112 | } |