Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment