Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Net.Sockets;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Renci.SshNet;
- using Renci.SshNet.Common;
- namespace SSH_Sandbox
- {
- public struct SftpReport
- {
- public string hostname;
- public int port;
- public string directory;
- public string username;
- public string password;
- public string fileLocation;
- public SftpReport(string host, int port, string dir,string uName, string passwd, string fileLoc)
- {
- this.hostname = host;
- this.port = port;
- this.directory = dir;
- this.username = uName;
- this.password = passwd;
- this.fileLocation = fileLoc;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- List<SftpReport> reports = GenerateTestReports();
- foreach (var report in reports)
- {
- Task.Run(()=> UploadFileToSftpClient(report));
- }
- Console.WriteLine("Application end.");
- Console.Read();
- }
- static List<SftpReport> GenerateTestReports()
- {
- List<SftpReport> reports = new List<SftpReport>();
- reports.Add(new SftpReport("localhost", 22, "/upload/jamie", "foo", "pass", @"c:/Test/sftp.txt"));
- return reports;
- }
- static async Task UploadFileToSftpClient(SftpReport report)
- {
- var connInfo = new ConnectionInfo(report.hostname, report.port, report.username, new PasswordAuthenticationMethod(report.username, report.password));
- using (var client = new SftpClient(connInfo))
- {
- try
- {
- client.Connect();
- SendFile(client, report.directory, report.fileLocation);
- }
- catch (SocketException sEx)
- {
- Console.WriteLine($"{client.ConnectionInfo.ServerVersion ?? "Unknown server"} - Error connecting to remote host. Message : {sEx.Message}");
- }
- catch (SshAuthenticationException sshEx)
- {
- Console.WriteLine($"{client.ConnectionInfo.ServerVersion ?? "Unknown server"} - Invalid credentials, cannot authenticate. Message : {sshEx.Message}");
- }
- catch (SftpPermissionDeniedException pdEx)
- {
- Console.WriteLine($"{client.ConnectionInfo.ServerVersion ?? "Unknown server"} - Permission denied. Message : {pdEx.Message}");
- }
- catch (Exception ex)
- {
- Console.WriteLine($"An unexpected error has occured: {ex.GetType()} - {ex.Message}");
- }
- }
- }
- static void SendFile(SftpClient client, string directory, string file)
- {
- if (!DirectoryExists(client, directory))
- {
- client.CreateDirectory(directory);
- }
- client.ChangeDirectory(directory);
- TransferFileToServer(client, file);
- }
- static bool DirectoryExists(SftpClient client, string directory)
- {
- try
- {
- client.ListDirectory(directory);
- return true;
- }
- catch (SftpPathNotFoundException ex)
- {
- return false;
- }
- }
- static void TransferFileToServer(SftpClient client, string filePath)
- {
- Random rand = new Random();
- using (var fileStream = new FileStream(filePath, FileMode.Open))
- {
- client.BufferSize = 4 * 1024;
- client.UploadFile(fileStream, $"Report {rand.Next()}.xml");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment