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 |

KnownHosts.cs

1using System;
2using System.IO;
3using System.Windows.Forms;
4using Tamir.SharpSsh.jsch;
5
6/* KnownHosts.cs
7 * ====================================================================
8 * The following example was posted with the original JSch java library,
9 * and is translated to C# to show the usage of SharpSSH JSch API
10 * ====================================================================
11 * */
12namespace sharpSshTest.jsch_samples
13{
14	/// <summary>
15	/// This program will demonstrate the 'known_hosts' file handling.
16	/// You will be asked username, hostname, a path for 'known_hosts' and passwd. 
17	/// If everything works fine, you will get the shell prompt.
18	/// In current implementation, jsch only reads 'known_hosts' for checking
19	/// and does not modify it.
20	/// </summary>
21	public class KnownHosts
22	{
23		public static void RunExample(String[] arg)
24		{
25			try
26			{
27				//Get the "known hosts" filename from the user
28				Console.WriteLine("Please select your 'known_hosts' from the poup window...");
29				String file = InputForm.GetFileFromUser("Choose your known_hosts(ex. ~/.ssh/known_hosts)");
30				Console.WriteLine("You chose "+file+".");
31				//Create a new JSch instance
32				JSch jsch=new JSch();
33				//Set the known hosts file
34				jsch.setKnownHosts(file);				
35
36				//Get the KnownHosts repository from JSchs
37				HostKeyRepository hkr=jsch.getHostKeyRepository();
38
39				//Print all known hosts and keys
40				HostKey[] hks=hkr.getHostKey();
41				HostKey hk;
42				if(hks!=null)
43				{
44					Console.WriteLine();
45					Console.WriteLine("Host keys in "+hkr.getKnownHostsRepositoryID()+":");
46					for(int i=0; i<hks.Length; i++)
47					{
48						hk=hks[i];
49						Console.WriteLine(hk.getHost()+" "+
50							hk.getType()+" "+
51							hk.getFingerPrint(jsch));
52					}
53					Console.WriteLine("");
54				}
55
56				//Now connect to the remote server...
57
58				//Prompt for username and server host
59				Console.WriteLine("Please enter the user and host info at the popup window...");
60				String host = InputForm.GetUserInput
61					("Enter username@hostname",
62					Environment.UserName+"@localhost");
63				String user=host.Substring(0, host.IndexOf('@'));
64				host=host.Substring(host.IndexOf('@')+1);
65
66				//Create a new SSH session
67				Session session=jsch.getSession(user, host, 22);
68
69				// username and password will be given via UserInfo interface.
70				UserInfo ui=new MyUserInfo();
71				session.setUserInfo(ui);
72
73				//Connect to remote SSH server
74				session.connect();
75
76				//Print the host key info
77				//of the connected server:
78				hk=session.getHostKey();
79				Console.WriteLine("HostKey: "+
80					hk.getHost()+" "+
81					hk.getType()+" "+
82					hk.getFingerPrint(jsch));
83			
84				//Open a new Shell channel on the SSH session
85				Channel channel=session.openChannel("shell");
86
87				//Redirect standard I/O to the SSH channel
88				channel.setInputStream(Console.OpenStandardInput());
89				channel.setOutputStream(Console.OpenStandardOutput());
90
91				//Connect the channel
92				channel.connect();
93
94				Console.WriteLine("-- Shell channel is connected using the {0} cipher", 
95					session.getCipher());
96
97				//Wait till channel is closed
98				while(!channel.isClosed())
99				{
100					System.Threading.Thread.Sleep(500);
101				}
102
103				//Disconnect from remote server
104				channel.disconnect();
105				session.disconnect();
106			}
107			catch(Exception e)
108			{
109				Console.WriteLine(e.Message);
110			}
111		}
112
113		/// <summary>
114		/// A user info for getting user data
115		/// </summary>
116		public class MyUserInfo : UserInfo
117		{
118			/// <summary>
119			/// Holds the user password
120			/// </summary>
121			private String passwd;
122
123			/// <summary>
124			/// Returns the user password
125			/// </summary>
126			public String getPassword(){ return passwd; }
127
128			/// <summary>
129			/// Prompt the user for a Yes/No input
130			/// </summary>
131			public bool promptYesNo(String str)
132			{
133				return InputForm.PromptYesNo(str);
134			}
135			
136			/// <summary>
137			/// Returns the user passphrase (passwd for the private key file)
138			/// </summary>
139			public String getPassphrase(){ return null; }
140
141			/// <summary>
142			/// Prompt the user for a passphrase (passwd for the private key file)
143			/// </summary>
144			public bool promptPassphrase(String message){ return true; }
145
146			/// <summary>
147			/// Prompt the user for a password
148			/// </summary>
149			public bool promptPassword(String message)
150			{
151				passwd=InputForm.GetUserInput(message, true);
152				return true;
153			}
154
155			/// <summary>
156			/// Shows a message to the user
157			/// </summary>
158			public void showMessage(String message)
159			{
160				InputForm.ShowMessage(message);
161			}
162		}
163	}
164}
165

Copyright (c) 2005 TamirGal.Com