00001 using System;
00002 using System.Windows.Forms;
00003
00004
00005
00006
00007
00008
00009
00010 namespace Tamir.SharpSsh.jsch.examples
00011 {
00017 public class UserAuthPubKey
00018 {
00019 public static void Main(String[] arg)
00020 {
00021 try
00022 {
00023 JSch jsch=new JSch();
00024
00025
00026 Console.WriteLine("Please choose your private key file...");
00027 String file = InputForm.GetFileFromUser("Choose your privatekey(ex. ~/.ssh/id_dsa)");
00028 Console.WriteLine("You chose "+file+".");
00029
00030
00031 jsch.addIdentity(file);
00032
00033
00034 Console.WriteLine("Please enter the user and host info at the popup window...");
00035 String host = InputForm.GetUserInput
00036 ("Enter username@hostname",
00037 Environment.UserName+"@localhost");
00038 String user=host.Substring(0, host.IndexOf('@'));
00039 host=host.Substring(host.IndexOf('@')+1);
00040
00041
00042 Session session=jsch.getSession(user, host, 22);
00043
00044
00045 UserInfo ui=new MyUserInfo();
00046 session.setUserInfo(ui);
00047
00048
00049 session.connect();
00050
00051
00052 Channel channel=session.openChannel("shell");
00053
00054
00055 channel.setInputStream(Console.OpenStandardInput());
00056 channel.setOutputStream(Console.OpenStandardOutput());
00057
00058
00059 channel.connect();
00060
00061
00062 while(!channel.isClosed())
00063 {
00064 System.Threading.Thread.Sleep(500);
00065 }
00066
00067
00068 channel.disconnect();
00069 session.disconnect();
00070
00071 }
00072 catch(Exception e)
00073 {
00074 Console.WriteLine(e);
00075 }
00076 }
00077
00081 public class MyUserInfo : UserInfo
00082 {
00086 private String passphrase;
00087
00091 public String getPassword(){ return null; }
00092
00096 public bool promptYesNo(String str)
00097 {
00098 return InputForm.PromptYesNo(str);
00099 }
00100
00104 public String getPassphrase(){ return passphrase; }
00105
00109 public bool promptPassphrase(String message)
00110 {
00111 passphrase=InputForm.GetUserInput(message, true);
00112 return true;
00113 }
00114
00118 public bool promptPassword(String message){ return true; }
00119 public void showMessage(String message)
00120 {
00121 InputForm.ShowMessage(message);
00122 }
00123 }
00124 }
00125
00126 }