implement a debug mode
[saartuer.git] / JavaTuer / src / de / hacksaar / javatuer / SshClient.java
1 package de.hacksaar.javatuer;
2
3 import com.jcraft.jsch.*;
4
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8
9 public class SshClient {
10         private static final String TAG = "SshClient";
11         private final String host;
12         private final String username;
13         private final int port;
14         private final JSch jSch;
15         private final TuerLogging log;
16         private Session session;
17         private Channel channel;
18
19         public SshClient(String host, String username, int port, TuerLogging log) {
20                 this.host = host;
21                 this.username = username;
22                 this.port = port;
23                 this.log = log;
24                 JSch.setLogger(log);
25                 jSch = new JSch();
26         }
27
28         public void addPrivateKey(String keyFilePath) throws JSchException {
29                 jSch.addIdentity(keyFilePath);
30         }
31
32         public void addPrivateKey(String keyFilePath, String passphrase) throws JSchException {
33                 jSch.addIdentity(keyFilePath, passphrase);
34         }
35
36         public void disconnect() {
37                 channel.disconnect();
38                 channel = null;
39                 session.disconnect();
40                 session = null;
41         }
42
43         public String getHost() {
44                 return host;
45         }
46
47         public InputStream getInputStream() throws IOException {
48                 if (channel == null) {
49                         return null;
50                 }
51                 return channel.getInputStream();
52         }
53
54         public OutputStream getOutputStream() throws IOException {
55                 if (channel == null) {
56                         return null;
57                 }
58                 return channel.getOutputStream();
59         }
60
61         public int getPort() {
62                 return port;
63         }
64
65         public String getUsername() {
66                 return username;
67         }
68
69         public boolean isConnected() {
70                 return channel != null && channel.isConnected() && session != null && session.isConnected();
71         }
72
73         public void login(final String password) throws JSchException {
74                 login(new UserInfo() {
75
76                         @Override
77                         public String getPassphrase() {
78                                 return null;
79                         }
80
81                         @Override
82                         public String getPassword() {
83                                 return password;
84                         }
85
86                         @Override
87                         public boolean promptPassphrase(String s) {
88                                 return false;
89                         }
90
91                         @Override
92                         public boolean promptPassword(String s) {
93                                 return true;
94                         }
95
96                         @Override
97                         public boolean promptYesNo(String s) {
98                                 return false;
99                         }
100
101                         @Override
102                         public void showMessage(String s) {
103                                 log.info(TAG, s);
104                         }
105                 });
106         }
107
108         public void login() throws JSchException {
109                 login(new UserInfo() {
110
111                         @Override
112                         public String getPassphrase() {
113                                 return null;
114                         }
115
116                         @Override
117                         public String getPassword() {
118                                 return null;
119                         }
120
121                         @Override
122                         public boolean promptPassphrase(String s) {
123                                 return false;
124                         }
125
126                         @Override
127                         public boolean promptPassword(String s) {
128                                 return false;
129                         }
130
131                         @Override
132                         public boolean promptYesNo(String s) {
133                                 return false;
134                         }
135
136                         @Override
137                         public void showMessage(String s) {
138                                 log.info(TAG, s);
139                         }
140                 });
141         }
142
143         public void login(UserInfo userInfo) throws JSchException {
144                 log.debug(TAG, "Creating session");
145                 if (session == null) {
146                         session = jSch.getSession(username, host, port);
147                 }
148                 if (!session.isConnected()) {
149                         session.setUserInfo(userInfo);
150                         log.debug(TAG, "Connecting");
151                         session.connect();
152                         log.debug(TAG, "Opening channel");
153                         channel = session.openChannel("shell");
154                         channel.connect();
155                 }
156         }
157
158 }