Last active
June 4, 2024 15:37
-
-
Save paulsena/10120453 to your computer and use it in GitHub Desktop.
ServiceNow SFTP / SSH Javascript Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
How can we send files to FTP server instead? Without using midservers or plugins. Is there a posibility to integrate an external library in Snow to achieve this? Thanks
Have you tried the sftp.put() command? I assume this should work when you provide a file name and a attachment content stream
It shows the following error:
*** Script: Setting up SSH client.
*** Script: undefined
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you tried the sftp.put() command? I assume this should work when you provide a file name and a attachment content stream