implement a debug mode
[saartuer.git] / JavaTuer / src / de / hacksaar / javatuer / TyshellClient.java
1 package de.hacksaar.javatuer;
2
3 import com.jcraft.jsch.JSchException;
4 import com.jcraft.jsch.UserInfo;
5
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.io.OutputStreamWriter;
9
10 public class TyshellClient {
11         private static final String TAG = "TyshellClient";
12         public static final char END_OF_COMMAND = '\n';
13         private final String hostname;
14         private final int port;
15         private final TuerLogging log;
16         private SshClient client;
17         private InputStreamReader inputStream;
18         private OutputStreamWriter outputStream;
19
20         public TyshellClient(String hostname, int port) {
21                 this(hostname, port, new DummyLogging());
22         }
23
24         public TyshellClient(String hostname, int port, TuerLogging log) {
25                 this.log = log;
26                 this.hostname = hostname;
27                 this.port = port;
28         }
29
30         public void connect(String username, String password) {
31                 try {
32                         client = new SshClient(hostname, username, port, log);
33                         client.login(password);
34                         inputStream = new InputStreamReader(client.getInputStream());
35                         outputStream = new OutputStreamWriter(client.getOutputStream());
36                 } catch (JSchException | IOException e) {
37                         log.exception(TAG, e);
38                 }
39         }
40
41         public void connect(String username, String keyFile, String passphrase) {
42                 try {
43                         client = new SshClient(hostname, username, port, log);
44                         client.addPrivateKey(keyFile, passphrase);
45                         client.login();
46                         inputStream = new InputStreamReader(client.getInputStream());
47                         outputStream = new OutputStreamWriter(client.getOutputStream());
48                 } catch (JSchException | IOException e) {
49                         log.exception(TAG, e);
50                 }
51         }
52
53         public void connect(String username, String keyFile, UserInfo interactiveLogin) {
54                 try {
55                         client = new SshClient(hostname, username, port, log);
56                         client.addPrivateKey(keyFile);
57                         client.login(interactiveLogin);
58                         inputStream = new InputStreamReader(client.getInputStream());
59                         outputStream = new OutputStreamWriter(client.getOutputStream());
60                 } catch (JSchException | IOException e) {
61                         log.exception(TAG, e);
62                 }
63         }
64
65         public void disconnect() {
66                 try {
67                         inputStream.close();
68                         outputStream.close();
69                 } catch (IOException e) {
70                         log.exception(TAG, e);
71                 }
72                 client.disconnect();
73         }
74
75         boolean isConnected() {
76                 return (client.isConnected() && inputStream != null && outputStream != null);
77         }
78
79         public void sendCommand(String command) {
80                 if (isConnected()) {
81                         try {
82                                 assert outputStream != null;
83                                 log.debug(TAG, "Sending: " + command);
84                                 outputStream.write((command + END_OF_COMMAND).toCharArray());
85                                 outputStream.flush();
86                         } catch (IOException e) {
87                                 log.exception(TAG, e);
88                         }
89                 }
90         }
91 }