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 |

ScpTo.cs

1using System;
2using Tamir.SharpSsh.jsch;
3using System.IO;
4using System.Windows.Forms;
5using System.Text;
6
7/* ScpTo.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 local to remote.
17	/// You will be asked passwd. 
18	/// If everything works fine, a local file 'file1' will copied to
19	/// 'file2' on 'remotehost'.
20	/// </summary>	
21	public class ScpTo
22	{
23		public static void RunExample(String[] arg)
24		{
25			if(arg.Length!=2)
26			{
27				Console.WriteLine("usage: java ScpTo file1 user@remotehost:file2");
28				Environment.Exit(-1);
29			}      
30
31			try
32			{
33
34				String lfile=arg[0];
35				String user=arg[1].Substring(0, arg[1].IndexOf('@'));
36				arg[1]=arg[1].Substring(arg[1].IndexOf('@')+1);
37				String host=arg[1].Substring(0, arg[1].IndexOf(':'));
38				String rfile=arg[1].Substring(arg[1].IndexOf(':')+1);
39
40				JSch jsch=new JSch();
41				Session session=jsch.getSession(user, host, 22);
42
43				// username and password will be given via UserInfo interface.
44				UserInfo ui=new MyUserInfo();
45				session.setUserInfo(ui);
46				session.connect();
47
48
49				// exec 'scp -t rfile' remotely
50				String command="scp -p -t "+rfile;
51				Channel channel=session.openChannel("exec");
52				((ChannelExec)channel).setCommand(command);
53
54				// get I/O streams for remote scp
55				Stream outs=channel.getOutputStream();
56				Stream ins=channel.getInputStream();
57
58				channel.connect();
59
60				byte[] tmp=new byte[1];
61
62				if(checkAck(ins)!=0)
63				{
64					Environment.Exit(0);
65				}
66
67				// send "C0644 filesize filename", where filename should not include '/'
68								
69				int filesize=(int)(new FileInfo(lfile)).Length;
70				command="C0644 "+filesize+" ";
71				if(lfile.LastIndexOf('/')>0)
72				{
73					command+=lfile.Substring(lfile.LastIndexOf('/')+1);
74				}
75				else
76				{
77					command+=lfile;
78				}
79				command+="\n";
80				byte[] buff = Util.getBytes(command);
81				outs.Write(buff, 0, buff.Length); outs.Flush();
82
83				if(checkAck(ins)!=0)
84				{
85					Environment.Exit(0);
86				}
87
88				// send a content of lfile
89				FileStream fis=File.OpenRead(lfile);
90				byte[] buf=new byte[1024];
91				while(true)
92				{
93					int len=fis.Read(buf, 0, buf.Length);
94					if(len<=0) break;
95					outs.Write(buf, 0, len); outs.Flush();
96					Console.Write("#");
97				}
98
99				// send '\0'
100				buf[0]=0; outs.Write(buf, 0, 1); outs.Flush();
101				Console.Write(".");
102
103				if(checkAck(ins)!=0)
104				{
105					Environment.Exit(0);
106				}
107				Console.WriteLine("OK");
108				Environment.Exit(0);
109			}
110			catch(Exception e)
111			{
112				Console.WriteLine(e);
113			}
114		}
115
116		static int checkAck(Stream ins) 
117		{
118			Console.Write(".");
119			int b=ins.ReadByte();
120			Console.Write(".");
121			// b may be 0 for success,
122			//          1 for error,
123			//          2 for fatal error,
124			//          -1
125			if(b==0) return b;
126			if(b==-1) return b;
127
128			if(b==1 || b==2)
129			{
130				StringBuilder sb=new StringBuilder();
131				int c;
132				do 
133				{
134					c=ins.ReadByte();
135					sb.Append((char)c);
136				}
137				while(c!='\n');
138				if(b==1)
139				{ // error
140					Console.Write(sb.ToString());
141				}
142				if(b==2)
143				{ // fatal error
144					Console.Write(sb.ToString());
145				}
146			}
147			return b;
148		}
149
150		public class MyUserInfo : UserInfo
151		{
152			public String getPassword(){ return passwd; }
153			public bool promptYesNo(String str)
154			{
155				DialogResult returnVal = MessageBox.Show(
156					str,
157					"SharpSSH",
158					MessageBoxButtons.YesNo,
159					MessageBoxIcon.Warning);
160				return (returnVal==DialogResult.Yes);
161			}
162  
163			String passwd;
164			
165
166			public String getPassphrase(){ return null; }
167			public bool promptPassphrase(String message){ return true; }
168			public bool promptPassword(String message)
169			{
170				InputForm passwordField=new InputForm();
171				passwordField.Text = message;
172				passwordField.PasswordField = true;
173
174				if ( passwordField.PromptForInput() )
175				{
176					passwd=passwordField.getText();
177					return true;
178				}
179				else{ return false; }
180			}
181			public void showMessage(String message)
182			{
183				MessageBox.Show(
184					message,
185					"SharpSSH",
186					MessageBoxButtons.OK,
187					MessageBoxIcon.Asterisk);
188			}
189		}
190
191	}
192
193}
194

Copyright (c) 2005 TamirGal.Com