Suitescript 2.0 SFTP Tool

Suitescript 2.0 SFTP Tool

One of the new features of SuiteScript API 2016.2 is the ability to connect to SFTP Servers directly from Netsuite.

Here are some downsides I found:

  • Not way to list the contents of a remote directory <– This has been added
  • No way of connecting using a private key. <– This has been added
  • Not all SFTP servers are supported as Netsuite requires the server to support some encryption algorithms.
  • No way of obtaining the server’s hostkey natively.

 

Unfortunately the process is a bit confusing if you are new to the SFTP world so I created a tool that will help you jump-start your script. I found this tool very useful as I was able to test an SFTP connection quickly for a client and turned out the SFTP server that they were trying to use didn’t actually support Netsuite’s approved algorithms (aes256-ctr, es192-ctr, es128-ctr), so it saved me a lot of time as I was able to quickly discard it and move on.

I also included an endpoint to get the HostKey of the URL you are trying to connect to. I think this will be very useful for people who don’t have a Linux server lying around where they can run ssh keyscan.

Note: The HostKey endpoint should be used to grab the hostkey and store it elsewhere on your end. Do not use the endpoint to request the hostkey every time you make a request. If your server HostKey changes very frequently then I suggest to build your own tool.

Here’s a video tutorial:

https://www.youtube.com/watch?v=azxo7GS3xRQ

 

And here’s the code ready to be uploaded as a Suitelet, have fun!:

  • Babin Kanakaraj

    Hi Adolfo,

    I have used the tool to return the password GUID and hostkey. It works fine, but when using the same in my script, its throwing “Could not establish connection to No more authentication methods available” error. I tried to connect through filezilla and looks the credentials are working fine. Do you have any suggestions on this. Thanks in advance.

    • Sorry, I don’t have anything

      • Babin Kanakaraj

        No problem, I found the issue is with “Restrict To Domains” field value. And I fixed this.

  • Agustín

    Wonderful thanks bro !!
    I will try it

  • Wellyntong Breton

    Can somebody help me find out why is this code :
    //****************************************************************************
    //** Connects to the sftp site and exports the Stock Status CSV
    //** files from the File Cabinet
    //****************************************************************************
    /**
    *@NApiVersion 2.x
    *@NScriptType ScheduledScript
    */
    define(["N/file", 'N/sftp'], function(file, sftp) {
    function execute (context){
    var password = "password";
    var username = "user";
    var passwordGuid = "Generated GUID";
    var url = "sftpsite.com";
    var hostKey = "SFTPSITEHOSTKEY";
    var hostKeyType = "rsa";
    var port = "22";
    var location = "/DIRECTORY THAT I CAN ACCESS VIA WINSCP AND USING CONNECTION.DOWNLOAD";
    var timeout = 0;
    var sftpConnection = getSFTPConnection(username, passwordGuid, url, hostKey, hostKeyType, port, location, timeout);
    var fileObj = file.load({
    id: 1740980
    });
    try {
    sftpConnection.upload({
    directory: location,
    file: fileObj,
    replaceExisting: true
    });
    }
    catch (e){
    log.debug("Error on try", e)
    }
    }
    function getSFTPConnection(username, passwordGuid, url, hostKey, hostKeyType, port, location, timeout){
    var preConnectionObj = {};
    preConnectionObj.passwordGuid = passwordGuid;
    preConnectionObj.url = url;
    preConnectionObj.hostKey = hostKey;
    if(username){ preConnectionObj.username = username; }
    if(hostKeyType){ preConnectionObj.hostKeyType = hostKeyType; }
    if(port){ preConnectionObj.port = Number(port); }
    if(location){ preConnectionObj.directory = location; }
    if(timeout){ preConnectionObj.timeout = Number(timeout); }
    var connectionObj = sftp.createConnection(preConnectionObj);
    return connectionObj;
    }
    return{
    execute: execute
    };
    });

    …is failing with this error:

    View Debug Error on try 10/17/2023 8:30 am -System- {“type”:”error.SuiteScriptError”,”name”:”UNEXPECTED_ERROR”,”message”:null,”id”:”e5171d9a-0ada-48d8-b40c-b13b3cb8f431-2d323032332e31302e3137″,”stack”:[“anonymous(N/file)”,”execute(/SuiteScripts/TSM Scripts/TSM – Export Inventory Stock File.js:25)”],”cause”:{“type”:”internal error”,”code”:”UNEXPECTED_ERROR”,”details”:null,”userEvent”:null,”stackTrace”:[“anonymous(N/file)”,”execute(/SuiteScripts/TSM Scripts/TSM – Export Inventory Stock File.js:25)”],”notifyOff”:false},”notifyOff”:false,”userFacing”:false}