00001 using System;
00002 using Tamir.SharpSsh.jsch;
00003
00004
00005
00006
00007
00008
00009
00010 namespace sharpSshTest.jsch_samples
00011 {
00018 public class ViaHTTP
00019 {
00020 public static void RunExample(String[] arg)
00021 {
00022 try
00023 {
00024
00025 JSch jsch=new JSch();
00026
00027
00028 Console.WriteLine("Please enter the user and host info at the popup window...");
00029
00030 String host = InputForm.GetUserInput("Enter username@hostname",
00031 Environment.UserName+"@localhost");
00032
00033 String user=host.Substring(0, host.IndexOf('@'));
00034 host=host.Substring(host.IndexOf('@')+1);
00035
00036
00037 Session session=jsch.getSession(user, host, 22);
00038
00039 String proxy=InputForm.GetUserInput("Enter proxy server",
00040 "hostname:port");
00041
00042 string proxy_host=proxy.Substring(0, proxy.IndexOf(':'));
00043 int proxy_port=int.Parse(proxy.Substring(proxy.IndexOf(':')+1));
00044
00045 session.setProxy(new ProxyHTTP(proxy_host, proxy_port));
00046
00047
00048 UserInfo ui=new MyUserInfo();
00049 session.setUserInfo(ui);
00050
00051
00052 session.connect();
00053
00054
00055 Channel channel=session.openChannel("shell");
00056
00057
00058 channel.setInputStream(Console.OpenStandardInput());
00059 channel.setOutputStream(Console.OpenStandardOutput());
00060
00061
00062 channel.connect();
00063
00064 Console.WriteLine("-- Shell channel is connected using the {0} cipher",
00065 session.getCipher());
00066
00067
00068 while(!channel.isClosed())
00069 {
00070 System.Threading.Thread.Sleep(500);
00071 }
00072
00073
00074 channel.disconnect();
00075 session.disconnect();
00076
00077 }
00078 catch(Exception e)
00079 {
00080 Console.WriteLine(e);
00081 }
00082 }
00083
00087 public class MyUserInfo : UserInfo, UIKeyboardInteractive
00088 {
00092 private String passwd;
00093
00097 public String getPassword(){ return passwd; }
00098
00102 public bool promptYesNo(String str)
00103 {
00104 return InputForm.PromptYesNo(str);
00105 }
00106
00110 public String getPassphrase(){ return null; }
00111
00115 public bool promptPassphrase(String message){ return true; }
00116
00120 public bool promptPassword(String message)
00121 {
00122 passwd=InputForm.GetUserInput(message, true);
00123 return true;
00124 }
00125
00129 public void showMessage(String message)
00130 {
00131 InputForm.ShowMessage(message);
00132 }
00133
00134 #region UIKeyboardInteractive Members
00135
00136 public string[] promptKeyboardInteractive(string destination, string name, string instruction, string[] prompt, bool[] echo)
00137 {
00138 string prmpt = prompt != null && prompt.Length > 0 ? prompt[0] : "";
00139 passwd=InputForm.GetUserInput(prmpt, true);
00140 return new string[] { passwd };
00141 }
00142
00143 #endregion
00144 }
00145 }
00146 }
00147