Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Getting Error while pushing .csv file to SFTP location

hameetkaur
Tera Contributor

Hi,

I have came across a requirement where I need to post a .csv file to my mid server and push to an SFTP location.

My SFTP location uses a username + password authentication method .

For that I am using the export set, and try to call a MID Server Script Include through a JavaScript probe, but that does not seem to be working. Could any one please help?

var SFTP = Class.create();
SFTP.prototype = {
    initialize: function() {
        this.File = Packages.java.io.File;
        this.J2ssh = Packages.com.sshtools.j2ssh;
        this.targetHost = probe.getParameter('host'); //hostname or IP address of the SFTP server
        this.targetPort = probe.getParameter('port');
        this.targetUser = probe.getParameter('user');
        this.targetPassword = probe.getParameter('password');
        this.targetFile = probe.getParameter('target') + probe.getParameter('filename');
        this.sourceFile = probe.getParameter('source') + probe.getParameter('filename');
        this.deleteFile = probe.getParameter('deletefile');
    },
    fileTransfer: function() {
        try {
            this._sftp();
            if (this.deleteFile) {
                this._deleteFile();
            }
        } catch (ex) {
            ms.log('SFTP Error: ' + ex);
        }
    },
    _authenticate: function(ssh) {
        var passwordAuth = new this.J2ssh.authentication.PasswordAuthenticationClient();
        passwordAuth.setUsername(this.targetUser);
        passwordAuth.setPassword(this.targetPass);
        var result = ssh.authenticate(passwordAuth);
        if (result != new this.J2ssh.authentication.AuthenticationProtocolState().COMPLETE) {
            throw 'SFTP Authenticate Failed: ' + this.targetUser + '@' + this.targetHost + ':' + this.targetPort;
        }
    },
    _deleteFile: function() {
        try {
            new this.File(this.sourceFile)['delete']();
        } catch (ex) {
            throw 'SFTP Delete: ' + ex;
        }
    },
    _sftp: function() {
        ms.log('SFTP Connect: ' + this.targetUser + '@' + this.targetHost + ':' + this.targetPort);
        var ssh = new this.J2ssh.SshClient();
        try {
            ssh.connect(this.targetHost, this.targetPort, new this.J2ssh.transport.IgnoreHostKeyVerification());
            this._authenticate(ssh);
            this._transfer(ssh);
        } catch (ex) {
            throw 'SFTP Error: ' + ex;
        } finally {
            ssh.disconnect();
        }
    },
    _transfer: function(ssh) {
        try {
            sftp = ssh.openSftpClient();
            sftp.put(this.sourceFile, this.targetFile);
            ms.log('SFTP Transfer: ' + this.sourceFile + ' copied to ' + this.targetHost + ':' + this.targetPort);
        } catch (ex) {
            throw 'SFTP Transfer: ' + ex;
        } finally {
            sftp.quit();
        }
    }
};

While running this I got below error message :
The Transport Protocol thread failed
java.io.IOException: The socket is EOF at com.sshtools.j2ssh.transport.TransportProtocolInputStream.readBufferedData(TransportProtocolInputStream.java:187)

while running the below command  on MID server, I got below error:

ssh -vvv <user>@<host>


debug1: kex: algorithm: curve25519-sha256

debug1: kex: host key algorithm: rsa-sha2-512


Please note that  I am using 'j2ssh' (Java Library)  in my MID Server Script Include.




0 REPLIES 0