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.

Operation denied while uploading the file to SFTP Server.

hameetkaur
Tera Contributor

Hi All,
We are implementing an integration to upload the data to a SFTP server. Export sets are used to export the data to MID Server and Post script under scheduled data export is being used to then send the file to SFTP server.
Further, post script is calling MID Server Script include to execute the above.
Below is the MID Server Script include :

var SFTP = Class.create();
SFTP.prototype = {
    initialize: function() {
        this.File = Packages.java.io.File;

        // Correct SSHJ packages
        this.SSHClient = Packages.net.schmizz.sshj.SSHClient;
        this.FileSystemFile = Packages.net.schmizz.sshj.xfer.FileSystemFile;
        this.PromiscuousVerifier = Packages.net.schmizz.sshj.transport.verification.PromiscuousVerifier;

        // Parameters from probe input
        this.targetHost = probe.getParameter('host'); // hostname or IP
        this.targetPort = probe.getParameter('port');
        this.targetUser = probe.getParameter('user');
        this.targetPass = probe.getParameter('password');
        this.targetFile = probe.getParameter('target') + probe.getParameter('filename'); // remote path
        this.sourceFile = probe.getParameter('source') + probe.getParameter('filename'); // local path
        this.deleteFile = probe.getParameter('deletefile');
        this.direction = probe.getParameter('direction'); // "upload" or "download"
    },

    fileTransfer: function() {
        try {
            this._sftp();
            if (this.deleteFile && this.direction === "upload") {
                this._deleteFile();
            }
        } catch (ex) {
            ms.log('SFTP Error: ' + ex);
        }
    },

    _deleteFile: function() {
        try {
            // Must use ["delete"] to avoid conflict with JS "delete" keyword
            new this.File(this.sourceFile)["delete"]();
            ms.log("Local file deleted: " + this.sourceFile);
        } catch (ex) {
            throw 'SFTP Delete: ' + ex;
        }
    },

    _sftp: function() {
        ms.log('SFTP Connect: ' + this.targetUser + '@' + this.targetHost + ':' + this.targetPort);
        // var ssh = new this.SSHClient();
        var ssh = new Packages.net.schmizz.sshj.SSHClient();
        try {
            ssh.addHostKeyVerifier(new this.PromiscuousVerifier());

            //  Use Packages.java instead of java
            ssh.connect(this.targetHost, Packages.java.lang.Integer.parseInt(this.targetPort));
            ssh.authPassword(this.targetUser, this.targetPass);

            if (this.direction === "download") {
                this._download(ssh);
            } else {
                this._upload(ssh);
            }
        } catch (ex) {
            throw 'SFTP Error: ' + ex;
        } finally {
            try {
                ssh.disconnect();
            } catch (ignore) {}
        }
    },

    _upload: function(ssh) {
        var sftp = null;
        try {
            sftp = ssh.newSFTPClient();
            sftp.put(new this.FileSystemFile(this.sourceFile), this.targetFile);

            ms.log('SFTP Upload: ' + this.sourceFile + ' → ' +
                this.targetHost + ':' + this.targetFile);
        } catch (ex) {
            throw 'SFTP Upload: ' + ex;
        } finally {
            if (sftp) {
                try {
                    sftp.close();
                } catch (ignore) {}
            }
        }
    },

    _download: function(ssh) {
        var sftp = null;
        try {
            sftp = ssh.newSFTPClient();
            sftp.get(this.targetFile, new this.FileSystemFile(this.sourceFile));

            ms.log('SFTP Download: ' + this.targetHost + ':' + this.targetFile +
                ' → ' + this.sourceFile);
        } catch (ex) {
            throw 'SFTP Download: ' + ex;
        } finally {
            if (sftp) {
                try {
                    sftp.close();
                } catch (ignore) {}
            }
        }
    },

    type: 'SFTP'
};

After execution below is the error we are getting : 
*** Script: SFTP Error: SFTP Error: SFTP Upload: net.schmizz.sshj.sftp.SFTPException: Operation not supported.
Below is the URL related to this implementation : How to use SNCSSH in MID Server Script Include 
@M Iftikhar Can you please help.

1 REPLY 1

M Iftikhar
Tera Sage

HI @hameetkaur,

The error net.schmizz.sshj.sftp.SFTPException: Operation not supported is typically related to the put operation and how the path is specified.

  • Log the paths - Before the upload, log both sourceFile and targetFile to confirm they are exactly what you expect:
ms.log('Source File: ' + this.sourceFile);
ms.log('Target File: ' + this.targetFile);
  • Target path issue - In the put function, targetFile should ideally be the directory path on the remote server, not a file path including the filename. So in initialize, try setting this.targetFile to the remote directory path only:
this.targetFile = probe.getParameter('target'); // remote directory
  • Ensure proper permissions - Verify the SFTP user has write permissions to the target directory.

After making these adjustments, test the upload again. Logging the exact paths will help confirm if the variables are as expected. And please share your findings.

 

If my response helped, please mark it as the accepted solution 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.