Skip to content

Instantly share code, notes, and snippets.

@assertivist
Forked from paulsena/SFTP Client.js
Created May 24, 2018 20:24
Show Gist options
  • Save assertivist/8679d5eedde583f01062427de6a4db76 to your computer and use it in GitHub Desktop.
Save assertivist/8679d5eedde583f01062427de6a4db76 to your computer and use it in GitHub Desktop.
ServiceNow SFTP / SSH Javascript Client
var Sftp = Class.create();
Sftp.prototype = {
initialize: function() {
gs.print("Setting up SSH client.");
this.dataSourceID = '';
this.hostname = '';
this.port = 22;
this.username = '';
this.password = '';
this.path = '';
this.filename = '';
this.contentType = 'text/csv';
},
getFile: function() {
//setup SSH client
var ssh = Packages.com.sshtools.j2ssh.SshClient();
var ignore = Packages.com.sshtools.j2ssh.transport.IgnoreHostKeyVerification(); //ignore the known_hosts file
//auth info
gs.print("Setting up auth.");
var pwd = Packages.com.sshtools.j2ssh.authentication.PasswordAuthenticationClient();
pwd.setUsername(this.username);
pwd.setPassword(this.password);
//ssh properties
var properties = Packages.com.sshtools.j2ssh.configuration.SshConnectionProperties();
properties.setHost(this.hostname);
properties.setPort(this.port);
//set up output stream
gs.print("Setting up output stream.");
var output = Packages.java.io.ByteArrayOutputStream();
//set up the attachment
gs.print("Setting up attachment.");
var attachment = Packages.com.glide.ui.SysAttachment();
attachment.setTableName('sys_data_source'); //table name of the record this is being attached to
attachment.setTableSysID(this.dataSourceID); //sys_id of the data source record this is being attached to
attachment.setFileName(this.filename); //the name of the file as it should appear after it's uploaded
attachment.setContentType(this.contentType);
//connect out to the SSH server
gs.print("Connecting to SSH server.");
ssh.connect(properties, ignore);
var result = ssh.authenticate(pwd);
gs.print("Connect code? " + result);
// Evaluate the result
if (result == 4) { //The connection is open and authenticated
//get the file via SCP
gs.print("Opening SftpClient.");
var sftp = ssh.openSftpClient();
gs.print("Changing directory.");
sftp.cd(this.path);
gs.print("Current directory: " + sftp.pwd());
gs.print("Current contents of this directory:");
var files = sftp.ls();
for(var i=0; i<files.size(); i++) {
file = files.get(i);
gs.print(file.getFilename());
}
gs.print("Trying to download file.");
try {
sftp.get(this.filename, output);
//delete the existing attachments
gs.print("Deleting existing attachments.");
var gr = new GlideRecord('sys_data_source');
gr.addQuery('sys_id', this.dataSourceID);
gr.query();
if (gr.next()) {
attachment.deleteAll(gr);
}
//set up input stream
gs.print("Setting up input stream.");
var input = Packages.java.io.ByteArrayInputStream(output.toByteArray());
//save the new attachment
gs.print("Saving new attachment.");
attachment.setInputStream(input);
attachment.write();
} catch (e) {
gs.print("File download failed. Disconnecting.");
ssh.disconnect();
return false;
}
// gs.print("File downloaded.");
// return; //returning here- if we can't even make it to this line, the attachment stuff is irrelevant
//close the connection
gs.print("Disconnecting.");
ssh.disconnect();
} else {
gs.print("Could not connect to SSH server.");
return false;
}
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment