-
Notifications
You must be signed in to change notification settings - Fork 0
/
scp.go
63 lines (50 loc) · 1.28 KB
/
scp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"log"
"os"
scp "github.com/jhand2/go-scp"
"github.com/jhand2/go-scp/auth"
"golang.org/x/crypto/ssh"
)
type ScpCopier struct {
client scp.Client
}
func (s *ScpCopier) Init(username string, privkey string, server string) {
// TODO: Allow to use specific privkey path or ssh agent.
// Get password for privkey if ssh-agent fails
//fmt.Print("Enter Password: ")
//pw, err := terminal.ReadPassword(int(syscall.Stdin))
//if err != nil {
//log.Fatal("Could not read password. Error: ", err)
//}
//fmt.Print("\n")
//conf, err := auth.PrivateKeyWithPassphrase(username, pw, privkey, ssh.InsecureIgnoreHostKey())
//if err != nil {
//log.Fatal("Could not use private key. Error: ", err)
//}
conf, err := auth.SshAgent(username, ssh.InsecureIgnoreHostKey())
if err != nil {
log.Fatal("Could not use ssh agent. Error: ", err)
}
s.client = scp.NewClient(server, &conf)
}
func (s *ScpCopier) copy_file(src string, dst string) error {
fmt.Printf("Copying %s to %s\n", src, dst)
err := s.client.Connect()
if err != nil {
return err
}
f, err := os.Open(src)
if err != nil {
return err
}
defer s.client.Close()
defer f.Close()
// TODO: Change file permissions?
err = s.client.CopyFile(f, dst, "0655")
if err != nil {
return err
}
return nil
}