|
PortForwardingL.cs
1using System;
2using System.Windows.Forms;
3using Tamir.SharpSsh.jsch;
4
5/* PortForwardingL.cs
6 * ====================================================================
7 * The following example was posted with the original JSch java library,
8 * and is translated to C# to show the usage of SharpSSH JSch API
9 * ====================================================================
10 * */
11namespace sharpSshTest.jsch_samples
12{
13 /// <summary>
14 /// This program will demonstrate the port forwarding like option -L of
15 /// ssh command; the given port on the local host will be forwarded to
16 /// the given remote host and port on the remote side.
17 /// You will be asked username, hostname, port:host:hostport and passwd.
18 /// If everything works fine, you will get the shell prompt.
19 /// Try the port on localhost.
20 /// </summary>
21 public class PortForwardingL
22 {
23 public static void RunExample(String[] arg)
24 {
25 int port;
26
27 try
28 {
29 //Create a new JSch instance
30 JSch jsch=new JSch();
31
32 //Prompt for username and server host
33 Console.WriteLine("Please enter the user and host info at the popup window...");
34 String host = InputForm.GetUserInput
35 ("Enter username@hostname",
36 Environment.UserName+"@localhost");
37 String user=host.Substring(0, host.IndexOf('@'));
38 host=host.Substring(host.IndexOf('@')+1);
39
40 //Create a new SSH session
41 Session session=jsch.getSession(user, host, 22);
42
43 //Get from user the local port, remote host and remote host port
44 String foo = InputForm.GetUserInput("Enter -L port:host:hostport","port:host:hostport");
45 int lport=int.Parse(foo.Substring(0, foo.IndexOf(':')));
46 foo=foo.Substring(foo.IndexOf(':')+1);
47 String rhost=foo.Substring(0, foo.IndexOf(':'));
48 int rport=int.Parse(foo.Substring(foo.IndexOf(':')+1));
49
50 // username and password will be given via UserInfo interface.
51 UserInfo ui=new MyUserInfo();
52 session.setUserInfo(ui);
53 session.connect();
54
55 Console.WriteLine("localhost:"+lport+" -> "+rhost+":"+rport);
56
57 //Set port forwarding on the opened session
58 session.setPortForwardingL(lport, rhost, rport);
59 }
60 catch(Exception e)
61 {
62 Console.WriteLine(e.Message);
63 }
64 }
65
66 /// <summary>
67 /// A user info for getting user data
68 /// </summary>
69 public class MyUserInfo : UserInfo
70 {
71 /// <summary>
72 /// Holds the user password
73 /// </summary>
74 private String passwd;
75
76 /// <summary>
77 /// Returns the user password
78 /// </summary>
79 public String getPassword(){ return passwd; }
80
81 /// <summary>
82 /// Prompt the user for a Yes/No input
83 /// </summary>
84 public bool promptYesNo(String str)
85 {
86 return InputForm.PromptYesNo(str);
87 }
88
89 /// <summary>
90 /// Returns the user passphrase (passwd for the private key file)
91 /// </summary>
92 public String getPassphrase(){ return null; }
93
94 /// <summary>
95 /// Prompt the user for a passphrase (passwd for the private key file)
96 /// </summary>
97 public bool promptPassphrase(String message){ return true; }
98
99 /// <summary>
100 /// Prompt the user for a password
101 /// </summary>\
102 public bool promptPassword(String message)
103 {
104 passwd=InputForm.GetUserInput(message, true);
105 return true;
106 }
107
108 /// <summary>
109 /// Shows a message to the user
110 /// </summary>
111 public void showMessage(String message)
112 {
113 InputForm.ShowMessage(message);
114 }
115 }
116 }
117}
118
|