Created
May 11, 2018 10:43
-
-
Save frkwtto/45033ac6ae93104d9c2f36a2420ee1c9 to your computer and use it in GitHub Desktop.
SSH.NET sftp example with socks5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ListDirectory | |
{ | |
class Program | |
{ | |
// This sample program is listup remote home directory. | |
// Requirements: | |
// you need to install SSH.NET from nuget. | |
// | |
// PM> Install-Package SSH.NET | |
// | |
// This sample is executed under 'SSH.NET.2016.1.0' | |
static void Main(string[] args) | |
{ | |
var remote_hostname = "sftp.server.example.com"; | |
var remote_port = 10022; | |
var remote_username = "sftpuser"; | |
var remote_password = "sftppass"; | |
// | |
// | |
// | |
//var ci = GetConnectionInfo_Direct(remote_hostname, remote_port, remote_username, remote_password); | |
var ci = GetConnectionInfo_Socks5(remote_hostname, remote_port, remote_username, remote_password); | |
ListDirectory(ci); | |
} | |
static Renci.SshNet.ConnectionInfo GetConnectionInfo_Direct(String remote_hostname, Int32 remote_port, String remote_username, String remote_password) | |
{ | |
// password authentication. | |
var authMethod = new Renci.SshNet.PasswordAuthenticationMethod(remote_username, remote_password); | |
// make a connection info. | |
var connectionInfo = new Renci.SshNet.ConnectionInfo(remote_hostname, remote_port, remote_username, authMethod); | |
return connectionInfo; | |
} | |
static Renci.SshNet.ConnectionInfo GetConnectionInfo_Socks5(String remote_hostname, Int32 remote_port, String remote_username, String remote_password) | |
{ | |
var socks5_hostname = "192.168.0.2"; | |
var socks5_port = 1080; | |
var socks5_username = "socks5user"; | |
var socks5_password = "socks5pass"; | |
// password authentication. | |
var authMethod = new Renci.SshNet.PasswordAuthenticationMethod(remote_username, remote_password); | |
// | |
// THAT'S THE POINT!! | |
// | |
// detour the BUG (using #174 pull request technique) | |
// | |
{ | |
var portBytes = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder((short)remote_port)); | |
remote_port = portBytes[0] * 0xFF + portBytes[1]; | |
} | |
// make a connection info. | |
var connectionInfo = new Renci.SshNet.ConnectionInfo(remote_hostname, remote_port, remote_username, Renci.SshNet.ProxyTypes.Socks5, socks5_hostname, socks5_port, socks5_username, socks5_password, authMethod); | |
return connectionInfo; | |
} | |
/// <summary> | |
/// List home directory. | |
/// </summary> | |
/// <param name="connectionInfo"></param> | |
static void ListDirectory(Renci.SshNet.ConnectionInfo connectionInfo) | |
{ | |
using (var client = new Renci.SshNet.SftpClient(connectionInfo)) | |
{ | |
client.Connect(); | |
foreach (var file in client.ListDirectory(".")) | |
{ | |
Console.WriteLine(">> {0}", file.Name); | |
} | |
client.Disconnect(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment