Debian Codegraph @

example/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "bufio"
5 "fmt"
6 "net"
7 "os"
8 "path/filepath"
9 "strings"
10 "time"
11
12 "github.com/dtylman/scp"
13 "golang.org/x/crypto/ssh"
14 )
15
16 func connect(host, user, password string) (*ssh.Client, error) {
17
18 fmt.Printf("Opening tcp to %v\n", host)
19 conn, err := net.DialTimeout("tcp", host, time.Second*30)
20 if err != nil {
21 return nil, err
22 }
23 config := &ssh.ClientConfig{
24 User: user,
25 Auth: []ssh.AuthMethod{ssh.Password(password)},
26 }
27 fmt.Printf("Establishing ssh session %v...\n", host)
28 sshconn, chans, reqs, err := ssh.NewClientConn(conn, host, config)
29 if err != nil {
30 return nil, err
31 }
32 return ssh.NewClient(sshconn, chans, reqs), nil
33 }
34
35 func doScp(host, user, remotepath string) error {
36 reader := bufio.NewReader(os.Stdin)
37 fmt.Print("Password: ")
38 password, err := reader.ReadString('\n')
39 if err != nil {
40 return err
41 }
42 sc, err := connect(host, user, strings.TrimSpace(password))
43 if err != nil {
44 return err
45 }
46 start := time.Now()
47 n, err := scp.CopyFrom(sc, remotepath, filepath.Base(remotepath))
48 if err != nil {
49 return err
50 }
51 fmt.Printf("Copied %v bytes in %v\n", n, time.Now().Sub(start))
52 return nil
53 }
54
55 func main() {
56 if len(os.Args) < 4 {
57 fmt.Println(os.Args[0] + " will scp a remote file here.")
58 fmt.Println("Usage [host:port] [user] [remote_path]")
59 return
60 }
61 err := doScp(os.Args[1], os.Args[2], os.Args[3])
62 if err != nil {
63 fmt.Println(err.Error())
64 }
65 }