Guest User

Untitled

a guest
May 16th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. namespace ListDirectory
  5. {
  6. class Program
  7. {
  8. // This sample program is listup remote home directory.
  9.  
  10.  
  11.  
  12. // Requirements:
  13. // you need to install SSH.NET from nuget.
  14. //
  15. // PM> Install-Package SSH.NET
  16. //
  17. // This sample is executed under 'SSH.NET.2016.1.0'
  18.  
  19. static void Main(string[] args)
  20. {
  21. var remote_hostname = "sftp.server.example.com";
  22. var remote_port = 10022;
  23. var remote_username = "sftpuser";
  24. var remote_password = "sftppass";
  25.  
  26. //
  27. //
  28. //
  29. //var ci = GetConnectionInfo_Direct(remote_hostname, remote_port, remote_username, remote_password);
  30. var ci = GetConnectionInfo_Socks5(remote_hostname, remote_port, remote_username, remote_password);
  31.  
  32.  
  33. ListDirectory(ci);
  34. }
  35.  
  36.  
  37. static Renci.SshNet.ConnectionInfo GetConnectionInfo_Direct(String remote_hostname, Int32 remote_port, String remote_username, String remote_password)
  38. {
  39. // password authentication.
  40. var authMethod = new Renci.SshNet.PasswordAuthenticationMethod(remote_username, remote_password);
  41.  
  42. // make a connection info.
  43. var connectionInfo = new Renci.SshNet.ConnectionInfo(remote_hostname, remote_port, remote_username, authMethod);
  44.  
  45. return connectionInfo;
  46. }
  47.  
  48. static Renci.SshNet.ConnectionInfo GetConnectionInfo_Socks5(String remote_hostname, Int32 remote_port, String remote_username, String remote_password)
  49. {
  50.  
  51. var socks5_hostname = "192.168.0.2";
  52. var socks5_port = 1080;
  53. var socks5_username = "socks5user";
  54. var socks5_password = "socks5pass";
  55.  
  56.  
  57.  
  58. // password authentication.
  59. var authMethod = new Renci.SshNet.PasswordAuthenticationMethod(remote_username, remote_password);
  60.  
  61.  
  62. //
  63. // THAT'S THE POINT!!
  64. //
  65. // detour the BUG (using #174 pull request technique)
  66. //
  67. {
  68. var portBytes = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder((short)remote_port));
  69. remote_port = portBytes[0] * 0xFF + portBytes[1];
  70. }
  71.  
  72.  
  73. // make a connection info.
  74. 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);
  75.  
  76.  
  77. return connectionInfo;
  78. }
  79.  
  80.  
  81. /// <summary>
  82. /// List home directory.
  83. /// </summary>
  84. /// <param name="connectionInfo"></param>
  85. static void ListDirectory(Renci.SshNet.ConnectionInfo connectionInfo)
  86. {
  87. using (var client = new Renci.SshNet.SftpClient(connectionInfo))
  88. {
  89. client.Connect();
  90.  
  91. foreach (var file in client.ListDirectory("."))
  92. {
  93. Console.WriteLine(">> {0}", file.Name);
  94. }
  95.  
  96. client.Disconnect();
  97. }
  98. }
  99. }
  100. }
Add Comment
Please, Sign In to add comment