Remote copy file - SFTP

0 votes
asked Jan 11, 2011 by Pavol (120 points)
edited Sep 13, 2011

Hello, I trying your .NET library. When I connected on SFTP server, i need copy files from server to another directory on the server (this directory is mapped disk array). Is it possible? Have you any idea? Thanx a lot.

Applies to: Rebex SFTP

1 Answer

+2 votes
answered Jan 11, 2011 by Lukas Pokorny (100,250 points)
edited Sep 13, 2011

Although this operation (copy a file located at the server to another directory at the server) is not supported by the SFTP protocol (and Rebex SFTP), it is often possible to achieve it by utilizing the fact that SFTP runs over SSH. This makes it possible to use SSH's "remote execute" feature to run arbitrary commands at the server (if it was configured to allow that, of course).

To give it a try, use the following class:


public class SftpCommandRunner
    public static string RunCommand(Sftp sftp, string command)
        SshChannel channel = null;
            // start an exec session
            channel = sftp.Session.OpenSession();

            // receive all response
            StringBuilder response = new StringBuilder();
            byte[] buffer = new byte[4096];
            while (channel.State == SshChannelState.Connected)
                if (!channel.Poll(sftp.Timeout * 1000, SocketSelectMode.SelectRead))

                int n = channel.Receive(buffer, 0, buffer.Length);
                response.Append(Encoding.Default.GetString(buffer, 0, n));

            return response.ToString().TrimEnd();
            if (channel != null)


Public Class SftpCommandRunner
    Public Shared Function RunCommand(ByVal sftp As Sftp, ByVal command As String) As String
        Dim channel As SshChannel = Nothing
            ''# start an exec session
            channel = sftp.Session.OpenSession()

            ''# receive all response
            Dim response As New StringBuilder()
            Dim buffer As Byte() = New Byte(4095) {}
            While channel.State = SshChannelState.Connected
                If Not channel.Poll(sftp.Timeout * 1000, SocketSelectMode.SelectRead) Then
                    Exit While
                End If

                Dim n As Integer = channel.Receive(buffer, 0, buffer.Length)
                response.Append(Encoding.[Default].GetString(buffer, 0, n))
            End While

            Return response.ToString().TrimEnd()
            If channel IsNot Nothing Then
            End If
        End Try
    End Function
End Class

To copy a file located at the server to another directory, establish an SFTP connection and call SftpCommandRunner.RunCommand, supplying your Sftp objecet and a command to it. For Unix-like OS, the command would be "cp source_path destination_path". For Windows OS, use "copy" instead of "cp".

Does this work for you?