0 votes
by (900 points)

I want to send custom command using sftp, for communicating to server.
same like as ftp.

Applies to: Rebex SFTP

1 Answer

+1 vote
by (58.9k points)

Sending remote commands can be done via SshChannel.RequestExec methods:

// open an SSH session channel over a connected SFTP client
SshChannel channel = sftp.Session.OpenSession();

// execute the 'uname' command to get OS info
channel.RequestExec("uname -a");

// process the command response
var response = new StringBuilder();
var buffer = new byte[4096];
while (true)
{
    int n = channel.Receive(buffer, 0, buffer.Length);
    if (n == 0)
    {
        channel.Close();
        break;
    }

    response.Append(Encoding.Default.GetString(buffer, 0, n));
}
Console.WriteLine("OS info: {0}", response);

You can learn more at https://www.rebex.net/sftp.net/features/ssh.aspx#remote-exec

For more complex scenarios, e.g. scripting an SSH terminal, I recommend our Terminal Emulation component that provides a dedicated Scripting API.

Rebex Terminal Emulation can be purchased together with Rebex SFTP for a discounted price as Rebex SSH Pack.

by (630 points)
RequestExec() is for executing standard commands. How do I send custom commands from my SFTP client?  I can see that SFTP server supports custom commands. http://www.rebex.net/file-server/features/ssh.aspx#custom-commands
by (15.2k points)
You can use RequestExec() for any command. If you have implemented a custom command  on your server using the link you posted, you can use that command in RequestExec() method the same way as Tomas did above.
by (630 points)
Thanks. Just wanted to double-check: SshChannel::Receive() is a blocking call. Pls confirm.
by (15.2k points)
Yes, SshChannel.Receive() is blocking operation.
...