|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net" |
| 9 | + "path/filepath" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/dtylman/scp" |
| 13 | + "golang.org/x/crypto/ssh" |
| 14 | +) |
| 15 | + |
| 16 | +var Options struct { |
| 17 | + From string |
| 18 | + Match string |
| 19 | + To string |
| 20 | + Host string |
| 21 | + Port int |
| 22 | + Username string |
| 23 | + Password string |
| 24 | + DialTimeout time.Duration |
| 25 | +} |
| 26 | + |
| 27 | +func connect() (*ssh.Client, error) { |
| 28 | + if Options.Host == "" { |
| 29 | + return nil, errors.New("host is empty") |
| 30 | + } |
| 31 | + address := fmt.Sprintf("%v:%v", Options.Host, Options.Port) |
| 32 | + log.Printf("opening tcp to %v", Options.Host) |
| 33 | + conn, err := net.DialTimeout("tcp", address, Options.DialTimeout) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + config := &ssh.ClientConfig{ |
| 38 | + User: Options.Username, |
| 39 | + HostKeyCallback: ssh.InsecureIgnoreHostKey(), |
| 40 | + Auth: []ssh.AuthMethod{ssh.Password(Options.Password)}, |
| 41 | + } |
| 42 | + log.Printf("establishing ssh session %v...", address) |
| 43 | + sshConn, sshChan, reqChan, err := ssh.NewClientConn(conn, address, config) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + return ssh.NewClient(sshConn, sshChan, reqChan), nil |
| 48 | +} |
| 49 | + |
| 50 | +func goSCP() error { |
| 51 | + walker, err := NewFilesWalker(Options.From, Options.Match) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + err = walker.Walk() |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + if len(walker.Matches) == 0 { |
| 60 | + log.Printf("pattern '%v' yields no files to copy", Options.From) |
| 61 | + return nil |
| 62 | + } |
| 63 | + sc, err := connect() |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + defer sc.Close() |
| 68 | + start := time.Now() |
| 69 | + total := int64(0) |
| 70 | + for _, path := range walker.Matches { |
| 71 | + remotePath := fmt.Sprintf("%v/%v", Options.To, filepath.Base(path)) |
| 72 | + log.Printf("copying %v to %v", path, remotePath) |
| 73 | + n, err := scp.CopyTo(sc, path, remotePath) |
| 74 | + if err != nil { |
| 75 | + log.Printf("error: %v", err) |
| 76 | + } |
| 77 | + total += n |
| 78 | + } |
| 79 | + log.Printf("copied %v bytes in %v", total, time.Since(start)) |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func main() { |
| 84 | + log.SetFlags(0) |
| 85 | + flag.StringVar(&Options.From, "from", "", "copy from (path)") |
| 86 | + flag.StringVar(&Options.Match, "match", ".*", "if [from] is a folder, scan recursively and match against this regular expression") |
| 87 | + flag.StringVar(&Options.To, "to", "", "path on target machine") |
| 88 | + flag.StringVar(&Options.Host, "host", "", "host machine") |
| 89 | + flag.IntVar(&Options.Port, "port", 22, "port") |
| 90 | + flag.StringVar(&Options.Username, "username", "", "user name") |
| 91 | + flag.StringVar(&Options.Password, "password", "", "user password") |
| 92 | + flag.DurationVar(&Options.DialTimeout, "dial-timeout", time.Minute/2, "dial timeout") |
| 93 | + |
| 94 | + flag.Parse() |
| 95 | + |
| 96 | + err := goSCP() |
| 97 | + if err != nil { |
| 98 | + log.Printf("error: %v", err) |
| 99 | + } |
| 100 | +} |
0 commit comments