This project is read-only.

Documentation & Samples

If you find this project interesting, go to Home and click the star in the box to the right. When you click it, it should turn green, if not - it looks like this:

Documentation

So here you are looking for documentation. Unfortunately, this project is formally in beta, and we do not have the time to always update the sample code to reflect new changes. Or add samle code for new features for that matter. But here I'm going to show you some code samples of the typical uses of this library.

Samples

If for any reason a sample does not work with a release, make sure you have the latest release, or go to Source Code and you can download the absolute latest code. No binaries though.

NOTE: These samples will be based of our unit tests. You can find them under Source Code -> Renci.SshClient -> Renci.SshNet.Tests.

  • SFTP Client (file transfer)
    • List Directory
    • Upload File
    • Download File
  • SSH Client
    • Local port forward

SFTP Client (file transfer)

List directory

/// <summary>
/// This sample will list the contents of the current directory.
/// </summary>
public void ListDirectory()
{
    string host            = "";
    string username        = "";
    string password        = "";
    string remoteDirectory = "."; // . always refers to the current directory.
    
    using (var sftp = new SftpClient(host, username, password))
    {
        var files = sftp.ListDirectory(remoteDirectory);
        foreach (var file in files)
        {
            Console.WriteLine(file.FullName);
        }
    }
}

Upload File

/// <summary>
/// This sample will upload a file on your local machine to the remote system.
/// </summary>
public void UploadFile()
{
    string host           = "";
    string username       = "";
    string password       = "";
    string localFileName  = "";
    string remoteFileName = System.IO.Path.GetFileName(localFile);

    using (var sftp = new SftpClient(host, username, password))
    {
        sftp.Connect();

        using (var file = File.OpenRead(localFileName))
        {
            sftp.UploadFile(remoteFileName, file);
        }

        sftp.Disconnect();
    }
}

Download File

/// <summary>
/// This sample will download a file on the remote system to your local machine.
/// </summary>
public void DownloadFile()
{
    string host           = "";
    string username       = "";
    string password       = "";
    string localFileName  = System.IO.Path.GetFileName(localFile);
    string remoteFileName = "";

    using (var sftp = new SftpClient(host, username, password))
    {
        sftp.Connect();

        using (var file = File.OpenWrite(localFileName))
        {
            sftp.DownloadFile(remoteFileName, file);
        }

        sftp.Disconnect();
    }
}

SSH Client

Local port forward

/// <summary>
/// This sample will open a port on your local machine, and allow connections to be forwarded
/// to the specified remote host:port.
/// </summary>
public void LocalPortForward()
{
    string host            = "";
    string username        = "";
    string password        = "";

    int localPort          = 8084;

    string hostToForwardTo = "www.google.com";
    int portToForwardTo    = 80;

    using (var client = new SshClient(host, username, password))
    {
        client.Connect();

        var tunnel = client.AddForwardedPort<ForwardedPortLocal>("localhost", localPort, hostToForwardTo, portToForwardTo);

        tunnel.Exception += new EventHandler<ExceptionEventArgs>(this.OnTunnelError);
        tunnel.Start();

        // You can now connect to localhost on port set in localPort.
        System.Console.ReadKey();
    }
}

/// <summary>
/// This method will be called when an error has occurred.
/// </summary>
/// <param name="sender">Typicall it's the SshClient instance.</param>
/// <param name="e">Information about the exception.</param>
public void OnTunnelError(object sender, ExceptionEventArgs e)
{
    // An error must be handled.
    System.Diagnostics.Debugger.Break();
}

Last edited Jul 12, 2011 at 8:09 AM by Kenneth_aa, version 9

Comments

burnproof21 Sep 22, 2014 at 5:16 PM 
Upload file wont work, it takes in a Stream and String, the args are reversed in this example. This documentation needs to be a bit better.

azharmahmood Sep 16, 2014 at 8:17 PM 
LocalPortForward method above won't compile, tried different things but no luck.

Howi Oct 23, 2013 at 4:00 PM 
there is an error in the upload example. it should be sftp.UploadFile(file,Remotefilename);

at least in the version i downloaded.

gustavosuarez4 Sep 30, 2013 at 4:27 PM 
In the 'List directory' example there's a
sftp.Connect();
missing right before
var files = sftp.ListDirectory(remoteDirectory);

tux007 Mar 12, 2013 at 2:28 AM 
Well, your Connection is not established.
I had the same problem. Maybe it the remote Host does only accept key interactive authentification, or key based authentification.

tcboring Mar 7, 2013 at 4:05 PM 
Using the "Download File" sample above, I get this exception:


[SshAuthenticationException: No suitable authentication method found to complete authentication.]
Renci.SshNet.ConnectionInfo.Authenticate(Session session) +760
Renci.SshNet.Session.Connect() +1474
Renci.SshNet.BaseClient.Connect() +270
gov.sbcounty.gis.GESecurity.Services.TestSSHNet.TestSSHNetButton_Click(Object sender, EventArgs e) in d:\ISD-VM-PTFS02\ADCollection2\T0250 ISD Google Earth Security\Source\TRUNK-DEV\ISD Google Earth Security Services\TestSSHNet.aspx.cs:33
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804


I'm using the latest build of SSH.NET, through a NuGet Reference.