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

Hello @VivekSattanatha ,

Thank you for your response. I have opted for a custom approach as it aligns with our client's specific requirements, and they haven't subscribed to IntegrationHub Professional either.