how to sent custom command using sftp?

0 votes
asked Mar 25, 2015 by ganeshspy (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
answered Mar 25, 2015 by Tomas Knopp (58,890 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.

commented Sep 20, 2016 by Prabhakar (390 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
commented Sep 20, 2016 by Pavel Matyska (10,800 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.
commented Sep 21, 2016 by Prabhakar (390 points)
Thanks. Just wanted to double-check: SshChannel::Receive() is a blocking call. Pls confirm.
commented Sep 22, 2016 by Pavel Matyska (10,800 points)
Yes, SshChannel.Receive() is blocking operation.
...