-
Notifications
You must be signed in to change notification settings - Fork 9
/
ftp.go
74 lines (66 loc) · 1.72 KB
/
ftp.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
64
65
66
67
68
69
70
71
72
73
74
package goutils
import (
"bufio"
"github.com/jlaffaye/ftp"
"golang.org/x/crypto/ssh"
"log"
"os"
"path/filepath"
"strings"
"time"
)
/*
ListOfAvailableFilesNoFTP o antigo nome era: ListaDeArquivosDisponiveisNoFTP
*/
func ListOfAvailableFilesNoFTP(remote string, port string, user string, pass string) (*ftp.ServerConn, error, []string) {
c, err := ftp.Dial(remote+":"+port, ftp.DialWithTimeout(60*time.Second))
if err != nil {
CreateFileDay(Message{Error: err.Error()})
}
err = c.Login(user, pass)
if err != nil {
CreateFileDay(Message{Error: err.Error()})
}
//var nome_arquivo string
list, _ := c.NameList("/")
if err := c.Quit(); err != nil {
CreateFileDay(Message{Error: err.Error()})
}
return c, err, list
}
/*
GetHostKey: parse OpenSSH known_hosts file ssh or use ssh-keyscan to get initial key
*/
func GetHostKey(host string, isProduction bool) ssh.PublicKey {
var file *os.File
var err error
if isProduction {
file, err = os.Open(filepath.Join("/", "home", "ubuntu", ".ssh", "known_hosts")) // Busca na pasta do usuario ubuntu
} else {
file, err = os.Open(filepath.Join(os.Getenv("HOME"), ".ssh", "known_hosts")) // Não busca no root, quando o crontab executa busca no root
}
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var hostKey ssh.PublicKey
for scanner.Scan() {
fields := strings.Split(scanner.Text(), " ")
if len(fields) != 3 {
continue
}
if strings.Contains(fields[0], host) {
var err error
hostKey, _, _, _, err = ssh.ParseAuthorizedKey(scanner.Bytes())
if err != nil {
log.Fatalf("error parsing %q: %v", fields[2], err)
}
break
}
}
if hostKey == nil {
log.Fatalf("no hostkey found for %s", host)
}
return hostKey
}