string hostName = ConfigurationManager.AppSettings["HostName"];
string userName = ConfigurationManager.AppSettings["UserName"];
string password = ConfigurationManager.AppSettings["Password"];
string SKey = ConfigurationManager.AppSettings["SshHostKeyFingerprint"];
string kex = ConfigurationManager.AppSettings["KEX"];
string sourcePath = ConfigurationManager.AppSettings["FromPath"];
string extenName = ConfigurationManager.AppSettings["FileName"];
string hostPath = ConfigurationManager.AppSettings["ToPath"];
try
{
// 配置会话选项
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Scp,
HostName = hostName,
UserName = userName,
Password = password,
SshHostKeyFingerprint = SKey,
};
sessionOptions.AddRawSettings("KEX", kex);
using (Session session = new Session())
{
session.Open(sessionOptions);
var commandResult = session.ExecuteCommand("cd " + sourcePath);
commandResult = session.ExecuteCommand("ls");
if (commandResult.IsSuccess)
{
var fileNames = commandResult.Output.Split(‘\n‘).Where(fileName => fileName.EndsWith(extenName)).ToList();
if (!Directory.Exists(hostPath))
{
Directory.CreateDirectory(hostPath);
}
foreach (var fileName in fileNames)
{
var result = session.GetFiles("/" + sourcePath + "/" + fileName, hostPath);
//Console.WriteLine(string.Format("FileName:{0},文件传输{1}!", fileName, result.IsSuccess ? "成功" : "失败"));
}
}
}
// Console.WriteLine("完成");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}