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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| using System;
using Tamir.SharpSsh;
using System.Threading;
namespace sharpSshTest
{
/// <summary>
/// Summary description for sharpSshTest.
/// </summary>
public class sharpSshTest
{
static string host, user, pass;
public static void Main()
{
PrintVersoin();
Console.WriteLine();
Console.WriteLine("1) Simple SSH session example using SshStream");
Console.WriteLine("2) SCP example from local to remote");
Console.WriteLine("3) SCP example from remote to local");
Console.WriteLine();
INPUT:
int i=-1;
Console.Write("Please enter your choice: ");
try
{
i = int.Parse( Console.ReadLine() );
Console.WriteLine();
}
catch
{
i=-1;
}
switch(i)
{
case 1:
SshStream();
break;
case 2:
Scp("to");
break;
case 3:
Scp("from");
break;
default:
Console.Write("Bad input, ");
goto INPUT;
}
}
/// <summary>
/// Get input from the user
/// </summary>
public static void GetInput()
{
Console.Write(" Remot Host: ");
host = Console.ReadLine();
Console.Write("User: ");
user = Console.ReadLine();
Console.Write("Password: ");
pass = Console.ReadLine();
Console.WriteLine();
}
/// <summary>
/// Demonstrates the SshStream class
/// </summary>
public static void SshStream()
{
GetInput();
try
{
Console.Write("-Connecting...");
SshStream ssh = new SshStream(host, user, pass);
Console.WriteLine("OK ({0}/{1})",ssh.Cipher,ssh.Mac);
Console.WriteLine("Server version={0}, Client version={1}", ssh.ServerVersion, ssh.ClientVersion);
Console.WriteLine("-Use the 'exit' command to disconnect.");
Console.WriteLine();
//Sets the end of response character
ssh.Prompt = "#";
//Remove terminal emulation characters
ssh.RemoveTerminalEmulationCharacters = true;
//Reads the initial response from the SSH stream
Console.Write( ssh.ReadResponse() );
Console.ReadLine();
//Send commands from the user
while(true)
{
string command = Console.ReadLine();
if (command.ToLower().Equals("exit"))
break;
//Write command to the SSH stream
ssh.Write( command );
//Read response from the SSH stream
Console.Write( ssh.ReadResponse() );
}
ssh.Close(); //Close the connection
Console.WriteLine("Connection closed.");
Console.ReadLine();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
/// <summary>
/// Demonstrates the Scp class
/// </summary>
/// <param name="cmd">Either "to" or "from"</param>
public static void Scp(string cmd)
{
GetInput();
string local=null, remote=null;
if(cmd.ToLower().Equals("to"))
{
Console.Write("Local file: ");
local = Console.ReadLine();
Console.Write("Remote file: ");
remote = Console.ReadLine();
}
else if(cmd.ToLower().Equals("from"))
{
Console.Write("Remote file: ");
remote = Console.ReadLine();
Console.Write("Local file: ");
local = Console.ReadLine();
}
Scp scp = new Scp();
scp.OnConnecting += new FileTansferEvent(scp_OnConnecting);
scp.OnStart += new FileTansferEvent(scp_OnProgress);
scp.OnEnd += new FileTansferEvent(scp_OnEnd);
scp.OnProgress += new FileTansferEvent(scp_OnProgress);
try
{
if(cmd.ToLower().Equals("to"))
scp.To(local, host, remote, user, pass);
else if(cmd.ToLower().Equals("from"))
scp.From(host, remote, user, pass,local);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
static void PrintVersoin()
{
try
{
System.Reflection.Assembly asm
= System.Reflection.Assembly.GetAssembly(typeof(Tamir.SharpSsh.SshStream));
Console.WriteLine("sharpSsh v"+asm.GetName().Version);
}
catch
{
Console.WriteLine("sharpSsh v1.0");
}
}
#region SCP Event Handlers
static ConsoleProgressBar progressBar;
private static void scp_OnConnecting(int transferredBytes, int totalBytes, string message)
{
Console.WriteLine();
progressBar = new ConsoleProgressBar();
progressBar.Update(transferredBytes, totalBytes, message);
}
private static void scp_OnProgress(int transferredBytes, int totalBytes, string message)
{
progressBar.Update(transferredBytes, totalBytes, message);
}
private static void scp_OnEnd(int transferredBytes, int totalBytes, string message)
{
progressBar.Update(transferredBytes, totalBytes, message);
progressBar=null;
}
#endregion SCP Event Handlers
}
} |
Partager