00001 using System;
00002 using System.IO;
00003 using System.Windows.Forms;
00004 using Tamir.SharpSsh.jsch;
00005
00006
00007
00008
00009
00010
00011
00012 namespace sharpSshTest.jsch_samples
00013 {
00020 public class Shell
00021 {
00022 public static void RunExample(String[] arg)
00023 {
00024 try
00025 {
00026
00027 JSch jsch=new JSch();
00028
00029
00030 Console.WriteLine("Please enter the user and host info at the popup window...");
00031 String host = InputForm.GetUserInput
00032 ("Enter username@hostname",
00033 Environment.UserName+"@localhost");
00034 String user=host.Substring(0, host.IndexOf('@'));
00035 host=host.Substring(host.IndexOf('@')+1);
00036
00037
00038 Session session=jsch.getSession(user, host, 22);
00039
00040
00041 UserInfo ui=new MyUserInfo();
00042 session.setUserInfo(ui);
00043
00044
00045 session.connect();
00046
00047
00048 Channel channel=session.openChannel("shell");
00049
00050
00051 channel.setInputStream(Console.OpenStandardInput());
00052 channel.setOutputStream(Console.OpenStandardOutput());
00053
00054
00055 channel.connect();
00056
00057 Console.WriteLine("-- Shell channel is connected using the {0} cipher",
00058 session.getCipher());
00059
00060
00061 while(!channel.isClosed())
00062 {
00063 System.Threading.Thread.Sleep(500);
00064 }
00065
00066
00067 channel.disconnect();
00068 session.disconnect();
00069
00070 }
00071 catch(Exception e)
00072 {
00073 Console.WriteLine(e);
00074 }
00075 }
00076
00080 public class MyUserInfo : UserInfo, UIKeyboardInteractive
00081 {
00085 private String passwd;
00086
00090 public String getPassword(){ return passwd; }
00091
00095 public bool promptYesNo(String str)
00096 {
00097 return InputForm.PromptYesNo(str);
00098 }
00099
00103 public String getPassphrase(){ return null; }
00104
00108 public bool promptPassphrase(String message){ return true; }
00109
00113 public bool promptPassword(String message)
00114 {
00115 passwd=InputForm.GetUserInput(message, true);
00116 return true;
00117 }
00118
00122 public void showMessage(String message)
00123 {
00124 InputForm.ShowMessage(message);
00125 }
00126
00127 #region UIKeyboardInteractive Members
00128
00129 public string[] promptKeyboardInteractive(string destination, string name, string instruction, string[] prompt, bool[] echo)
00130 {
00131 string prmpt = prompt != null && prompt.Length > 0 ? prompt[0] : "";
00132 passwd=InputForm.GetUserInput(prmpt, true);
00133 return new string[] { passwd };
00134 }
00135
00136 #endregion
00137 }
00138 }
00139 }