Table of Contents

File transfer (SCP)

using SshSharp.Scp; adds SCP transfers to SshClient. They run the server's scp program (as scp -t to receive, scp -f to send), so they work wherever scp does, including servers without SFTP.

Files

using SshSharp.Scp;

await client.UploadFileAsync("report.csv", "/srv/reports/2024-05.csv");
await client.DownloadFileAsync("/var/log/app.log", "app.log");

// An existing directory as the target keeps the other side's file name.
await client.UploadFileAsync("report.csv", "/srv/reports/");
await client.DownloadFileAsync("/var/log/app.log", Path.GetTempPath());

Streams

await using var source = File.OpenRead("backup.tar");
await client.UploadAsync(source, "/backups/backup.tar");     // the length comes from the stream, or pass length: for one that cannot seek

using var memory = new MemoryStream();
await client.DownloadAsync("/etc/os-release", memory);

Directories

// Like scp -r: the directory becomes the target when that does not exist, or goes inside it when it does.
await client.UploadDirectoryAsync("site", "/var/www/site", new SshScpOptions { Preserve = true });
await client.DownloadDirectoryAsync("/var/log/app", "logs");

Options and progress

var options = new SshScpOptions
{
    Preserve = true,                                 // scp -p: modification and access times, and permission bits
    FileMode = UnixFileMode.UserRead | UnixFileMode.UserWrite, // 0600, for uploads whose mode is not preserved
    Progress = new Progress<SshScpProgress>(p => Console.Write($"\r{p.Path}: {p.BytesTransferred}/{p.TotalBytes}")),
};
await client.UploadFileAsync("secrets.env", "/etc/app/secrets.env", options, cancellationToken);

Errors and safety

A failed transfer (a missing file, permission denied, a server without scp) throws an SshException with SshFailureReason.TransferFailed and the server's own message.

SCP runs scp on the server through the server's shell, and the remote path is part of that command. By default SshSharp passes it as one single-quoted word, so spaces and special characters are safe with the shells sshd uses on Unix-like servers (sh, bash, zsh, fish, tcsh).

SshScpOptions.PathQuoting (1.3) writes it for other servers: None sends it unquoted, for network devices (Cisco IOS, MikroTik RouterOS) that read the line themselves, and DoubleQuote double-quotes it, for OpenSSH for Windows with cmd.exe. Both refuse, with ArgumentException and before anything is sent, a path they cannot carry safely. See Network devices and Windows servers.

// Cisco IOS: "scp -f nvram:startup-config", no quotes.
await router.DownloadFileAsync("nvram:startup-config", "startup-config.txt", new SshScpOptions { PathQuoting = SshScpPathQuoting.None });

// OpenSSH for Windows with cmd.exe: scp -t -- "C:\Reports\May 2024\report.csv".
await windows.UploadFileAsync("report.csv", @"C:\Reports\May 2024\report.csv", new SshScpOptions { PathQuoting = SshScpPathQuoting.DoubleQuote });
Warning

Windows OpenSSH servers run commands through cmd.exe by default, or PowerShell when configured so, and neither reads the default quoting as a POSIX shell does. cmd.exe does not treat single quotes as quotes at all, so a path with & in it runs what follows as a command. PowerShell does, but escapes a quote inside them differently, so a path with a ' in it can end the quoted word and run a command. Either runs as the logged-in user. On such a server set PathQuoting = SshScpPathQuoting.DoubleQuote, which refuses the characters either shell would act on, and pass only remote paths you control. For a path that comes from user input or another untrusted source, use SFTP, which sends paths as data and runs no shell.

When downloading, every name the server sends must be a single path component (no /, \, :, ., .. or control characters), and a single-file download accepts exactly one file: a hostile server cannot write outside the target or slip in extra files (CVE-2019-6111). Permission bits from the server are applied without set-id or sticky bits, and only with Preserve.

Tracing and metrics

Each UploadFileAsync, UploadAsync, DownloadFileAsync and DownloadAsync is a span (SshSharp.Scp.Upload or SshSharp.Scp.Download) tagged with the bytes it moved, and each directory transfer is one (SshSharp.Scp.UploadDirectory or SshSharp.Scp.DownloadDirectory) tagged with its bytes, files and directories. The sshsharp.scp.bytes metric counts the file data as it moves, by direction. Paths are never recorded: add them to a span of your own if your policy allows. See Tracing and metrics.

See also