-
Notifications
You must be signed in to change notification settings - Fork 2
/
sftp_get_to_attachment_newJsch.js
60 lines (45 loc) · 2.17 KB
/
sftp_get_to_attachment_newJsch.js
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
// Note - stub function here that does work - but adapt ciphers and
// session.setConfig("StrictHostKeyChecking", "no") should not be set at all.
// Import required classes from the JSch library
var JSch = com.jcraft.jsch.JSch;
var Session = com.jcraft.jsch.Session;
function sftpFileTransferGet(serverName, port, userName, password, privateKeyPath, fromFileName, mimeType, base64encode) {
var jsch = new JSch();
var fileTransferred = false;
var session = null;
// Set up the session
if (privateKeyPath) {
jsch.addIdentity(privateKeyPath);
}
session = jsch.getSession(userName, serverName, port);
if (password) {
session.setPassword(password);
}
// Configure the host key verifier (remove in production, as it's not secure)
session.setConfig("StrictHostKeyChecking", "no");
// Configure the cipher algorithms to match the server's supported algorithms you may not need these.
session.setConfig("cipher.s2c", "aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],[email protected]");
session.setConfig("cipher.c2s", "aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],[email protected]");
session.connect();
// Create an SFTP channel
var channel = session.openChannel("sftp");
channel.connect();
var sftp = channel;
var inputStream = sftp.get(fromFileName);
var fileContent = org.apache.commons.io.IOUtils.toByteArray(inputStream);
inputStream.close()
addAttachment(fileContent, mimeType, base64encode)
fileTransferred = true; // Mark the file as transferred
// Disconnect and close the SFTP channel
sftp.disconnect();
// Disconnect and close the SSH session
session.disconnect();
return fileTransferred; // Return true if the file was transferred successfully
}
// Using username/password authentication for upload
var serverName1 = 'some.domain.com';
var userName1 = 'username';
var password1 = 'password';
var privateKeyPath1 = null;
var fromFileName1 = '/tmp/junk.txt';
var fileGet = sftpFileTransferGet(serverName1, 22, userName1, password1, privateKeyPath1, fromFileName1, 'text/plain', false);