tamirgal.com logo


SharpSSH examples

The following samples are raw examples of how to directly use the JSch API from sharpSSH. The samples were translated from the java examples found on JSch homepage.

Please select a sample:


AES.cs | ChangePassphrase.cs | KeyGen.cs | KnownHosts.cs | PortForwardingL.cs | PortForwardingR.cs | ScpFrom.cs | Scp.cs | ScpTo.cs | Sftp.cs | Shell.cs | SshExeTest.cs | SshExpectTest.cs | SshFileTransferTest.cs | SshShellTest.cs | StreamForwarding.cs | Subsystem.cs | UserAuthPubKey.cs | Util.cs | README.txt |

SshFileTransferTest.cs

1using System;
2using System.Collections;
3using Tamir.SharpSsh;
4
5namespace sharpSshTest.sharpssh_samples
6{
7	/// <summary>
8	/// Sample showing the use of the SSH file trasfer features of 
9	/// SharpSSH such as SFTP and SCP
10	/// </summary>
11	public class SshFileTransferTest
12	{
13		public static void RunExample()
14		{		
15			try
16			{
17				SshConnectionInfo input = Util.GetInput();
18				string proto = GetProtocol();
19				SshTransferProtocolBase sshCp;
20
21				if(proto.Equals("scp"))
22					sshCp = new Scp(input.Host, input.User);
23				else
24					sshCp = new Sftp(input.Host, input.User);
25
26				if(input.Pass != null) sshCp.Password = input.Pass;
27				if(input.IdentityFile != null) sshCp.AddIdentityFile( input.IdentityFile );
28				sshCp.OnTransferStart += new FileTransferEvent(sshCp_OnTransferStart);
29				sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
30				sshCp.OnTransferEnd += new FileTransferEvent(sshCp_OnTransferEnd);
31
32				Console.Write("Connecting...");
33				sshCp.Connect();
34				Console.WriteLine("OK");
35
36				while(true)
37				{
38					string direction = GetTransferDirection();
39					if(direction.Equals("to"))
40					{
41						string lfile = GetArg("Enter local file ['Enter to cancel']");
42						if(lfile=="") break;
43						string rfile = GetArg("Enter remote file ['Enter to cancel']");
44						if(rfile=="") break;
45						sshCp.Put(lfile, rfile);
46					}
47					else
48					{
49						string rfile = GetArg("Enter remote file ['Enter to cancel']");
50						if(rfile=="") break;
51						string lpath = GetArg("Enter local path ['Enter to cancel']");
52						if(lpath=="") break;
53						sshCp.Get(rfile, lpath);
54					}
55				}
56
57				Console.Write("Disconnecting...");
58				sshCp.Close();
59				Console.WriteLine("OK");
60			}
61			catch(Exception e)
62			{
63				Console.WriteLine(e.Message);
64			}
65		}
66
67		public static string GetProtocol()
68		{
69			string proto = "";
70			while( true )
71			{
72				Console.Write("Enter SSH transfer protocol [SCP|SFTP]: ");
73				proto = Console.ReadLine();
74				if(proto.ToLower().Equals("")) break;
75				if( proto.ToLower().Equals("scp") || proto.ToLower().Equals("sftp"))
76					break;
77				Console.Write("Bad input, ");
78			}
79			return proto;
80		}
81
82		public static string GetTransferDirection()
83		{
84			string dir = "";
85			while( true )
86			{
87				Console.Write("Enter transfer direction [To|From]: ");
88				dir = Console.ReadLine();
89				if(dir.ToLower().Equals("")) break;
90				if( dir.ToLower().Equals("to") || dir.ToLower().Equals("from"))
91					break;
92				Console.Write("Bad input, ");
93			}
94			return dir;
95		}
96
97		public static string GetArg(string msg)
98		{
99			Console.Write(msg+": ");
100			return Console.ReadLine();
101		}
102
103		static ConsoleProgressBar progressBar;
104
105		private static void sshCp_OnTransferStart(string src, string dst, int transferredBytes, int totalBytes, string message)
106		{
107			Console.WriteLine();
108			progressBar = new ConsoleProgressBar();
109			progressBar.Update(transferredBytes, totalBytes, message);
110		}
111
112		private static void sshCp_OnTransferProgress(string src, string dst, int transferredBytes, int totalBytes, string message)
113		{
114			if(progressBar!=null)
115			{
116				progressBar.Update(transferredBytes, totalBytes, message);
117			}
118		}
119
120		private static void sshCp_OnTransferEnd(string src, string dst, int transferredBytes, int totalBytes, string message)
121		{
122			if(progressBar!=null)
123			{
124				progressBar.Update(transferredBytes, totalBytes, message);
125				progressBar=null;
126			}
127		}
128	}
129}
130

Copyright (c) 2005 TamirGal.Com