00001 using System;
00002 using System.Windows.Forms;
00003
00004
00005
00006
00007
00008
00009
00010 namespace Tamir.SharpSsh.jsch.examples
00011 {
00021 public class StreamForwarding
00022 {
00023 public static void Main(String[] arg)
00024 {
00025 int port;
00026
00027 try
00028 {
00029
00030 JSch jsch=new JSch();
00031
00032
00033 Console.WriteLine("Please enter the user and host info at the popup window...");
00034 String host = InputForm.GetUserInput
00035 ("Enter username@hostname",
00036 Environment.UserName+"@localhost");
00037 String user=host.Substring(0, host.IndexOf('@'));
00038 host=host.Substring(host.IndexOf('@')+1);
00039
00040
00041 Session session=jsch.getSession(user, host, 22);
00042
00043
00044 UserInfo ui=new MyUserInfo();
00045 session.setUserInfo(ui);
00046 session.connect();
00047
00048
00049 String foo = InputForm.GetUserInput("Enter host and port", "host:port");
00050 host=foo.Substring(0, foo.IndexOf(':'));
00051 port=int.Parse(foo.Substring(foo.IndexOf(':')+1));
00052
00053 Console.WriteLine("System.{in,out} will be forwarded to "+
00054 host+":"+port+".");
00055 Channel channel=session.openChannel("direct-tcpip");
00056 ((ChannelDirectTCPIP)channel).setInputStream(Console.OpenStandardInput());
00057 ((ChannelDirectTCPIP)channel).setOutputStream(Console.OpenStandardOutput());
00058 ((ChannelDirectTCPIP)channel).setHost(host);
00059 ((ChannelDirectTCPIP)channel).setPort(port);
00060 channel.connect();
00061
00062 while(!channel.isClosed())
00063 {
00064 System.Threading.Thread.Sleep(500);
00065 }
00066 channel.disconnect();
00067 session.disconnect();
00068 }
00069 catch(Exception e)
00070 {
00071 Console.WriteLine(e);
00072 }
00073 }
00074
00078 public class MyUserInfo : UserInfo
00079 {
00083 private String passwd;
00084
00088 public String getPassword(){ return passwd; }
00089
00093 public bool promptYesNo(String str)
00094 {
00095 return InputForm.PromptYesNo(str);
00096 }
00097
00101 public String getPassphrase(){ return null; }
00102
00106 public bool promptPassphrase(String message){ return true; }
00107
00111 public bool promptPassword(String message)
00112 {
00113 passwd=InputForm.GetUserInput(message, true);
00114 return true;
00115 }
00116
00120 public void showMessage(String message)
00121 {
00122 InputForm.ShowMessage(message);
00123 }
00124 }
00125 }
00126 }