-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscp_example_test.go
58 lines (49 loc) · 1.23 KB
/
scp_example_test.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
package scp_test
import (
"fmt"
"io/ioutil"
"log"
"net"
"os"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"github.com/tmc/scp"
)
func getAgent() (agent.Agent, error) {
agentConn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
return agent.NewClient(agentConn), err
}
func ExampleCopyPath() {
f, _ := ioutil.TempFile("", "")
fmt.Fprintln(f, "hello world")
f.Close()
defer os.Remove(f.Name())
defer os.Remove(f.Name() + "-copy")
agent, err := getAgent()
if err != nil {
log.Fatalln("Failed to connect to SSH_AUTH_SOCK:", err)
}
client, err := ssh.Dial("tcp", "127.0.0.1:22", &ssh.ClientConfig{
User: os.Getenv("USER"),
Auth: []ssh.AuthMethod{
ssh.PublicKeysCallback(agent.Signers),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // FIXME: please be more secure in checking host keys
})
if err != nil {
log.Fatalln("Failed to dial:", err)
}
session, err := client.NewSession()
if err != nil {
log.Fatalln("Failed to create session: " + err.Error())
}
dest := f.Name() + "-copy"
err = scp.CopyPath(f.Name(), dest, session)
if _, err := os.Stat(dest); os.IsNotExist(err) {
fmt.Printf("no such file or directory: %s", dest)
} else {
fmt.Println("success")
}
// output:
// success
}