1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ 2 /** 3 * This program will demonstrate the stream forwarding. The given Java 4 * I/O streams will be forwared to the given remote host and port on 5 * the remote side. It is simmilar to the -L option of ssh command, 6 * but you don't have to assign and open a local tcp port. 7 * $ CLASSPATH=.:../build javac StreamForwarding.java 8 * $ CLASSPATH=.:../build java StreamForwarding 9 * You will be asked username, hostname, host:hostport and passwd. 10 * If everything works fine, System.in and System.out streams will be 11 * forwared to remote port and you can send messages from command line. 12 * 13 */ 14 import com.jcraft.jsch.*; 15 import java.awt.*; 16 import javax.swing.*; 17 18 public class StreamForwarding{ main(String[] arg)19 public static void main(String[] arg){ 20 int port; 21 22 try{ 23 JSch jsch=new JSch(); 24 25 String host=null; 26 if(arg.length>0){ 27 host=arg[0]; 28 } 29 else{ 30 host=JOptionPane.showInputDialog("Enter username@hostname", 31 System.getProperty("user.name")+ 32 "@localhost"); 33 } 34 String user=host.substring(0, host.indexOf('@')); 35 host=host.substring(host.indexOf('@')+1); 36 37 Session session=jsch.getSession(user, host, 22); 38 39 // username and password will be given via UserInfo interface. 40 UserInfo ui=new MyUserInfo(); 41 session.setUserInfo(ui); 42 session.connect(); 43 44 String foo=JOptionPane.showInputDialog("Enter host and port", 45 "host:port"); 46 host=foo.substring(0, foo.indexOf(':')); 47 port=Integer.parseInt(foo.substring(foo.indexOf(':')+1)); 48 49 System.out.println("System.{in,out} will be forwarded to "+ 50 host+":"+port+"."); 51 Channel channel = session.getStreamForwarder(host, port); 52 // InputStream in = channel.getInputStream(); 53 // OutpuStream out = channel.getOutputStream(); 54 channel.setInputStream(System.in); 55 channel.setOutputStream(System.out); 56 channel.connect(1000); 57 } 58 catch(Exception e){ 59 System.out.println(e); 60 } 61 } 62 63 public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ getPassword()64 public String getPassword(){ return passwd; } promptYesNo(String str)65 public boolean promptYesNo(String str){ 66 Object[] options={ "yes", "no" }; 67 int foo=JOptionPane.showOptionDialog(null, 68 str, 69 "Warning", 70 JOptionPane.DEFAULT_OPTION, 71 JOptionPane.WARNING_MESSAGE, 72 null, options, options[0]); 73 return foo==0; 74 } 75 76 String passwd; 77 JTextField passwordField=(JTextField)new JPasswordField(20); 78 getPassphrase()79 public String getPassphrase(){ return null; } promptPassphrase(String message)80 public boolean promptPassphrase(String message){ return true; } promptPassword(String message)81 public boolean promptPassword(String message){ 82 Object[] ob={passwordField}; 83 int result= 84 JOptionPane.showConfirmDialog(null, ob, message, 85 JOptionPane.OK_CANCEL_OPTION); 86 if(result==JOptionPane.OK_OPTION){ 87 passwd=passwordField.getText(); 88 return true; 89 } 90 else{ return false; } 91 } showMessage(String message)92 public void showMessage(String message){ 93 JOptionPane.showMessageDialog(null, message); 94 } 95 final GridBagConstraints gbc = 96 new GridBagConstraints(0,0,1,1,1,1, 97 GridBagConstraints.NORTHWEST, 98 GridBagConstraints.NONE, 99 new Insets(0,0,0,0),0,0); 100 private Container panel; promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo)101 public String[] promptKeyboardInteractive(String destination, 102 String name, 103 String instruction, 104 String[] prompt, 105 boolean[] echo){ 106 panel = new JPanel(); 107 panel.setLayout(new GridBagLayout()); 108 109 gbc.weightx = 1.0; 110 gbc.gridwidth = GridBagConstraints.REMAINDER; 111 gbc.gridx = 0; 112 panel.add(new JLabel(instruction), gbc); 113 gbc.gridy++; 114 115 gbc.gridwidth = GridBagConstraints.RELATIVE; 116 117 JTextField[] texts=new JTextField[prompt.length]; 118 for(int i=0; i<prompt.length; i++){ 119 gbc.fill = GridBagConstraints.NONE; 120 gbc.gridx = 0; 121 gbc.weightx = 1; 122 panel.add(new JLabel(prompt[i]),gbc); 123 124 gbc.gridx = 1; 125 gbc.fill = GridBagConstraints.HORIZONTAL; 126 gbc.weighty = 1; 127 if(echo[i]){ 128 texts[i]=new JTextField(20); 129 } 130 else{ 131 texts[i]=new JPasswordField(20); 132 } 133 panel.add(texts[i], gbc); 134 gbc.gridy++; 135 } 136 137 if(JOptionPane.showConfirmDialog(null, panel, 138 destination+": "+name, 139 JOptionPane.OK_CANCEL_OPTION, 140 JOptionPane.QUESTION_MESSAGE) 141 ==JOptionPane.OK_OPTION){ 142 String[] response=new String[prompt.length]; 143 for(int i=0; i<prompt.length; i++){ 144 response[i]=texts[i].getText(); 145 } 146 return response; 147 } 148 else{ 149 return null; // cancel 150 } 151 } 152 } 153 } 154 155 156