Skip to content

ScpClient: Make -d flag optional or avoid it for better compatibility with Cisco SCP servers #1746

Closed

Description

@MicaelLysen

Hi, and thanks for SSH.NET 👋

I’m using SSH.NET 2025.1.0 (via NuGet) and ran into the same Cisco SCP issue as described in #829 (“Scp on cisco device not working”).

On Cisco devices, the -d option used by ScpClient for uploads is not supported by their SCP implementation. In #829 this shows up as errors like:

-p -d options not supported

When I look at the 2025.1.0 source for ScpClient, I still see scp -t -d ... being used for uploads.

Example usage:

using var scp = new ScpClient(host, username, password);
scp.RemotePathTransformation = RemotePathTransformation.None;
scp.Connect();

using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
scp.Upload(stream, "startup-config");

This works fine with OpenSSH servers but fails against Cisco devices due to the -d flag.

Request / proposal

For better interoperability, it would be helpful if ScpClient:

either removed -d for file uploads and just used

scp -t

or exposed a property to disable -d, e.g.:

public bool UseDirectoryFlag { get; set; } = true;

and then:

var target = _remotePathTransformation.Transform(UseDirectoryFlag ? posixPath.Directory : posixPath.File);
var exec = UseDirectoryFlag
    ? $"scp -t -d {target}"
    : $"scp -t {target}";
if (!channel.SendExecRequest(exec))
{
    throw SecureExecutionRequestRejectedException();
}

That would keep current behavior for OpenSSH, but allow users to work with Cisco and other limited SCP implementations without maintaining a custom fork.

Happy to help test any change against Cisco devices if needed.

Activity

  1. mus65 commented on Dec 22, 2025

    @mus65
    Contributor

    In the scp implementation this flag is automatically set if argc > 2 .

    https://github.com/openssh/openssh-portable/blob/09daf2ac5f248dc5d60a6f3a703b479d67da14b4/scp.c#L667-L668

    Since argc includes the program name and SSH.NET passes -t and target, this should always be true. So unless I'm missing something, the -d flag doesn't actually do anything, at least not with the OpenSSH server.

    So we might as well remove it? I will open a PR.

  2. mus65 commented on Dec 22, 2025

    @mus65
    Contributor

    I indeed did miss something. 😄

    https://github.com/openssh/openssh-portable/blob/09daf2ac5f248dc5d60a6f3a703b479d67da14b4/scp.c#L624

    I guess that means argc only includes the file/directory arguments.

    There are indeed multiple test failures when we remove -d, so I guess there must be an opt-in for this. see #1751

  3. Rob-Hague commented on Dec 23, 2025

    @Rob-Hague
    Collaborator

    Would be fine with a switch in principle, but given our inability to easily test it (i.e. the failures against OpenSSH), it would probably require some work on your end @MicaelLysen to prove that it works as expected.

  4. MicaelLysen commented on Dec 26, 2025

    @MicaelLysen
    Author

    I am definitely able to help out.
    I implemented this lazy solution for myself:

    public void Upload(FileInfo fileInfo, string path, bool useDirectoryFlag = true)
    {
        ArgumentNullException.ThrowIfNull(fileInfo);
        if (Session is null)
        {
            throw new SshConnectionException("Client not connected.");
        }
        var posixPath = PosixPath.CreateAbsoluteOrRelativeFilePath(path);
        using (var input = ServiceFactory.CreatePipeStream())
        using (var channel = Session.CreateChannelSession())
        {
            channel.DataReceived += (sender, e) => input.Write(e.Data.Array!, e.Data.Offset, e.Data.Count);
            channel.Closed += (sender, e) => input.Dispose();
            channel.Open();
            var target = _remotePathTransformation.Transform(useDirectoryFlag ? posixPath.Directory : posixPath.File);
            var exec = useDirectoryFlag
                ? $"scp -t -d {target}"
                : $"scp -t {target}";
            if (!channel.SendExecRequest(exec))
            {
                throw SecureExecutionRequestRejectedException();
            }
            CheckReturnCode(input);
            using (var source = fileInfo.OpenRead())
            {
                UploadTimes(channel, input, fileInfo);
                UploadFileModeAndName(channel, input, source.Length, posixPath.File);
                UploadFileContent(channel, input, source, fileInfo.Name);
            }
        }
    }

    This works on our Cisco devices—both routers and switches—across different IOS versions. I’ve tested it on about 10 different devices.

  5. added a commit that references this issue on Mar 21, 2026
    f9fd818
  6. added a commit that references this issue on Mar 22, 2026
    25a931c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions