KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > tools > SshShell


1 package net.sf.jftp.tools;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Font JavaDoc;
5 import java.awt.event.KeyAdapter JavaDoc;
6 import java.awt.event.KeyEvent JavaDoc;
7 import java.awt.event.WindowAdapter JavaDoc;
8 import java.awt.event.WindowEvent JavaDoc;
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.BufferedOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.StreamTokenizer JavaDoc;
13 import java.util.Vector JavaDoc;
14
15 import javax.swing.JFrame JavaDoc;
16 import javax.swing.JOptionPane JavaDoc;
17 import javax.swing.JScrollBar JavaDoc;
18 import javax.swing.JScrollPane JavaDoc;
19 import javax.swing.JTextArea JavaDoc;
20
21 import net.sf.jftp.JFtp;
22 import net.sf.jftp.config.Settings;
23 import net.sf.jftp.gui.framework.HFrame;
24 import net.sf.jftp.net.SftpVerification;
25 import net.sf.jftp.system.logging.Log;
26
27 import com.sshtools.common.hosts.DialogHostKeyVerification;
28 import com.sshtools.j2ssh.SshClient;
29 import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
30 import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
31 import com.sshtools.j2ssh.configuration.ConfigurationLoader;
32 import com.sshtools.j2ssh.configuration.SshConnectionProperties;
33 import com.sshtools.j2ssh.session.SessionChannelClient;
34
35
36 public class SshShell extends JFrame JavaDoc implements Runnable JavaDoc
37 {
38     BufferedOutputStream JavaDoc out;
39     BufferedInputStream JavaDoc in;
40     BufferedInputStream JavaDoc err;
41     JTextArea JavaDoc text = new JTextArea JavaDoc(25, 101);
42
43     //JTextField input = new JTextField();
44
long off;
45     Thread JavaDoc runner;
46     JScrollPane JavaDoc textP;
47     int port;
48     SshConnectionProperties properties;
49     String JavaDoc input = "";
50     SshClient ssh;
51     Vector JavaDoc commands = new Vector JavaDoc();
52     int currCmd = 0;
53
54     public SshShell(SshConnectionProperties properties, String JavaDoc user, String JavaDoc pass, int port)
55     {
56         this.port = port;
57         this.properties = properties;
58
59         try
60         {
61             init(user, pass);
62         }
63         catch(Exception JavaDoc e)
64         {
65             e.printStackTrace();
66             Log.debug("ERROR: " + e.getMessage());
67         }
68     }
69
70     public SshShell()
71     {
72         try
73         {
74             String JavaDoc host = JOptionPane.showInternalInputDialog(JFtp.desktop,
75                                                               "Please enter a host:");
76             String JavaDoc user = JOptionPane.showInternalInputDialog(JFtp.desktop,
77                                                               "Please enter your username:");
78             String JavaDoc pass = JOptionPane.showInternalInputDialog(JFtp.desktop,
79                                                               "Please enter your password:");
80
81             if((host != null) && (user != null) && (pass != null))
82             {
83                 init(user, pass);
84             }
85         }
86         catch(Exception JavaDoc e)
87         {
88             e.printStackTrace();
89             Log.debug("ERROR: " + e.getMessage());
90         }
91     }
92
93     public void init(String JavaDoc user, String JavaDoc pass) throws Exception JavaDoc
94     {
95         setTitle("SSH Shell");
96
97         //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
98
addWindowListener(new WindowAdapter JavaDoc()
99             {
100                 public void windowClosing(WindowEvent JavaDoc e)
101                 {
102                     ssh.disconnect();
103                     dispose();
104                 }
105             });
106
107         //setLocation(150, 150);
108
HFrame.fixLocation(this);
109
110         textP = new JScrollPane JavaDoc(text);
111         text.setFont(new Font JavaDoc("Monospaced", Font.TRUETYPE_FONT, 10));
112
113         //text.setCaretColor(Color.black);
114
/*
115                 text.setCaret(new DefaultCaret()
116                 {
117                              public void focusGained(FocusEvent e)
118                              {
119                                  super.focusGained(e);
120                                   setVisible(true);
121                               }
122
123                              public void focusLost(FocusEvent e)
124                              {
125                                  super.focusLost(e);
126                                   setVisible(true);
127                               }
128
129                 });
130         */

131         getContentPane().setLayout(new BorderLayout JavaDoc(5, 5));
132         getContentPane().add("Center", textP);
133
134         //getContentPane().add("South", input);
135
text.setEditable(false);
136         text.setLineWrap(true);
137         
138         setBackground(text.getBackground());
139
140         text.addKeyListener(new KeyAdapter JavaDoc()
141             {
142                 public void keyPressed(KeyEvent JavaDoc e)
143                 {
144                     if((e.getKeyCode() == KeyEvent.VK_BACK_SPACE) &&
145                            (input.length() > 0))
146                     {
147                         input = input.substring(0, input.length() - 1);
148
149                         String JavaDoc t = text.getText();
150                         t = t.substring(0, t.length() - 1);
151                         text.setText(t);
152                     }
153                     else if(e.getKeyCode() == KeyEvent.VK_UP)
154                     {
155                         String JavaDoc t = text.getText();
156                         t = t.substring(0, t.length() - input.length());
157
158                         if((currCmd <= commands.size()) && (currCmd > 0))
159                         {
160                             currCmd--;
161
162                             String JavaDoc cmd = (String JavaDoc) commands.get(currCmd);
163                             input = cmd.substring(0, cmd.length() - 1);
164                             text.setText(t + input);
165                         }
166                     }
167                     else if(e.getKeyCode() == KeyEvent.VK_DOWN)
168                     {
169                         String JavaDoc t = text.getText();
170                         t = t.substring(0, t.length() - input.length());
171
172                         if(((currCmd + 1) < commands.size()) && (currCmd >= 0))
173                         {
174                             currCmd++;
175
176                             String JavaDoc cmd = (String JavaDoc) commands.get(currCmd);
177                             input = cmd.substring(0, cmd.length() - 1);
178                             text.setText(t + input);
179                         }
180                     }
181                     else if(e.getKeyCode() != KeyEvent.VK_SHIFT)
182                     {
183                         //Char c = new Char(e.getKeyChar());
184
if(!e.isActionKey())
185                         {
186                             input += e.getKeyChar();
187                             text.append("" + e.getKeyChar());
188                         }
189                     }
190
191                     if(e.getKeyCode() == KeyEvent.VK_ENTER)
192                     {
193                         send();
194                     }
195                 }
196             });
197
198         ConfigurationLoader.initialize(false);
199
200         ssh = new SshClient();
201         ssh.setSocketTimeout(30000);
202
203         //SshConnectionProperties properties = new SshConnectionProperties();
204
//properties.setHost(host);
205
//properties.setPort(port);
206
//properties.setPrefPublicKey("ssh-dss");
207
if(Settings.getEnableSshKeys())
208         {
209             ssh.connect(properties, new DialogHostKeyVerification(this));
210         }
211         else
212         {
213             ssh.connect(properties,
214                         new SftpVerification(Settings.sshHostKeyVerificationFile));
215         }
216
217         PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
218         pwd.setUsername(user);
219         pwd.setPassword(pass);
220
221         int result = ssh.authenticate(pwd);
222
223         if(result == AuthenticationProtocolState.COMPLETE)
224         {
225             SessionChannelClient session = ssh.openSessionChannel();
226
227             if(!session.requestPseudoTerminal("vt100", 80, 24, 0, 0, ""))
228             {
229                 Log.debug("ERROR: Could not open terminal");
230             }
231
232             if(session.startShell())
233             {
234                 out = new BufferedOutputStream JavaDoc(session.getOutputStream());
235                 in = new BufferedInputStream JavaDoc(session.getInputStream());
236                 err = new BufferedInputStream JavaDoc(session.getStderrInputStream());
237
238                 //session.getState().waitForState(ChannelState.CHANNEL_CLOSED);
239
}
240             else
241             {
242                 Log.debug("ERROR: Could not start shell");
243             }
244         }
245         else
246         {
247             ssh.disconnect();
248         }
249
250         pack();
251         setVisible(true);
252
253         runner = new Thread JavaDoc(this);
254         runner.start();
255
256         toFront();
257         text.requestFocus();
258     }
259
260     public void run()
261     {
262         try
263         {
264             byte[] b = new byte[4096];
265             int i;
266
267             while((i = in.read(b)) != StreamTokenizer.TT_EOF)
268             {
269                 text.append(new String JavaDoc(b, 0, i));
270
271                 //Log.out("recv: "+i+" -> "+new String(b));
272
while(err.available() > 0)
273                 {
274                     err.read(b);
275                     text.append(new String JavaDoc(b, 0, i));
276                 }
277
278                 while(text.getRows() > 500)
279                 {
280                     String JavaDoc t = text.getText();
281                     t = t.substring(250);
282
283                     text.setText(t);
284                 }
285
286                 try
287                 {
288                     Thread.sleep(100);
289                 }
290                 catch(Exception JavaDoc ex)
291                 {
292                     ex.printStackTrace();
293                 }
294
295                 JScrollBar JavaDoc bar = textP.getVerticalScrollBar();
296                 bar.setValue(bar.getMaximum());
297             }
298         }
299         catch(Exception JavaDoc ex)
300         {
301             ex.printStackTrace();
302             Log.debug("ERROR: " + ex.getMessage());
303             this.dispose();
304         }
305     }
306
307     private void send()
308     {
309         try
310         {
311             String JavaDoc msg = input;
312             input = "";
313
314             out.write(msg.getBytes());
315             out.flush();
316
317             commands.add(msg);
318             currCmd = commands.size();
319
320             //Log.out("send: "+msg);
321
}
322         catch(IOException JavaDoc ex)
323         {
324             ex.printStackTrace();
325             Log.debug("ERROR: " + ex.getMessage());
326             this.dispose();
327         }
328     }
329
330     public static void main(String JavaDoc[] args)
331     {
332         new SshShell();
333     }
334 }
335
Popular Tags