More .NET libraries
-
Rebex FTP
.NET FTP client
-
Rebex SSH Shell
.NET SSH Shell
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
SCP
On this page:
SCP is a legacy file transfer protocol, or rather a command-line program, a secure analog of rcp
that runs over a secure SSH channel.
SCP has a very limited feature set and only supports file transfer.
In most cases, using SFTP is a better choice. However, SCP is still useful with legacy SSH servers that don't support SFTP yet.
Connecting and authenticating
Connecting and authenticating with Scp
object is the same as with Sftp
object.
For more information and sample code, see Connecting and
Proxies sections.
To connect to a server running on default port 22, use Connect
method. Once connected, don't forget to
check the server's fingerprint
as a security measure.
// create SCP client instance using (Scp scp = new Rebex.Net.Scp()) { // connect scp.Connect(hostname); // check scp.Fingerprint here // authenticate scp.Login(username, password); // upload or download files // ... // disconnect (not required by most servers, but polite) scp.Disconnect(); }
' create SCP client instance Using scp As New Rebex.Net.Scp ' connect to a server scp.Connect(hostname) ' check scp.Fingerprint here ' authenticate scp.Login(username, password) ' upload or download files ' ... ' disconnect (not required by most servers, but polite) scp.Disconnect() End Using
To connect to a server running on non-standard port, just pass the port number to the Connect
method as an additional argument.
Uploading or downloading a single file
Use PutFile
method to upload a single file, and GetFile
method to download it.
Both methods require a target name, which means it's possible to use a different target file name if needed.
There is no concept of a current directory in SCP. If the specified remote path is a not an absolute path, the user's home directory at the remote server will be used as base directory.
// upload "file.txt" file to user's home directory at the server scp.PutFile(@"C:\MyData\file.txt", "file.txt"); // upload "file.txt" file to "/MyData" at the server as "data.txt" scp.PutFile(@"C:\MyData\file.txt", "/MyData/data.txt"); // download "/MyData/data.txt" file from the server scp.GetFile("/MyData/data.txt", @"C:\MyData\data.txt");
' upload "file.txt" file to user's home directory at the server scp.PutFile("C:\MyData\file.txt", "file.txt") ' upload "file.txt" file to "/MyData" at the server as "data.txt" scp.PutFile("C:\MyData\file.txt", "/MyData/data.txt") ' download "/MyData/data.txt" file from the server scp.GetFile("/MyData/data.txt", "C:\MyData\data.txt")
Stream-based upload/download methods are available as well. They are equivalent to their SFTP counterparts.
Uploading or downloading multiple files
To transfer a directory with multiple files or subdirectories to/from an SSH server, use Download
and Upload
methods.
The target directory is created automatically if it doesn't already exist.
// upload content of "MyData" directory to "/MyData" directory at the server scp.Upload(@"C:\MyData\*", "/MyData"); // download content of "MyData" directory from the server to a local directory scp.Download("/MyData/*", @"C:\MyData");
' upload content of "MyData" directory to "/MyData" directory at the server scp.Upload("C:\MyData\*", "/MyData") ' download content of "MyData" directory from the server to a local directory scp.Download("/MyData/*", "C:\MyData")
Progress reporting
Scp
provides two progress-reporting events: TransferProgress
for single-file operations, and BatchTransferProgress
for multiple-file operations.
Sample multi-file transfer progress event handler:
void scp_BatchTransferProgress(object sender, ScpBatchTransferProgressEventArgs e) { if (e.Operation == ScpBatchTransferOperation.FileTransferStarting) { switch (e.State) { case ScpTransferState.Downloading: Console.WriteLine("Downloading {0} -> {1}", e.RemotePath, e.LocalPath); break; case ScpTransferState.Uploading: Console.WriteLine("Uploading {0} -> {1}", e.LocalPath, e.RemotePath); break; } } }
Sub scp_BatchTransferProgress(ByVal sender As Object, ByVal e As ScpBatchTransferProgressEventArgs) If e.Operation = ScpBatchTransferOperation.FileTransferStarting Then Select Case e.State Case ScpTransferState.Downloading Console.WriteLine("Downloading {0} -> {1}", e.RemotePath, e.LocalPath) Case ScpTransferState.Uploading Console.WriteLine("Uploading {0} -> {1}", e.LocalPath, e.RemotePath) End Select End If End Sub
Registering the event handler:
// register BatchTransferProgress event handler scp.BatchTransferProgress += scp_BatchTransferProgress; // transfer files // ...
' register BatchTransferProgress event handler AddHandler scp.BatchTransferProgress, AddressOf scp_BatchTransferProgress ' transfer files ' ...
Back to feature list...