using System; using System.IO; using System.Text; using LeafWeb.Core.Utility; using Renci.SshNet; namespace LeafWeb.Core.Remote { public interface IPiscalClient { void SubmitLeafInputFile(PiscalLeafInputFile file); void RetrieveLeafInputResult(PiscalLeafInputFile file); } public class PiscalSshClient : IPiscalClient { private const string BaseDirectory = "LeafInput"; private readonly PasswordConnectionInfo _connectionInfo; public PiscalSshClient(string connectionString) { var conn = connectionString.SplitConnectionString(); var host = conn["host"]; var username = conn["username"]; var password = conn["password"]; _connectionInfo = new PasswordConnectionInfo(host, username, password); } private SshClient GetSshClient() { return new SshClient(_connectionInfo); } private ScpClient GetScpClient() { return new ScpClient(_connectionInfo); } public void SubmitLeafInputFile(PiscalLeafInputFile file) { var inputPath = BaseDirectory + "/" + file.DirectoryName + "/" + file.Filename; var outputPath = BaseDirectory + "/" + file.DirectoryName + "/" + file.Filename; using (var scp = GetScpClient()) using (var stream = new MemoryStream(file.Contents)) { scp.Connect(); scp.Upload(stream, inputPath); } using (var ssh = GetSshClient()) { ssh.Connect(); var lsCommend = ssh.CreateCommand("ls -r"); lsCommend.Execute(); //var extendedData = Encoding.ASCII.GetString(cmd.ExtendedOutputStream.ToArray()); var extendedData = new StreamReader(lsCommend.ExtendedOutputStream, Encoding.ASCII).ReadToEnd(); ssh.Disconnect(); Console.Write(lsCommend.Result); Console.Write(extendedData); //Assert.AreEqual("12345\n", cmd.Result); //Assert.AreEqual("654321\n", extendedData); } // copy file // begin processing } public void RetrieveLeafInputResult(PiscalLeafInputFile file) { using (var scp = GetScpClient()) using (var stream = new MemoryStream(file.Contents)) { scp.Connect(); scp.Upload(stream, BaseDirectory + "/" + file.DirectoryName + "/" + file.Filename); } } } }