| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"flag" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"log" |
| 9 |
"net" |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
"sync" |
| 13 |
|
| 14 |
"github.com/kisom/goutils/lib" |
| 15 |
"github.com/pkg/sftp" |
| 16 |
"golang.org/x/crypto/ssh" |
| 17 |
"golang.org/x/crypto/ssh/agent" |
| 18 |
) |
| 19 |
|
| 20 |
func usage(w io.Writer) { |
| 21 |
fmt.Fprintf(w, `Usage: %s [-a addresses] [-c chunksize] [-h] [-u user] command args |
| 22 |
|
| 23 |
Flags: |
| 24 |
-a addresses The comma-separated list of servers to send |
| 25 |
to. There must not be any spaces in this list. |
| 26 |
|
| 27 |
-c chunk The size of chunks to transfer, in bytes. The |
| 28 |
default is 16MB. |
| 29 |
|
| 30 |
-u user The SSH username to use. It must be the |
| 31 |
same for all hosts. Defaults to the value |
| 32 |
of the USER environment variable. |
| 33 |
|
| 34 |
Commands: |
| 35 |
exec, run: run a command on the servers |
| 36 |
The args list must be the command line to send to the hosts. |
| 37 |
|
| 38 |
upload, up, push, send: upload a file to the servers |
| 39 |
The first argument is the local file to upload, and the second |
| 40 |
is the filename to store it as on the remote. |
| 41 |
|
| 42 |
download, down, pull, fetch: download a file from the servers |
| 43 |
The first argument is the filename to fetch.The second argument |
| 44 |
is the base name for the local file. It will have a '-' and the |
| 45 |
hostname appended. |
| 46 |
|
| 47 |
`, lib.ProgName()) |
| 48 |
} |
| 49 |
|
| 50 |
var chunkSize = 16777216 |
| 51 |
|
| 52 |
var modes = ssh.TerminalModes{ |
| 53 |
ssh.ECHO: 0, |
| 54 |
ssh.TTY_OP_ISPEED: 14400, |
| 55 |
ssh.TTY_OP_OSPEED: 14400, |
| 56 |
} |
| 57 |
|
| 58 |
func sshAgent() ssh.AuthMethod { |
| 59 |
a, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")) |
| 60 |
if err == nil { |
| 61 |
return ssh.PublicKeysCallback(agent.NewClient(a).Signers) |
| 62 |
} |
| 63 |
|
| 64 |
lib.Err(lib.ExitFailure, err, "failed to authenticate with SSH agent") |
| 65 |
return nil |
| 66 |
} |
| 67 |
|
| 68 |
func sshConfig(user string) *ssh.ClientConfig { |
| 69 |
return &ssh.ClientConfig{ |
| 70 |
User: user, |
| 71 |
Auth: []ssh.AuthMethod{ |
| 72 |
sshAgent(), |
| 73 |
}, |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func scanner(host string, in io.Reader, out io.Writer) { |
| 78 |
scanner := bufio.NewScanner(in) |
| 79 |
for scanner.Scan() { |
| 80 |
line := scanner.Text() |
| 81 |
fmt.Fprintf(out, "[%s] %s\n", host, line) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func logError(host string, err error, format string, args ...interface{}) { |
| 86 |
msg := fmt.Sprintf(format, args...) |
| 87 |
log.Printf("[%s] FAILED: %s: %v\n", host, msg, err) |
| 88 |
} |
| 89 |
|
| 90 |
func exec(wg *sync.WaitGroup, user, host string, commands []string) { |
| 91 |
var shutdown []func() error |
| 92 |
|
| 93 |
defer func() { |
| 94 |
for i := len(shutdown) - 1; i >= 0; i-- { |
| 95 |
err := shutdown[i]() |
| 96 |
if err != nil && err != io.EOF { |
| 97 |
logError(host, err, "shutting down") |
| 98 |
} |
| 99 |
} |
| 100 |
}() |
| 101 |
defer wg.Done() |
| 102 |
|
| 103 |
conf := sshConfig(user) |
| 104 |
conn, err := ssh.Dial("tcp", host+":22", conf) |
| 105 |
if err != nil { |
| 106 |
logError(host, err, "failed to connect") |
| 107 |
return |
| 108 |
} |
| 109 |
shutdown = append(shutdown, conn.Close) |
| 110 |
|
| 111 |
session, err := conn.NewSession() |
| 112 |
if err != nil { |
| 113 |
logError(host, err, "failed to establish session") |
| 114 |
return |
| 115 |
} |
| 116 |
shutdown = append(shutdown, session.Close) |
| 117 |
|
| 118 |
if err := session.RequestPty("xterm", 80, 40, modes); err != nil { |
| 119 |
session.Close() |
| 120 |
logError(host, err, "request for pty failed") |
| 121 |
return |
| 122 |
} |
| 123 |
|
| 124 |
stdout, err := session.StdoutPipe() |
| 125 |
if err != nil { |
| 126 |
logError(host, err, "failed to setup standard output") |
| 127 |
return |
| 128 |
} |
| 129 |
go scanner(host, stdout, os.Stdout) |
| 130 |
|
| 131 |
stderr, err := session.StderrPipe() |
| 132 |
if err != nil { |
| 133 |
logError(host, err, "failed to setup standard error") |
| 134 |
return |
| 135 |
} |
| 136 |
go scanner(host, stderr, os.Stderr) |
| 137 |
|
| 138 |
for _, command := range commands { |
| 139 |
err = session.Run(command) |
| 140 |
if err != nil { |
| 141 |
logError(host, err, "running command failed") |
| 142 |
return |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func upload(wg *sync.WaitGroup, user, host, local, remote string) { |
| 148 |
var shutdown []func() error |
| 149 |
|
| 150 |
defer func() { |
| 151 |
for i := len(shutdown) - 1; i >= 0; i-- { |
| 152 |
err := shutdown[i]() |
| 153 |
if err != nil && err != io.EOF { |
| 154 |
logError(host, err, "shutting down") |
| 155 |
} |
| 156 |
} |
| 157 |
}() |
| 158 |
defer wg.Done() |
| 159 |
|
| 160 |
conf := sshConfig(user) |
| 161 |
conn, err := ssh.Dial("tcp", host+":22", conf) |
| 162 |
if err != nil { |
| 163 |
logError(host, err, "failed to connect") |
| 164 |
return |
| 165 |
} |
| 166 |
shutdown = append(shutdown, conn.Close) |
| 167 |
|
| 168 |
sftp, err := sftp.NewClient(conn) |
| 169 |
if err != nil { |
| 170 |
logError(host, err, "setting up SFTP client") |
| 171 |
return |
| 172 |
} |
| 173 |
shutdown = append(shutdown, sftp.Close) |
| 174 |
|
| 175 |
remoteFile, err := sftp.Create(remote) |
| 176 |
if err != nil { |
| 177 |
logError(host, err, "creating file %s on remote", remote) |
| 178 |
return |
| 179 |
} |
| 180 |
shutdown = append(shutdown, remoteFile.Close) |
| 181 |
|
| 182 |
localFile, err := os.Open(local) |
| 183 |
if err != nil { |
| 184 |
logError(host, err, "opening local file") |
| 185 |
return |
| 186 |
} |
| 187 |
shutdown = append(shutdown, localFile.Close) |
| 188 |
|
| 189 |
var buf = make([]byte, chunkSize) |
| 190 |
for { |
| 191 |
var n int |
| 192 |
n, err = localFile.Read(buf) |
| 193 |
if n > 0 { |
| 194 |
_, err = remoteFile.Write(buf[:n]) |
| 195 |
if err != nil { |
| 196 |
logError(host, err, "writing chunk") |
| 197 |
return |
| 198 |
} |
| 199 |
fmt.Printf("[%s] wrote %d-byte chunk\n", host, n) |
| 200 |
} |
| 201 |
|
| 202 |
if err == io.EOF { |
| 203 |
break |
| 204 |
} else if err != nil { |
| 205 |
logError(host, err, "reading chunk") |
| 206 |
return |
| 207 |
} |
| 208 |
} |
| 209 |
fmt.Printf("[%s] %s uploaded to %s\n", host, remote, local) |
| 210 |
} |
| 211 |
|
| 212 |
func download(wg *sync.WaitGroup, user, host, local, remote string) { |
| 213 |
var shutdown []func() error |
| 214 |
|
| 215 |
defer func() { |
| 216 |
for i := len(shutdown) - 1; i >= 0; i-- { |
| 217 |
err := shutdown[i]() |
| 218 |
if err != nil && err != io.EOF { |
| 219 |
logError(host, err, "shutting down") |
| 220 |
} |
| 221 |
} |
| 222 |
}() |
| 223 |
defer wg.Done() |
| 224 |
|
| 225 |
conf := sshConfig(user) |
| 226 |
conn, err := ssh.Dial("tcp", host+":22", conf) |
| 227 |
if err != nil { |
| 228 |
logError(host, err, "failed to connect") |
| 229 |
return |
| 230 |
} |
| 231 |
shutdown = append(shutdown, conn.Close) |
| 232 |
|
| 233 |
sftp, err := sftp.NewClient(conn) |
| 234 |
if err != nil { |
| 235 |
logError(host, err, "setting up SFTP client") |
| 236 |
return |
| 237 |
} |
| 238 |
shutdown = append(shutdown, sftp.Close) |
| 239 |
|
| 240 |
remoteFile, err := sftp.Open(remote) |
| 241 |
if err != nil { |
| 242 |
logError(host, err, "opening file %s on remote", remote) |
| 243 |
return |
| 244 |
} |
| 245 |
shutdown = append(shutdown, remoteFile.Close) |
| 246 |
|
| 247 |
local = local + "-" + host |
| 248 |
localFile, err := os.Create(local) |
| 249 |
if err != nil { |
| 250 |
logError(host, err, "opening local file") |
| 251 |
return |
| 252 |
} |
| 253 |
shutdown = append(shutdown, localFile.Close) |
| 254 |
|
| 255 |
var buf = make([]byte, chunkSize) |
| 256 |
for { |
| 257 |
var n int |
| 258 |
n, err = remoteFile.Read(buf) |
| 259 |
if n > 0 { |
| 260 |
_, err = localFile.Write(buf[:n]) |
| 261 |
if err != nil { |
| 262 |
logError(host, err, "writing chunk") |
| 263 |
return |
| 264 |
} |
| 265 |
fmt.Printf("[%s] wrote %d-byte chunk\n", host, n) |
| 266 |
} |
| 267 |
|
| 268 |
if err == io.EOF { |
| 269 |
break |
| 270 |
} else if err != nil { |
| 271 |
logError(host, err, "reading chunk") |
| 272 |
return |
| 273 |
} |
| 274 |
} |
| 275 |
fmt.Printf("[%s] %s downloaded to %s\n", host, remote, local) |
| 276 |
} |
| 277 |
|
| 278 |
func init() { |
| 279 |
flag.Usage = func() { |
| 280 |
usage(os.Stdout) |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
func main() { |
| 285 |
var hostsFlag, user string |
| 286 |
flag.StringVar(&hostsFlag, "a", "", "`hosts` to run on") |
| 287 |
flag.IntVar(&chunkSize, "c", chunkSize, "`size` in bytes of transfer chunks") |
| 288 |
flag.StringVar(&user, "u", os.Getenv("USER"), "`username` to run commands as") |
| 289 |
flag.Parse() |
| 290 |
|
| 291 |
hosts := strings.Split(hostsFlag, ",") |
| 292 |
if len(hosts) == 0 { |
| 293 |
os.Exit(0) |
| 294 |
} |
| 295 |
|
| 296 |
if flag.NArg() < 1 { |
| 297 |
usage(os.Stderr) |
| 298 |
os.Exit(1) |
| 299 |
} |
| 300 |
|
| 301 |
cmd := flag.Arg(0) |
| 302 |
var wg = new(sync.WaitGroup) |
| 303 |
|
| 304 |
switch cmd { |
| 305 |
case "exec", "run": |
| 306 |
if flag.NArg() < 2 { |
| 307 |
usage(os.Stderr) |
| 308 |
os.Exit(1) |
| 309 |
} |
| 310 |
|
| 311 |
commands := []string{strings.Join(flag.Args()[1:], " ")} |
| 312 |
for _, host := range hosts { |
| 313 |
wg.Add(1) |
| 314 |
go exec(wg, user, host, commands) |
| 315 |
} |
| 316 |
case "upload", "up", "push", "send": |
| 317 |
if flag.NArg() != 3 { |
| 318 |
usage(os.Stderr) |
| 319 |
os.Exit(1) |
| 320 |
} |
| 321 |
|
| 322 |
for _, host := range hosts { |
| 323 |
wg.Add(1) |
| 324 |
go upload(wg, user, host, flag.Arg(1), flag.Arg(2)) |
| 325 |
} |
| 326 |
case "download", "down", "pull", "fetch": |
| 327 |
if flag.NArg() != 3 { |
| 328 |
usage(os.Stderr) |
| 329 |
os.Exit(1) |
| 330 |
} |
| 331 |
|
| 332 |
for _, host := range hosts { |
| 333 |
wg.Add(1) |
| 334 |
go download(wg, user, host, flag.Arg(2), flag.Arg(1)) |
| 335 |
} |
| 336 |
case "help": |
| 337 |
usage(os.Stdout) |
| 338 |
os.Exit(0) |
| 339 |
default: |
| 340 |
fmt.Fprintf(os.Stderr, "Unknown command %s.\n", cmd) |
| 341 |
usage(os.Stderr) |
| 342 |
os.Exit(1) |
| 343 |
} |
| 344 |
|
| 345 |
log.Printf("waiting for sessions to complete") |
| 346 |
wg.Wait() |
| 347 |
} |