001: package net.sf.jftp.tools;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Font;
005: import java.awt.event.KeyAdapter;
006: import java.awt.event.KeyEvent;
007: import java.awt.event.WindowAdapter;
008: import java.awt.event.WindowEvent;
009: import java.io.BufferedInputStream;
010: import java.io.BufferedOutputStream;
011: import java.io.IOException;
012: import java.io.StreamTokenizer;
013: import java.util.Vector;
014:
015: import javax.swing.JFrame;
016: import javax.swing.JOptionPane;
017: import javax.swing.JScrollBar;
018: import javax.swing.JScrollPane;
019: import javax.swing.JTextArea;
020:
021: import net.sf.jftp.JFtp;
022: import net.sf.jftp.config.Settings;
023: import net.sf.jftp.gui.framework.HFrame;
024: import net.sf.jftp.net.SftpVerification;
025: import net.sf.jftp.system.logging.Log;
026:
027: import com.sshtools.common.hosts.DialogHostKeyVerification;
028: import com.sshtools.j2ssh.SshClient;
029: import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
030: import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
031: import com.sshtools.j2ssh.configuration.ConfigurationLoader;
032: import com.sshtools.j2ssh.configuration.SshConnectionProperties;
033: import com.sshtools.j2ssh.session.SessionChannelClient;
034:
035: public class SshShell extends JFrame implements Runnable {
036: BufferedOutputStream out;
037: BufferedInputStream in;
038: BufferedInputStream err;
039: JTextArea text = new JTextArea(25, 101);
040:
041: //JTextField input = new JTextField();
042: long off;
043: Thread runner;
044: JScrollPane textP;
045: int port;
046: SshConnectionProperties properties;
047: String input = "";
048: SshClient ssh;
049: Vector commands = new Vector();
050: int currCmd = 0;
051:
052: public SshShell(SshConnectionProperties properties, String user,
053: String pass, int port) {
054: this .port = port;
055: this .properties = properties;
056:
057: try {
058: init(user, pass);
059: } catch (Exception e) {
060: e.printStackTrace();
061: Log.debug("ERROR: " + e.getMessage());
062: }
063: }
064:
065: public SshShell() {
066: try {
067: String host = JOptionPane.showInternalInputDialog(
068: JFtp.desktop, "Please enter a host:");
069: String user = JOptionPane.showInternalInputDialog(
070: JFtp.desktop, "Please enter your username:");
071: String pass = JOptionPane.showInternalInputDialog(
072: JFtp.desktop, "Please enter your password:");
073:
074: if ((host != null) && (user != null) && (pass != null)) {
075: init(user, pass);
076: }
077: } catch (Exception e) {
078: e.printStackTrace();
079: Log.debug("ERROR: " + e.getMessage());
080: }
081: }
082:
083: public void init(String user, String pass) throws Exception {
084: setTitle("SSH Shell");
085:
086: //setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
087: addWindowListener(new WindowAdapter() {
088: public void windowClosing(WindowEvent e) {
089: ssh.disconnect();
090: dispose();
091: }
092: });
093:
094: //setLocation(150, 150);
095: HFrame.fixLocation(this );
096:
097: textP = new JScrollPane(text);
098: text.setFont(new Font("Monospaced", Font.TRUETYPE_FONT, 10));
099:
100: //text.setCaretColor(Color.black);
101: /*
102: text.setCaret(new DefaultCaret()
103: {
104: public void focusGained(FocusEvent e)
105: {
106: super.focusGained(e);
107: setVisible(true);
108: }
109:
110: public void focusLost(FocusEvent e)
111: {
112: super.focusLost(e);
113: setVisible(true);
114: }
115:
116: });
117: */
118: getContentPane().setLayout(new BorderLayout(5, 5));
119: getContentPane().add("Center", textP);
120:
121: //getContentPane().add("South", input);
122: text.setEditable(false);
123: text.setLineWrap(true);
124:
125: setBackground(text.getBackground());
126:
127: text.addKeyListener(new KeyAdapter() {
128: public void keyPressed(KeyEvent e) {
129: if ((e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
130: && (input.length() > 0)) {
131: input = input.substring(0, input.length() - 1);
132:
133: String t = text.getText();
134: t = t.substring(0, t.length() - 1);
135: text.setText(t);
136: } else if (e.getKeyCode() == KeyEvent.VK_UP) {
137: String t = text.getText();
138: t = t.substring(0, t.length() - input.length());
139:
140: if ((currCmd <= commands.size()) && (currCmd > 0)) {
141: currCmd--;
142:
143: String cmd = (String) commands.get(currCmd);
144: input = cmd.substring(0, cmd.length() - 1);
145: text.setText(t + input);
146: }
147: } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
148: String t = text.getText();
149: t = t.substring(0, t.length() - input.length());
150:
151: if (((currCmd + 1) < commands.size())
152: && (currCmd >= 0)) {
153: currCmd++;
154:
155: String cmd = (String) commands.get(currCmd);
156: input = cmd.substring(0, cmd.length() - 1);
157: text.setText(t + input);
158: }
159: } else if (e.getKeyCode() != KeyEvent.VK_SHIFT) {
160: //Char c = new Char(e.getKeyChar());
161: if (!e.isActionKey()) {
162: input += e.getKeyChar();
163: text.append("" + e.getKeyChar());
164: }
165: }
166:
167: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
168: send();
169: }
170: }
171: });
172:
173: ConfigurationLoader.initialize(false);
174:
175: ssh = new SshClient();
176: ssh.setSocketTimeout(30000);
177:
178: //SshConnectionProperties properties = new SshConnectionProperties();
179: //properties.setHost(host);
180: //properties.setPort(port);
181: //properties.setPrefPublicKey("ssh-dss");
182: if (Settings.getEnableSshKeys()) {
183: ssh
184: .connect(properties, new DialogHostKeyVerification(
185: this ));
186: } else {
187: ssh.connect(properties, new SftpVerification(
188: Settings.sshHostKeyVerificationFile));
189: }
190:
191: PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
192: pwd.setUsername(user);
193: pwd.setPassword(pass);
194:
195: int result = ssh.authenticate(pwd);
196:
197: if (result == AuthenticationProtocolState.COMPLETE) {
198: SessionChannelClient session = ssh.openSessionChannel();
199:
200: if (!session.requestPseudoTerminal("vt100", 80, 24, 0, 0,
201: "")) {
202: Log.debug("ERROR: Could not open terminal");
203: }
204:
205: if (session.startShell()) {
206: out = new BufferedOutputStream(session
207: .getOutputStream());
208: in = new BufferedInputStream(session.getInputStream());
209: err = new BufferedInputStream(session
210: .getStderrInputStream());
211:
212: //session.getState().waitForState(ChannelState.CHANNEL_CLOSED);
213: } else {
214: Log.debug("ERROR: Could not start shell");
215: }
216: } else {
217: ssh.disconnect();
218: }
219:
220: pack();
221: setVisible(true);
222:
223: runner = new Thread(this );
224: runner.start();
225:
226: toFront();
227: text.requestFocus();
228: }
229:
230: public void run() {
231: try {
232: byte[] b = new byte[4096];
233: int i;
234:
235: while ((i = in.read(b)) != StreamTokenizer.TT_EOF) {
236: text.append(new String(b, 0, i));
237:
238: //Log.out("recv: "+i+" -> "+new String(b));
239: while (err.available() > 0) {
240: err.read(b);
241: text.append(new String(b, 0, i));
242: }
243:
244: while (text.getRows() > 500) {
245: String t = text.getText();
246: t = t.substring(250);
247:
248: text.setText(t);
249: }
250:
251: try {
252: Thread.sleep(100);
253: } catch (Exception ex) {
254: ex.printStackTrace();
255: }
256:
257: JScrollBar bar = textP.getVerticalScrollBar();
258: bar.setValue(bar.getMaximum());
259: }
260: } catch (Exception ex) {
261: ex.printStackTrace();
262: Log.debug("ERROR: " + ex.getMessage());
263: this .dispose();
264: }
265: }
266:
267: private void send() {
268: try {
269: String msg = input;
270: input = "";
271:
272: out.write(msg.getBytes());
273: out.flush();
274:
275: commands.add(msg);
276: currCmd = commands.size();
277:
278: //Log.out("send: "+msg);
279: } catch (IOException ex) {
280: ex.printStackTrace();
281: Log.debug("ERROR: " + ex.getMessage());
282: this .dispose();
283: }
284: }
285:
286: public static void main(String[] args) {
287: new SshShell();
288: }
289: }
|