Connection Sharing between SSH SCP and SFTP

Jun 18, 2014 at 6:13 AM
Edited Jun 18, 2014 at 6:28 AM
Hi,

Is there a current method to share a SSH connection between SshClient ScpClient and SFTPClient?

I couldn't find an obvious method so resolved the issue by modifying BaseClient and ScpClient as follows,

BaseClient.cs
/// <summary>
/// Gets current session.
/// </summary>
public Session Session { get; protected set; }
ScpClient.cs
public ScpClient(Session establishedSession) : this (establishedSession.ConnectionInfo)
{
    base.Session = establishedSession;
}
To test the modifications I created the following application,

TestProgram.cs
var sshClient = new SshClient(host, username, password);

sshClient.Connect();
var scpClient = new ScpClient(sshClient.Session);

Console.WriteLine("SCP connect? " + scpClient.IsConnected);
FileInfo fileInfo = new FileInfo(@"C:\mybin\temp.exe");
scpClient.Upload(fileInfo, "/home/temp/test/temp.exe");

scpClient.Disconnect();
Thoughts? The TestProgram works fine.
Jun 18, 2014 at 6:47 AM
In addition to using the SshClient to create the session, I have modified Session.cs as follows,

Session.cs
public Session(ConnectionInfo connectionInfo)
{
    this.ConnectionInfo = connectionInfo;
    //this.ClientVersion = string.Format(CultureInfo.CurrentCulture, "SSH-2.0-Renci.SshNet.SshClient.{0}", this.GetType().Assembly.GetName().Version);
    this.ClientVersion = string.Format(CultureInfo.CurrentCulture, "SSH-2.0-Renci.SshNet.SshClient.0.0.1");
}
Which meant the test program could look like this,

TestProgram.cs
using(var session = new Session(new PasswordConnectionInfo(host, username, password)))
{
    session.Connect();
    var scpClient = new ScpClient(session);
    scpClient.Uploading += scpClient_Uploading;
    Console.WriteLine("SCP connect? " + scpClient.IsConnected);
    FileInfo fileInfo = new FileInfo(@"C:\mybin\temp.exe");
    scpClient.Upload(fileInfo, "/home/temp/test/temp.exe");


    scpClient = new ScpClient(session);

    scpClient.Uploading += scpClient_Uploading;
    Console.WriteLine("SCP connect? " + scpClient.IsConnected);
    fileInfo = new FileInfo(@"C:\mybin\temp.exe");
    scpClient.Upload(fileInfo, "/home/temp/test/temp2.exe");
}