-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.go
48 lines (38 loc) · 882 Bytes
/
lib.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
package sink
import (
"bytes"
"net"
"strings"
"golang.org/x/crypto/ssh"
)
func GetDirectoryListing(addr, user, pass, dir string) (*[]string, *[]string, error) {
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(pass),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
client, err := ssh.Dial("tcp", net.JoinHostPort(addr, "22"), config)
if err != nil {
return nil, nil, err
}
session, err := client.NewSession()
if err != nil {
return nil, nil, err
}
defer session.Close()
var b bytes.Buffer
session.Stdout = &b
err = session.Run("ls -p " + dir)
files := []string{}
folders := []string{}
for _, line := range strings.Split(b.String(), "\n") {
if strings.HasSuffix(line, "/") {
folders = append(folders, line[:len(line)-1])
} else {
files = append(files, line)
}
}
return &folders, &files, nil
}