00001 using System;
00002 using System.Collections;
00003 using Tamir.SharpSsh;
00004
00005 namespace sharpSshTest.sharpssh_samples
00006 {
00011 public class SshFileTransferTest
00012 {
00013 public static void RunExample()
00014 {
00015 try
00016 {
00017 SshConnectionInfo input = Util.GetInput();
00018 string proto = GetProtocol();
00019 SshTransferProtocolBase sshCp;
00020
00021 if(proto.Equals("scp"))
00022 sshCp = new Scp(input.Host, input.User);
00023 else
00024 sshCp = new Sftp(input.Host, input.User);
00025
00026 if(input.Pass != null) sshCp.Password = input.Pass;
00027 if(input.IdentityFile != null) sshCp.AddIdentityFile( input.IdentityFile );
00028 sshCp.OnTransferStart += new FileTransferEvent(sshCp_OnTransferStart);
00029 sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
00030 sshCp.OnTransferEnd += new FileTransferEvent(sshCp_OnTransferEnd);
00031
00032 Console.Write("Connecting...");
00033 sshCp.Connect();
00034 Console.WriteLine("OK");
00035
00036 while(true)
00037 {
00038 string direction = GetTransferDirection();
00039 if(direction.Equals("to"))
00040 {
00041 string lfile = GetArg("Enter local file ['Enter to cancel']");
00042 if(lfile=="") break;
00043 string rfile = GetArg("Enter remote file ['Enter to cancel']");
00044 if(rfile=="") break;
00045 sshCp.Put(lfile, rfile);
00046 }
00047 else
00048 {
00049 string rfile = GetArg("Enter remote file ['Enter to cancel']");
00050 if(rfile=="") break;
00051 string lpath = GetArg("Enter local path ['Enter to cancel']");
00052 if(lpath=="") break;
00053 sshCp.Get(rfile, lpath);
00054 }
00055 }
00056
00057 Console.Write("Disconnecting...");
00058 sshCp.Close();
00059 Console.WriteLine("OK");
00060 }
00061 catch(Exception e)
00062 {
00063 Console.WriteLine(e.Message);
00064 }
00065 }
00066
00067 public static string GetProtocol()
00068 {
00069 string proto = "";
00070 while( true )
00071 {
00072 Console.Write("Enter SSH transfer protocol [SCP|SFTP]: ");
00073 proto = Console.ReadLine();
00074 if(proto.ToLower().Equals("")) break;
00075 if( proto.ToLower().Equals("scp") || proto.ToLower().Equals("sftp"))
00076 break;
00077 Console.Write("Bad input, ");
00078 }
00079 return proto;
00080 }
00081
00082 public static string GetTransferDirection()
00083 {
00084 string dir = "";
00085 while( true )
00086 {
00087 Console.Write("Enter transfer direction [To|From]: ");
00088 dir = Console.ReadLine();
00089 if(dir.ToLower().Equals("")) break;
00090 if( dir.ToLower().Equals("to") || dir.ToLower().Equals("from"))
00091 break;
00092 Console.Write("Bad input, ");
00093 }
00094 return dir;
00095 }
00096
00097 public static string GetArg(string msg)
00098 {
00099 Console.Write(msg+": ");
00100 return Console.ReadLine();
00101 }
00102
00103 static ConsoleProgressBar progressBar;
00104
00105 private static void sshCp_OnTransferStart(string src, string dst, int transferredBytes, int totalBytes, string message)
00106 {
00107 Console.WriteLine();
00108 progressBar = new ConsoleProgressBar();
00109 progressBar.Update(transferredBytes, totalBytes, message);
00110 }
00111
00112 private static void sshCp_OnTransferProgress(string src, string dst, int transferredBytes, int totalBytes, string message)
00113 {
00114 if(progressBar!=null)
00115 {
00116 progressBar.Update(transferredBytes, totalBytes, message);
00117 }
00118 }
00119
00120 private static void sshCp_OnTransferEnd(string src, string dst, int transferredBytes, int totalBytes, string message)
00121 {
00122 if(progressBar!=null)
00123 {
00124 progressBar.Update(transferredBytes, totalBytes, message);
00125 progressBar=null;
00126 }
00127 }
00128 }
00129 }