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