CoSSH manages SSH connections, which are referenced by the @name identifier. It handles the following cases: - @name -- switches to using given host for all subsequent commands - host [name] -- connects to a server specified in first arg and switches to using it, with optional name instead of defau
(cmdIO *exec.CmdIO, args ...string)
| 177 | // to using it, with optional name instead of default sequential number. |
| 178 | // - close -- closes all open connections, or the specified one |
| 179 | func (sh *Shell) CoSSH(cmdIO *exec.CmdIO, args ...string) error { |
| 180 | if len(args) < 1 { |
| 181 | return fmt.Errorf("cossh: requires at least one argument") |
| 182 | } |
| 183 | cmd := args[0] |
| 184 | var err error |
| 185 | host := "" |
| 186 | name := fmt.Sprintf("%d", 1+len(sh.SSHClients)) |
| 187 | con := false |
| 188 | switch { |
| 189 | case cmd == "close": |
| 190 | sh.CloseSSH() |
| 191 | return nil |
| 192 | case cmd == "@" && len(args) == 2: |
| 193 | name = args[1] |
| 194 | case len(args) == 2: |
| 195 | con = true |
| 196 | host = args[0] |
| 197 | name = args[1] |
| 198 | default: |
| 199 | con = true |
| 200 | host = args[0] |
| 201 | } |
| 202 | if con { |
| 203 | cl := sshclient.NewClient(sh.SSH) |
| 204 | err = cl.Connect(host) |
| 205 | if err != nil { |
| 206 | return err |
| 207 | } |
| 208 | sh.SSHClients[name] = cl |
| 209 | sh.SSHActive = name |
| 210 | } else { |
| 211 | if name == "0" { |
| 212 | sh.SSHActive = "" |
| 213 | } else { |
| 214 | sh.SSHActive = name |
| 215 | cl := sh.ActiveSSH() |
| 216 | if cl == nil { |
| 217 | err = fmt.Errorf("cosh: ssh connection named: %q not found", name) |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | return err |
| 222 | } |
| 223 | |
| 224 | // Scp performs file copy over SSH connection, with the remote filename |
| 225 | // prefixed with the @name: and the local filename un-prefixed. |