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 {
00022 public class StreamForwarding
00023 {
00024 public static void RunExample(String[] arg)
00025 {
00026 int port;
00027
00028 try
00029 {
00030
00031 JSch jsch=new JSch();
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 session.connect();
00048
00049
00050 String foo = InputForm.GetUserInput("Enter host and port", "host:port");
00051 host=foo.Substring(0, foo.IndexOf(':'));
00052 port=int.Parse(foo.Substring(foo.IndexOf(':')+1));
00053
00054 Console.WriteLine("System.{in,out} will be forwarded to "+
00055 host+":"+port+".");
00056 Channel channel=session.openChannel("direct-tcpip");
00057 ((ChannelDirectTCPIP)channel).setInputStream(Console.OpenStandardInput());
00058 ((ChannelDirectTCPIP)channel).setOutputStream(Console.OpenStandardOutput());
00059 ((ChannelDirectTCPIP)channel).setHost(host);
00060 ((ChannelDirectTCPIP)channel).setPort(port);
00061 channel.connect();
00062
00063 while(!channel.isClosed())
00064 {
00065 System.Threading.Thread.Sleep(500);
00066 }
00067 channel.disconnect();
00068 session.disconnect();
00069 }
00070 catch(Exception e)
00071 {
00072 Console.WriteLine(e);
00073 }
00074 }
00075
00079 public class MyUserInfo : UserInfo
00080 {
00084 private String passwd;
00085
00089 public String getPassword(){ return passwd; }
00090
00094 public bool promptYesNo(String str)
00095 {
00096 return InputForm.PromptYesNo(str);
00097 }
00098
00102 public String getPassphrase(){ return null; }
00103
00107 public bool promptPassphrase(String message){ return true; }
00108
00112 public bool promptPassword(String message)
00113 {
00114 passwd=InputForm.GetUserInput(message, true);
00115 return true;
00116 }
00117
00121 public void showMessage(String message)
00122 {
00123 InputForm.ShowMessage(message);
00124 }
00125 }
00126 }
00127 }