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 {
00021 public class PortForwardingR
00022 {
00023 public static void RunExample(String[] arg)
00024 {
00025
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 String foo = InputForm.GetUserInput("Enter -R port:host:hostport","port:host:hostport");
00045 int rport=int.Parse(foo.Substring(0, foo.IndexOf(':')));
00046 foo=foo.Substring(foo.IndexOf(':')+1);
00047 String lhost=foo.Substring(0, foo.IndexOf(':'));
00048 int lport=int.Parse(foo.Substring(foo.IndexOf(':')+1));
00049
00050
00051 UserInfo ui=new MyUserInfo();
00052 session.setUserInfo(ui);
00053 session.connect();
00054
00055 Console.WriteLine(host+":"+rport+" -> "+lhost+":"+lport);
00056
00057
00058 session.setPortForwardingR(rport, lhost, lport);
00059 }
00060 catch(Exception e)
00061 {
00062 Console.WriteLine(e.Message);
00063 }
00064 }
00065
00069 public class MyUserInfo : UserInfo, UIKeyboardInteractive
00070 {
00074 private String passwd;
00075
00079 public String getPassword(){ return passwd; }
00080
00084 public bool promptYesNo(String str)
00085 {
00086 return InputForm.PromptYesNo(str);
00087 }
00088
00092 public String getPassphrase(){ return null; }
00093
00097 public bool promptPassphrase(String message){ return true; }
00098
00102 public bool promptPassword(String message)
00103 {
00104 passwd=InputForm.GetUserInput(message, true);
00105 return true;
00106 }
00107
00111 public void showMessage(String message)
00112 {
00113 InputForm.ShowMessage(message);
00114 }
00115
00116 public string[] promptKeyboardInteractive(string destination, string name, string instruction, string[] prompt,
00117 bool[] echo)
00118 {
00119 string prmpt = prompt != null && prompt.Length > 0 ? prompt[0] : "";
00120 passwd=InputForm.GetUserInput(prmpt, true);
00121 return new string[] { passwd };
00122 }
00123 }
00124 }
00125 }