1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
using System;
using System.Collections;
using Tamir.SharpSsh;
namespace cls_SSH
{
/// <summary>
/// Effectue un transfère de fichier en SSH.
/// </summary>
public class SshFileTransfer
{
public enum _publickey { n, y };
public enum _DirectionTransfere { to, from };
public enum _Protocol { scp, sftp };
/// <summary>
/// Effectue un transfère de fichier en SSH.
/// </summary>
/// <param name="host">Hôte (Ex: 192.168.1.10)</param>
/// <param name="user">Nom Utilisateur</param>
/// <param name="Password">Mot de passe</param>
/// <param name="identity_key">Clé d'identification (Non obligatoire si identification par mot de passe)</param>
/// <param name="FichierLocal">Fichier local (ex: M:/Toto.txt)</param>
/// <param name="FichierDistant">Fichier Distant (ex: UnDossier/Toto.txt)</param>
/// <param name="Protocol">Choix du protocol</param>
/// <param name="publickey">Choix de l'identification</param>
/// <param name="DirectionTransfere">Choix de la direction du tranfère</param>
public void Transfert(string host,string user,string Password,string identity_key,string FichierLocal,string FichierDistant,_Protocol Protocol,_publickey publickey, _DirectionTransfere DirectionTransfere)
{
try
{
SshConnectionInfo info = new SshConnectionInfo();
info.Host = host;
info.User = user;
if (publickey == _publickey.y)
{
info.IdentityFile = identity_key;
}
else
{
info.Pass = Password;
}
SshTransferProtocolBase sshCp;
if (Protocol==_Protocol.scp)
sshCp = new Scp(info.Host, info.User);
else
sshCp = new Sftp(info.Host, info.User);
sshCp.Password = info.Pass;
if (info.Pass != null) sshCp.Password = info.Pass;
if (info.IdentityFile != null) sshCp.AddIdentityFile(info.IdentityFile);
sshCp.OnTransferStart += new FileTransferEvent(sshCp_OnTransferStart);
sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
sshCp.OnTransferEnd += new FileTransferEvent(sshCp_OnTransferEnd);
sshCp.Connect();
if (DirectionTransfere==_DirectionTransfere.to)
sshCp.Put( FichierLocal, FichierDistant);
else
sshCp.Get(FichierLocal, FichierDistant);
sshCp.Close();
}
catch (Exception ex)
{
throw ex;
}
}
public struct SshConnectionInfo
{
public string Host;
public string User;
public string Pass;
public string IdentityFile;
}
//public static string GetArg(string msg)
//{
// Console.Write(msg + ": ");
// return Console.ReadLine();
//}
static Tamir.SharpSsh.jsch.examples.ConsoleProgressBar progressBar;
private static void sshCp_OnTransferStart(string src, string dst, int transferredBytes, int totalBytes, string message)
{
Console.WriteLine();
progressBar = new Tamir.SharpSsh.jsch.examples.ConsoleProgressBar();
progressBar.Update(transferredBytes, totalBytes, message);
}
private static void sshCp_OnTransferProgress(string src, string dst, int transferredBytes, int totalBytes, string message)
{
if (progressBar != null)
{
progressBar.Update(transferredBytes, totalBytes, message);
}
}
private static void sshCp_OnTransferEnd(string src, string dst, int transferredBytes, int totalBytes, string message)
{
if (progressBar != null)
{
progressBar.Update(transferredBytes, totalBytes, message);
progressBar = null;
}
}
}
} |
Partager