File Transfer from ServiceNow to SFTP Server Not Working.

_gaurav_
Tera Contributor

Hello,
I'm attempting an SFTP file transfer from ServiceNow to an SFTP server from scratch. I've followed the steps outlined in this article (Article Link), but I'm encountering the following error: 'The Transport Protocol thread failed java.io.IOException: The socket is EOF.' Can someone please help me resolve this issue?

Script Include Code:

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.OutputStraem = Packages.java.io.OutputStream;
        this.BufferedOutputStream = Packages.java.io.BufferedOutputStream;
        this.Thread = Packages.java.lang.Thread;

        this.targetServer = 'hostname';
        this.targetPort = 22;
        this.targetUsername = 'username';
        this.targetPassword = 'password';
        this.deleteAfterUpload = probe.getParameter("deleteAfterUpload");
        this.targetPath = probe.getParameter("targetPath");
        this.MIDSERVER_FILE_PATH = probe.getParameter("midServerTempPath");
        this.MIDSERVER_FILE_NAME = probe.getParameter("fileName");
    },


    fileTransfer: function() {

        try {
            var localFileName = this.MIDSERVER_FILE_PATH + "/" + this.MIDSERVER_FILE_NAME;
            var remoteFileName = this.targetPath + '/' + this.MIDSERVER_FILE_NAME;
            this.log("Copying from local file of MID Server: " + localFileName);
            this.sftpFile(this.targetServer, this.targetUsername, this.targetPassword, localFileName, remoteFileName);
        } catch (e) {
            this.log("Error in writing file to SFTP server: " + e);
        }

    },


    sftpFile: function(hostName, userName, password, localFileName, remoteFileName) {
        var shortDescription = '';
        var description = '';

        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);

        try {
            ssh.connect(hostName, this.targetPort, ignoreHost);
            this.log("Connected to the host successfully");
        } catch (e) {
            this.log('Connection failed to the host ' + e);
            shortDescription = 'Connection failed to the host';
            description = 'An error occurred while attempting to connect to the server: ' + hostName + ' with an error: \n\n' + e;
            //this.createIncident(shortDescription, description);
            this.log("Created an Incident for the error encountered");
            return; // return if connection fails to the host
        }

        pwd = new Packages.com.sshtools.j2ssh.authentication.PasswordAuthenticationClient();
        var authPassword = new Packages.com.glide.util.Encrypter().decrypt(password);
        pwd.setUsername(userName);
        pwd.setPassword(authPassword);

        // Get full path of filename

        this.log('sftpFile(): attempting to copy ' + localFileName + ' to ' + remoteFileName);
        try {
            if (ssh.authenticate(pwd) == new Packages.com.sshtools.j2ssh.authentication.AuthenticationProtocolState().COMPLETE) {
                try {
                    sftp = ssh.openSftpClient();
                    this.log('Connection Established to the client');
                } catch (e) {
                    this.log('Unable to connect to the client ' + e);
                    shortDescription = 'Unable to connect to the client';
                    description = 'An error occurred while establishing a connection to the client with an error: \n\n' + e;
                    //this.createIncident(shortDescription, description);
                    this.log("Created an Incident for the error encountered");
                    return; // return if unable to connect to the client
                }
                try {
                    sftp.put(localFileName, remoteFileName);
                    this.log("File successfully copied to target 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);
                    shortDescription = 'File not found error';
                    description = 'An error occurred while attempting to copy file from MID server: ' + localFileName + ' to target path of the SFTP server ' + remoteFileName + ' with an error: \n\n' + e;
                    //this.createIncident(shortDescription, description);
                    this.log("Created an Incident for the error encountered");
                    return; // return if file not found or error occurs
                }
                sftp.quit();

                try {
                    // kill connection
                    ssh.disconnect();
                } catch (e) {
                    this.log('Manual connection kill not successful with error: ' + e);
                }
            }
        } catch (e) {
            this.log('User authentication Failed ' + e);
            shortDescription = 'User authentication';
        }
    },

    log: function(data) {
        ms.log(data);
    },

    type: SftpFile
};



5 REPLIES 5

Maik Skoddow
Tera Patron
Tera Patron

Hi @_gaurav_ 

did you open a console on your MID server and have you tried establishing a sftp connection there to make sure your MID server can reach the target server and is not blocked on the network level?

Maik

Hello Maik,

Thank you for your response. I haven't had the chance to verify the connection at the console level, but I did attempt an SFTP operation using the SFTP Step in IntegrationHUB, and it worked flawlessly.

Hello Maik,

Thank you for your response. I haven't had the chance to verify the connection at the console level, but I did attempt an SFTP operation using the SFTP Step in IntegrationHUB, and it worked flawlessly.

 

VivekSattanatha
Mega Sage
Mega Sage

Hi,

 

The article you mentioned was from the Geneva version. If it is working through IntegrationHub then you better use those Integration hub actions rather than this custom implementation. The IntegrationHub actions can be called by script. What would be the reason you are going through this custom implementation?