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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
| package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"github.com/pkg/sftp"
)
const (
sftpUser = "citizix"
sftpPass = "Str0ngP4ss"
sftpHost = "10.2.11.10"
sftpPort = "22"
)
func main() {
// Create a url
rawurl := fmt.Sprintf("sftp://%v:%v@%v", sftpUser, sftpPass, sftpHost)
// Parse the URL
parsedUrl, err := url.Parse(rawurl)
if err != nil {
log.Fatalf("Failed to parse SFTP To Go URL: %s", err)
}
// Get user name and pass
user := parsedUrl.User.Username()
pass, _ := parsedUrl.User.Password()
// Parse Host and Port
host := parsedUrl.Host
// Get hostkey
hostKey := getHostKey(host)
log.Printf("Connecting to %s ...\n", host)
var auths []ssh.AuthMethod
// Try to use $SSH_AUTH_SOCK which contains the path of the unix file socket that the sshd agent uses
// for communication with other processes.
if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))
}
// Use password authentication if provided
if pass != "" {
auths = append(auths, ssh.Password(pass))
}
// Initialize client configuration
config := ssh.ClientConfig{
User: user,
Auth: auths,
// Auth: []ssh.AuthMethod{
// ssh.KeyboardInteractive(SshInteractive),
// },
// Uncomment to ignore host key check
// HostKeyCallback: ssh.InsecureIgnoreHostKey(),
HostKeyCallback: ssh.FixedHostKey(hostKey),
// HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
// return nil
// },
Timeout: 30 * time.Second,
}
addr := fmt.Sprintf("%s:%s", host, sftpPort)
// Connect to server
conn, err := ssh.Dial("tcp", addr, &config)
if err != nil {
log.Fatalf("Failed to connec to host [%s]: %v", addr, err)
}
defer conn.Close()
// Create new SFTP client
sc, err := sftp.NewClient(conn)
if err != nil {
log.Fatalf("Unable to start SFTP subsystem: %v", err)
}
defer sc.Close()
// List files in the root directory .
theFiles, err := listFiles(*sc, ".")
if err != nil {
log.Fatalf("failed to list files in .: %v", err)
}
log.Printf("Found Files in . Files")
// Output each file name and size in bytes
log.Printf("%19s %12s %s", "MOD TIME", "SIZE", "NAME")
for _, theFile := range theFiles {
log.Printf("%19s %12s %s", theFile.ModTime, theFile.Size, theFile.Name)
}
// Upload local file
err = uploadFile(*sc, "/Users/etowett/Desktop/data.csv", "./citizix/data.csv")
if err != nil {
log.Fatalf("could not upload file: %v", err)
}
// Download remote file to local file.
err = downloadFile(*sc, "citizix/data.csv", "data.csv")
if err != nil {
log.Fatalf("Could not download file data.csv; %v", err)
}
return
}
func SshInteractive(user, instruction string, questions []string, echos []bool) (answers []string, err error) {
// Hack, check https://stackoverflow.com/questions/47102080/ssh-in-go-unable-to-authenticate-attempted-methods-none-no-supported-method
answers = make([]string, len(questions))
// The second parameter is unused
for n, _ := range questions {
answers[n] = sftpPass
}
return answers, nil
}
type remoteFiles struct {
Name string
Size string
ModTime string
}
func listFiles(sc sftp.Client, remoteDir string) (theFiles []remoteFiles, err error) {
files, err := sc.ReadDir(remoteDir)
if err != nil {
return theFiles, fmt.Errorf("Unable to list remote dir: %v", err)
}
for _, f := range files {
var name, modTime, size string
name = f.Name()
modTime = f.ModTime().Format("2006-01-02 15:04:05")
size = fmt.Sprintf("%12d", f.Size())
if f.IsDir() {
name = name + "/"
modTime = ""
size = "PRE"
}
theFiles = append(theFiles, remoteFiles{
Name: name,
Size: size,
ModTime: modTime,
})
}
return theFiles, nil
}
// Upload file to sftp server
func uploadFile(sc sftp.Client, localFile, remoteFile string) (err error) {
log.Printf("Uploading [%s] to [%s] ...", localFile, remoteFile)
srcFile, err := os.Open(localFile)
if err != nil {
return fmt.Errorf("Unable to open local file: %v", err)
}
defer srcFile.Close()
// Make remote directories recursion
parent := filepath.Dir(remoteFile)
path := string(filepath.Separator)
dirs := strings.Split(parent, path)
for _, dir := range dirs {
path = filepath.Join(path, dir)
sc.Mkdir(path)
}
// Note: SFTP Go doesn't support O_RDWR mode
dstFile, err := sc.OpenFile(remoteFile, (os.O_WRONLY | os.O_CREATE | os.O_TRUNC))
if err != nil {
return fmt.Errorf("Unable to open remote file: %v", err)
}
defer dstFile.Close()
bytes, err := io.Copy(dstFile, srcFile)
if err != nil {
return fmt.Errorf("Unable to upload local file: %v", err)
}
log.Printf("%d bytes copied", bytes)
return nil
}
// Download file from sftp server
func downloadFile(sc sftp.Client, remoteFile, localFile string) (err error) {
log.Printf("Downloading [%s] to [%s] ...\n", remoteFile, localFile)
// Note: SFTP To Go doesn't support O_RDWR mode
srcFile, err := sc.OpenFile(remoteFile, (os.O_RDONLY))
if err != nil {
return fmt.Errorf("unable to open remote file: %v", err)
}
defer srcFile.Close()
dstFile, err := os.Create(localFile)
if err != nil {
return fmt.Errorf("unable to open local file: %v", err)
}
defer dstFile.Close()
bytes, err := io.Copy(dstFile, srcFile)
if err != nil {
return fmt.Errorf("unable to download remote file: %v", err)
}
log.Printf("%d bytes copied to %v", bytes, dstFile)
return nil
}
// Get host key from local known hosts
func getHostKey(host string) ssh.PublicKey {
// parse OpenSSH known_hosts file
// ssh or use ssh-keyscan to get initial key
file, err := os.Open(filepath.Join(os.Getenv("HOME"), ".ssh", "known_hosts"))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to read known_hosts file: %v\n", err)
os.Exit(1)
}
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 {
fmt.Fprintf(os.Stderr, "Error parsing %q: %v\n", fields[2], err)
os.Exit(1)
}
break
}
}
if hostKey == nil {
fmt.Fprintf(os.Stderr, "No hostkey found for %s", host)
os.Exit(1)
}
return hostKey
}
|