Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
606 views
in FAQ: CompleteFTP by (200 points)
var ftp = new Ftp();

//Create variable for local directory (3.)
var Folder = "/Home/UsersHomeFolder/OtherFolder/outgoing/";
system.currentFolder = Folder;

try {
    ftp.connect();
} catch(e){
    console.log("connection error " + e);
}
try {
    //Create a directory to contain files
    var outgoing = new File(system.currentFolder);
 
    //Assign files in outgoing to a variable
    var files = outgoing.getFiles();
     
    //Iterate through list of files
    for(var i in files) { 
        //Get fullpath of current file 
        var oFile = files[i].fullPath;
        //Get file name from fullpath
        var fileName = oFile.replace(/^.*[\\\/]/, '');
        if(files[i].isFile){
            console.log("File Name: " + fileName);
        if(fileName == "ftpCommand.txt") { //This file has the shell command I //want to send to server
            //The method I want to execute is below
            //scp.executeCommand(files[i].readText());
        }   
    }
}

1 Answer

0 votes
by (51.1k points)

Well, yes, sort of.  SecureFileTransferClient is a Java class, which won't work, but you can use SecureFTPConnection, which is the main class for our .NET FTP component, edtFTPnet/PRO.  Below I've included a JSS sample, which I just tested as the JSS script for a process trigger.  Please also refer to the edtFTPnet/PRO User Guide and Examples.  I'm not aware of anyone else doing this before, but, as far as I can see, it should work fine.

try {
    var ftp = new EnterpriseDT.Net.Ftp.SecureFTPConnection();
    ftp.ServerAddress = "myhostname";
    ftp.UserName = "myusername";
    ftp.Password = "mypassword";
    ftp.Connect();
    ftp.Close();
    console.log("FTP OK");
} catch (e) {
    console.error(e.toString());
}

Please be aware that, although you're accessing a .NET class, the code is actually Javascript as evidenced by the syntax in the catch statement.

SecureFTPConnection has three methods similar to executeCommand, namely InvokeCommandSSHInvokeFTPCommand and InvokeSiteCommand.  I'm not sure, which one you need, but I think one of them should do what you need.

by (200 points)
Thanks for the quick response. I have looked into the InvokeCommandSSH and the other but I am unsure how to properly use them. I want to read the shell command from a text file I have stored on a server and those methods don't seem to allow you put a string as an argument to execute the function
(i.e ftp.InvokeCommandSSH(commandScript.txt.readText())).
by (51.1k points)
I just added the following line and it executed the command fine:

ftp.Connect();
ftp.InvokeCommandSSH("hello");
ftp.Close();

Of course, I had set the protocol to SFTP first, as follows:

ftp.Protocol = EnterpriseDT.Net.Ftp.FileTransferProtocol.SFTP;

since InvokeCommandSSH only works over SSH.  I also had to configure the remote server, also CompleteFTP, to accept SSH terminal connections (for the site and the user), add a custom JSS command, hello, as follows:

function hello() {
    console.log("hello");
}

and make sure my user had permission to execute the command (Permissions tab).  Once I did all that I was able to see "Javascript (log): hello" in the log when the event triggered.

I don't know anything about the server you're connecting to or the command you're trying to execute.  Without those I can't help you beyond saying that it is possible for it to work.

What error are you seeing?
by (51.1k points)
I just noticed that you've posted another question with more information.  I'm deleting that question and answering it here.

I changed my code as follows:

var returnValue = ftp.InvokeCommandSSH("ls -al");
console.log(returnValue);

This also worked fine, logging the directory listing.

What server are you connecting to?  Is it another CompleteFTP server?
...