The CreatorCon Call for Content is officially open! Get started here.

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

M Iftikhar
Giga Sage

Hi @hameetkaur

 

New and recommended approach is to use ServiceNow's IntegrationHub with the SFTP Spoke, which replaces the need for custom scripts and manual library management.

 

While SNCSSH is ServiceNow's internal SSH library, it is not designed for direct use in custom MID Server Script Includes. Instead of trying to use it, you should leverage the platform's built-in capabilities:

  1. Recommended Solution: IntegrationHub SFTP Spoke: This is the best practice. It provides out-of-the-box actions like "Put File" that handle the connection, authentication (using credentials stored securely in the instance), and file transfer in a robust, upgrade-safe manner without writing any code.

  2. Alternative (If IntegrationHub is not available): If you must use a custom script, you should use a modern, industry-standard Java library like Apache MINA SSHD or SSHJ. To do this, you would need to add the library's JAR file to your MID Server's /agent/lib directory, restart the MID Server, and then rewrite your MID Server Script Include to use the new library's methods.

For official guidance, please see the following references:

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 
Thank you for your response. Can you please provide some more info regarding alternative approach(we don't have integration hub). 
What are the criteria on the basis of which we should select Apache MINA SSHD or SSHJ.
Can you also provide me the links to download the JAR files for Apache MINA SSHD and SSHJ. How do we identify which version is compatible.

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.

Hi @M Iftikhar 
If we follow alternate approach of using IntegrationHub. Can you please let me know what exact spoke to be used. From the store, i was able to find below plugin:

hameetkaur_0-1758607399448.png

 

Please confirm if this is the correct plugin to be used.