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.

How to use SNCSSH in MID Server Script Include

hameetkaur
Tera Contributor

Hi,
I have been using J2ssh library in mid server script include. However, the SFTP server no longer supports this encryption algorithm. I want to upgrade to SNCSSH. Can anyone let me know how to proceed with this.
The requirement is to push the CSV file from MID Server to target SFTP Server.

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

Please let me know if the requirement can be achieved through any other way.

1 ACCEPTED SOLUTION

Sure @hameetkaur

 

For SSHJ:

For Apache MINA SSHD:

  • Apache MINA SSHD Project: https://mina.apache.org/sshd-project/downloads.html

  • You will typically need to download the binary distribution (.zip or .tar.gz) which contains all the necessary JARs in its lib folder, including sshd-core, sshd-sftp, and its dependencies like slf4j-api. This is often easier than finding each JAR individua

Implementation steps:

  1. Stop the MID Server service.

  2. Copy all the required JAR files (e.g., sshj-0.38.0.jar, slf4j-api-2.0.12.jar, slf4j-simple-2.0.12.jar) into the agent/lib directory of your MID Server.

  3. Start the MID Server service.

  4. Create a new MID Server Script Include and use the Java classes from the library to write your SFTP logic. You will use Packages.com.hierynomus.sshj... to access the SSHJ classes.

Note: The library version you choose must be compatible with the Java version used by your MID Server. Recent ServiceNow releases (e.g., Vancouver, Washington DC) typically use Java 11. You should verify this by checking the java -version command in your MID Server's agent/jre/bin directory. Select the latest library version that supports your MID Server's Java version.

 

Hope this cleared all the things now!

 

Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution and helpful so others can benefit as well.

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

View solution in original post

6 REPLIES 6

Hi @hameetkaur

 

The "Procurement File Transfer Framework" is a specialized, application-specific tool and is not the correct one for your general requirement.

The spoke you need is the SFTP Spoke, which is a core component of ServiceNow's IntegrationHub. It is not a separate store app but is included with an IntegrationHub subscription (typically IH-Starter or higher).

 

Hope this helps!

 

Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution and helpful so others can benefit as well.

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

Hi @M Iftikhar 
I installed ServiceNow IntegratationHub Professional Pack starter in my PDI. However, i am not able to see SFTP Spoke option in the flow designer.

hameetkaur_0-1758632859455.png