SFTP file transfer issue from MID Server
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2025 10:28 PM
Hello I have created a post Export Script and Script Include to transfer the files from Midserver to an SFTP but its not working, can anyone help in debugging.
I checked few posts but unable to fix issue.
Solved: Push .csv file from mid server to an SFTP location - ServiceNow Community
Using export sets for SFTP file transfer - ServiceNow Community
Post Export Script
var jprobe = new JavascriptProbe("midserver");
jprobe.setName("sftp File Transfer");
jprobe.setJavascript('new SftpFile().FileTransfer();');
jprobe.addParameter("targetPath", "/");
jprobe.addParameter("fileName", "test.csv");
jprobe.addParameter("midServerPath", "test");
jprobe.addParameter("skip_sensor", true);
jprobe.create();
Script Include:
var SftpFile = Class.create();
SftpFile.prototype = {
initialize: function() {
this.Properties = Packages.java.util.Properties;
this.StringUtil = Packages.com.glide.util.StringUtil;
this.BufferedWriter = Packages.java.io.BufferedWriter;
this.File = Packages.java.io.File;
this.FileWriter = Packages.java.io.FileWriter;
this.Encrypter = new Packages.com.glide.util.Encrypter();
this.FileOutputStream = Packages.java.io.FileOutputStream;
this.FileInputStream = Packages.java.io.FileInputStream;
this.BufferedReader = Packages.java.io.BufferedReader;
this.InputStreamReader = Packages.java.io.InputStreamReader;
this.OutputStream = Packages.java.io.OutputStream;
this.BufferedOutputStream = Packages.java.io.BufferedOutputStream;
this.Thread = Packages.java.lang.Thread;
this.targetServer = 'test.com';
this.targetPort =22 ;
this.targetUsername = 'test';
this.targetPassword = 'testpw';
//this.deleteAfterUpload= probe.getParameter("deleteAfterUpload");
this.targetPath = probe.getParameter("targetPath");
this.MIDSERVER_FILE_PATH = probe.getParameter("midServerPath");
this.MIDSERVER_FILE_NAME = probe.getParameter("fileName");
},
FileTransfer: function() {
try {
var localFileName = this.MIDSERVER_FILE_PATH + "/" + this.MIDSERVER_FILE_NAME;
this.log("Copying from local file of MID Server: " + localFileName);
this.SftpFile(this.targetServer, this.targetUsername, this.targetPassword, localFileName, this.targetPath + this.MIDSERVER_FILE_NAME);
} catch (e) {
this.log("Error in writing file to SFTP server: " + e);
}
},
SftpFile : function(hostName, userName, password, localFileName, remoteFileName) {
this.log('SftpFile(): attempting to connect to ' + hostName);
var ssh = new Packages.com.sshtools.j2ssh.SshClient();
var ignoreHost = new Packages.com.sshtools.j2ssh.transport.IgnoreHostKeyVerification();
if (!this.targetPort){
this.targetPort = 22;
}
this.log('SftpFile(): attempting to connect to ' + hostName + " on port " + this.targetPort);
ssh.connect(hostName, this.targetPort, ignoreHost);
pwd.setUsername(userName);
pwd.setPassword(password);
// Get full path of filename
this.log('SftpFile(): attempting to copy ' + localFileName + ' to ' + remoteFileName);
if(ssh.authenticate(pwd) == new Packages.com.sshtools.j2ssh.authentication.AuthenticationProtocolState().COMPLETE) {
sftp = ssh.openSftpClient();
try {
sftp.put(localFileName, remoteFileName);
this.log("File successfully copied to targert path\n\n");
if (this.deleteAfterUpload == "true") {
this.log("deleteAfterUpload -> " + this.deleteAfterUpload + ", deleting local file...");
new this.File(localFileName)["delete"]();
}
}
catch(e) {
this.log('FILE NOT FOUND ' + remoteFileName + ' or error: ' + e);
}
sftp.quit();
try{
// kill connection
ssh.disconnect();
}
catch(e){
this.log('Manual connection kill not successful with error: ' + e);
}
}
},
log: function(data) {
ms.log(data);
},
type: 'SftpFile'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2025 10:58 PM
this is very custom solution.
What debugging did you perform? you already added logs, please grab the mid server logs and check
Are you able to reach to that SFTP server from mid server?
Are you able to connect to that SFTP server?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2025 11:07 PM
I am able yo connect SFTP server from Mid Server, and SFTP connection is working manually, but the script include is not executing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2025 11:09 PM
unless you debug the code 1 by 1 it won't help.
things to check
1) is the file transferred to that path from where mid server is picking and sending to SFTP
2) check respective logs i.e. connection successful etc
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader