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 |

ScpFrom.cs

1using System;
2using Tamir.SharpSsh.jsch;
3using System.IO;
4using System.Windows.Forms;
5using System.Text;
6
7/* ScpFrom.cs
8 * ====================================================================
9 * The following example was posted with the original JSch java library,
10 * and is translated to C# to show the usage of SharpSSH JSch API
11 * ====================================================================
12 * */
13namespace sharpSshTest.jsch_samples
14{
15	/// <summary>
16	/// This program will demonstrate the file transfer from remote to local
17	/// You will be asked passwd. 
18	/// If everything works fine, a file 'file1' on 'remotehost' will copied to
19	/// local 'file1'.
20	/// </summary>
21	public class ScpFrom
22	{	
23		public static void RunExample(String[] arg)
24		{
25			if(arg.Length!=2)
26			{
27				Console.WriteLine("usage: java ScpFrom user@remotehost:file1 file2");
28				return;
29			}      
30
31			try
32			{
33				String user=arg[0].Substring(0, arg[0].IndexOf('@'));
34				arg[0]=arg[0].Substring(arg[0].IndexOf('@')+1);
35				String host=arg[0].Substring(0, arg[0].IndexOf(':'));
36				String rfile=arg[0].Substring(arg[0].IndexOf(':')+1);
37				String lfile=arg[1];
38
39				String prefix=null;
40				if(Directory.Exists(lfile))
41				{
42					prefix=lfile+Path.DirectorySeparatorChar;
43				}
44
45				JSch jsch=new JSch();
46				Session session=jsch.getSession(user, host, 22);
47
48				// username and password will be given via UserInfo interface.
49				UserInfo ui=new MyUserInfo();
50				session.setUserInfo(ui);
51				session.connect();
52
53				// exec 'scp -f rfile' remotely
54				String command="scp -f "+rfile;
55				Channel channel=session.openChannel("exec");
56				((ChannelExec)channel).setCommand(command);
57
58				// get I/O streams for remote scp
59				Stream outs=channel.getOutputStream();
60				Stream ins=channel.getInputStream();
61
62				channel.connect();
63
64				byte[] buf=new byte[1024];
65
66				// send '\0'
67				buf[0]=0; outs.Write(buf, 0, 1); outs.Flush();
68
69				while(true)
70				{
71					int c=checkAck(ins);
72					if(c!='C')
73					{
74						break;
75					}
76
77					// read '0644 '
78					ins.Read(buf, 0, 5);
79
80					int filesize=0;
81					while(true)
82					{
83						ins.Read(buf, 0, 1);
84						if(buf[0]==' ')break;
85						filesize=filesize*10+(buf[0]-'0');
86					}
87
88					String file=null;
89					for(int i=0;;i++)
90					{
91						ins.Read(buf, i, 1);
92						if(buf[i]==(byte)0x0a)
93						{
94							file=Util.getString(buf, 0, i);
95							break;
96						}
97					}
98
99					//Console.WriteLine("filesize="+filesize+", file="+file);
100
101					// send '\0'
102					buf[0]=0; outs.Write(buf, 0, 1); outs.Flush();
103
104					// read a content of lfile
105					FileStream fos=File.OpenWrite(prefix==null ? 
106					lfile :
107						prefix+file);
108					int foo;
109					while(true)
110					{
111						if(buf.Length<filesize) foo=buf.Length;
112						else foo=filesize;
113						ins.Read(buf, 0, foo);
114						fos.Write(buf, 0, foo);
115						filesize-=foo;
116						if(filesize==0) break;
117					}
118					fos.Close();
119
120					byte[] tmp=new byte[1];
121
122					if(checkAck(ins)!=0)
123					{
124						Environment.Exit(0);
125					}
126
127					// send '\0'
128					buf[0]=0; outs.Write(buf, 0, 1); outs.Flush();
129				}
130				Environment.Exit(0);
131			}
132			catch(Exception e)
133			{
134				Console.WriteLine(e);
135			}
136		}
137
138		static int checkAck(Stream ins) 
139		{
140			int b=ins.ReadByte();
141			// b may be 0 for success,
142			//          1 for error,
143			//          2 for fatal error,
144			//          -1
145			if(b==0) return b;
146			if(b==-1) return b;
147
148			if(b==1 || b==2)
149			{
150				StringBuilder sb=new StringBuilder();
151				int c;
152				do 
153				{
154					c=ins.ReadByte();
155					sb.Append((char)c);
156				}
157				while(c!='\n');
158				if(b==1)
159				{ // error
160					Console.Write(sb.ToString());
161				}
162				if(b==2)
163				{ // fatal error
164					Console.Write(sb.ToString());
165				}
166			}
167			return b;
168		}
169
170		/// <summary>
171		/// A user info for getting user data
172		/// </summary>
173		public class MyUserInfo : UserInfo
174		{
175			/// <summary>
176			/// Holds the user password
177			/// </summary>
178			private String passwd;
179
180			/// <summary>
181			/// Returns the user password
182			/// </summary>
183			public String getPassword(){ return passwd; }
184
185			/// <summary>
186			/// Prompt the user for a Yes/No input
187			/// </summary>
188			public bool promptYesNo(String str)
189			{
190				return InputForm.PromptYesNo(str);
191			}
192			
193			/// <summary>
194			/// Returns the user passphrase (passwd for the private key file)
195			/// </summary>
196			public String getPassphrase(){ return null; }
197
198			/// <summary>
199			/// Prompt the user for a passphrase (passwd for the private key file)
200			/// </summary>
201			public bool promptPassphrase(String message){ return true; }
202
203			/// <summary>
204			/// Prompt the user for a password
205			/// </summary>\
206			public bool promptPassword(String message)
207			{
208				passwd=InputForm.GetUserInput(message, true);
209				return true;
210			}
211
212			/// <summary>
213			/// Shows a message to the user
214			/// </summary>
215			public void showMessage(String message)
216			{
217				InputForm.ShowMessage(message);
218			}
219		}
220	}
221}
222

Copyright (c) 2005 TamirGal.Com