1 2 import com.jcraft.jsch.*; 3 import java.awt.*; 4 import javax.swing.*; 5 6 public class Subsystem{ 7 public static void main(String [] arg){ 8 try{ 9 JSch jsch=new JSch(); 10 11 String host=JOptionPane.showInputDialog("Enter username@hostname", 12 System.getProperty("user.name")+ 13 "@localhost"); 14 String user=host.substring(0, host.indexOf('@')); 15 host=host.substring(host.indexOf('@')+1); 16 17 Session session=jsch.getSession(user, host, 22); 18 19 UserInfo ui=new MyUserInfo(); 20 session.setUserInfo(ui); 21 session.connect(); 22 23 String subsystem=JOptionPane.showInputDialog("Enter subsystem name", ""); 24 25 Channel channel=session.openChannel("subsystem"); 26 ((ChannelSubsystem)channel).setSubsystem(subsystem); 27 ((ChannelSubsystem)channel).setPty(true); 28 29 channel.setInputStream(System.in); 30 ((ChannelSubsystem)channel).setErrStream(System.err); 31 channel.setOutputStream(System.out); 32 channel.connect(); 33 34 56 } 57 catch(Exception e){ 58 System.out.println(e); 59 } 60 } 61 62 public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ 63 public String getPassword(){ return passwd; } 64 public boolean promptYesNo(String str){ 65 Object [] options={ "yes", "no" }; 66 int foo=JOptionPane.showOptionDialog(null, 67 str, 68 "Warning", 69 JOptionPane.DEFAULT_OPTION, 70 JOptionPane.WARNING_MESSAGE, 71 null, options, options[0]); 72 return foo==0; 73 } 74 75 String passwd; 76 JTextField passwordField=(JTextField)new JPasswordField(20); 77 78 public String getPassphrase(){ return null; } 79 public boolean promptPassphrase(String message){ return true; } 80 public boolean promptPassword(String message){ 81 Object [] ob={passwordField}; 82 int result= 83 JOptionPane.showConfirmDialog(null, ob, message, 84 JOptionPane.OK_CANCEL_OPTION); 85 if(result==JOptionPane.OK_OPTION){ 86 passwd=passwordField.getText(); 87 return true; 88 } 89 else{ 90 return false; 91 } 92 } 93 public void showMessage(String message){ 94 JOptionPane.showMessageDialog(null, message); 95 } 96 final GridBagConstraints gbc = 97 new GridBagConstraints(0,0,1,1,1,1, 98 GridBagConstraints.NORTHWEST, 99 GridBagConstraints.NONE, 100 new Insets(0,0,0,0),0,0); 101 private Container panel; 102 public String [] promptKeyboardInteractive(String destination, 103 String name, 104 String instruction, 105 String [] prompt, 106 boolean[] echo){ 107 panel = new JPanel(); 108 panel.setLayout(new GridBagLayout()); 109 110 gbc.weightx = 1.0; 111 gbc.gridwidth = GridBagConstraints.REMAINDER; 112 gbc.gridx = 0; 113 panel.add(new JLabel(instruction), gbc); 114 gbc.gridy++; 115 116 gbc.gridwidth = GridBagConstraints.RELATIVE; 117 118 JTextField[] texts=new JTextField[prompt.length]; 119 for(int i=0; i<prompt.length; i++){ 120 gbc.fill = GridBagConstraints.NONE; 121 gbc.gridx = 0; 122 gbc.weightx = 1; 123 panel.add(new JLabel(prompt[i]),gbc); 124 125 gbc.gridx = 1; 126 gbc.fill = GridBagConstraints.HORIZONTAL; 127 gbc.weighty = 1; 128 if(echo[i]){ 129 texts[i]=new JTextField(20); 130 } 131 else{ 132 texts[i]=new JPasswordField(20); 133 } 134 panel.add(texts[i], gbc); 135 gbc.gridy++; 136 } 137 138 if(JOptionPane.showConfirmDialog(null, panel, 139 destination+": "+name, 140 JOptionPane.OK_CANCEL_OPTION, 141 JOptionPane.QUESTION_MESSAGE) 142 ==JOptionPane.OK_OPTION){ 143 String [] response=new String [prompt.length]; 144 for(int i=0; i<prompt.length; i++){ 145 response[i]=texts[i].getText(); 146 } 147 return response; 148 } 149 else{ 150 return null; } 152 } 153 } 154 } 155 | Popular Tags |