71 lines
1.7 KiB
C#
71 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using LeafWeb.Core.Entities;
|
|
using Renci.SshNet;
|
|
|
|
namespace LeafWeb.Core
|
|
{
|
|
public interface IPiscalClient
|
|
{
|
|
void SubmitLeafInputFile(LeafInputFile file);
|
|
void RetrieveLeafInputResult();
|
|
}
|
|
|
|
public class PiscalSshClient : IPiscalClient
|
|
{
|
|
private readonly Dictionary<string, string> _conn;
|
|
|
|
public PiscalSshClient(string connectionString)
|
|
{
|
|
_conn = connectionString.Split(';')
|
|
.Select(t => t.Split(new [] { '=' }, 2))
|
|
.ToDictionary(t => t[0].Trim(), t => t[1].Trim(), StringComparer.InvariantCultureIgnoreCase);
|
|
}
|
|
|
|
private SshClient GetSshClient()
|
|
{
|
|
return new SshClient(_conn["host"], _conn["username"], _conn["password"]);
|
|
}
|
|
private ScpClient GetScpClient()
|
|
{
|
|
return new ScpClient(_conn["host"], _conn["username"], _conn["password"]);
|
|
}
|
|
|
|
public void SubmitLeafInputFile(LeafInputFile file)
|
|
{
|
|
using (var scp = GetScpClient())
|
|
using (var stream = new MemoryStream(file.Contents))
|
|
{
|
|
scp.Connect();
|
|
scp.Upload(stream, file.Filename);
|
|
}
|
|
|
|
using (var ssh = GetSshClient())
|
|
{
|
|
ssh.Connect();
|
|
var lsCommend = ssh.CreateCommand("ls");
|
|
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()
|
|
{
|
|
throw new System.NotImplementedException();
|
|
}
|
|
}
|
|
}
|