Guest User

Untitled

a guest
Jun 26th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net.Sockets;
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. using Renci.SshNet;
  7. using Renci.SshNet.Common;
  8.  
  9. namespace SSH_Sandbox
  10. {
  11. public struct SftpReport
  12. {
  13. public string hostname;
  14. public int port;
  15. public string directory;
  16. public string username;
  17. public string password;
  18. public string fileLocation;
  19.  
  20. public SftpReport(string host, int port, string dir,string uName, string passwd, string fileLoc)
  21. {
  22. this.hostname = host;
  23. this.port = port;
  24. this.directory = dir;
  25. this.username = uName;
  26. this.password = passwd;
  27. this.fileLocation = fileLoc;
  28. }
  29. }
  30.  
  31.  
  32. class Program
  33. {
  34. static void Main(string[] args)
  35. {
  36. List<SftpReport> reports = GenerateTestReports();
  37.  
  38. foreach (var report in reports)
  39. {
  40. Task.Run(()=> UploadFileToSftpClient(report));
  41. }
  42.  
  43. Console.WriteLine("Application end.");
  44. Console.Read();
  45. }
  46.  
  47. static List<SftpReport> GenerateTestReports()
  48. {
  49. List<SftpReport> reports = new List<SftpReport>();
  50.  
  51. reports.Add(new SftpReport("localhost", 22, "/upload/jamie", "foo", "pass", @"c:/Test/sftp.txt"));
  52.  
  53. return reports;
  54. }
  55.  
  56. static async Task UploadFileToSftpClient(SftpReport report)
  57. {
  58. var connInfo = new ConnectionInfo(report.hostname, report.port, report.username, new PasswordAuthenticationMethod(report.username, report.password));
  59. using (var client = new SftpClient(connInfo))
  60. {
  61. try
  62. {
  63. client.Connect();
  64. SendFile(client, report.directory, report.fileLocation);
  65. }
  66. catch (SocketException sEx)
  67. {
  68. Console.WriteLine($"{client.ConnectionInfo.ServerVersion ?? "Unknown server"} - Error connecting to remote host. Message : {sEx.Message}");
  69. }
  70. catch (SshAuthenticationException sshEx)
  71. {
  72. Console.WriteLine($"{client.ConnectionInfo.ServerVersion ?? "Unknown server"} - Invalid credentials, cannot authenticate. Message : {sshEx.Message}");
  73. }
  74. catch (SftpPermissionDeniedException pdEx)
  75. {
  76. Console.WriteLine($"{client.ConnectionInfo.ServerVersion ?? "Unknown server"} - Permission denied. Message : {pdEx.Message}");
  77. }
  78. catch (Exception ex)
  79. {
  80. Console.WriteLine($"An unexpected error has occured: {ex.GetType()} - {ex.Message}");
  81. }
  82. }
  83. }
  84.  
  85. static void SendFile(SftpClient client, string directory, string file)
  86. {
  87. if (!DirectoryExists(client, directory))
  88. {
  89. client.CreateDirectory(directory);
  90. }
  91. client.ChangeDirectory(directory);
  92.  
  93. TransferFileToServer(client, file);
  94. }
  95.  
  96. static bool DirectoryExists(SftpClient client, string directory)
  97. {
  98. try
  99. {
  100. client.ListDirectory(directory);
  101. return true;
  102. }
  103. catch (SftpPathNotFoundException ex)
  104. {
  105. return false;
  106. }
  107. }
  108.  
  109. static void TransferFileToServer(SftpClient client, string filePath)
  110. {
  111. Random rand = new Random();
  112.  
  113. using (var fileStream = new FileStream(filePath, FileMode.Open))
  114. {
  115. client.BufferSize = 4 * 1024;
  116. client.UploadFile(fileStream, $"Report {rand.Next()}.xml");
  117. }
  118. }
  119. }
  120. }
Add Comment
Please, Sign In to add comment