11
11
import com .jcraft .jsch .JSchException ;
12
12
import com .jcraft .jsch .KeyPair ;
13
13
import com .jcraft .jsch .Session ;
14
- import expect4j .Closure ;
15
- import expect4j .Expect4j ;
16
- import expect4j .ExpectState ;
17
- import expect4j .matches .Match ;
18
- import expect4j .matches .RegExpMatch ;
19
- import org .apache .oro .text .regex .MalformedPatternException ;
20
14
21
15
import java .io .BufferedOutputStream ;
22
16
import java .io .ByteArrayOutputStream ;
23
17
import java .io .IOException ;
24
18
import java .io .InputStream ;
25
19
import java .io .UnsupportedEncodingException ;
26
20
import java .nio .charset .StandardCharsets ;
27
- import java .util .ArrayList ;
28
21
import java .util .Hashtable ;
29
- import java .util .List ;
30
22
31
23
/**
32
24
* Utility class to run commands on Linux VM via SSH.
33
25
*/
34
26
public final class SSHShell {
35
27
private final Session session ;
36
28
private final ChannelShell channel ;
37
- private final Expect4j expect ;
38
- private final StringBuilder shellBuffer = new StringBuilder ();
39
- private List <Match > linuxPromptMatches = new ArrayList <>();
40
29
41
30
/**
42
31
* Creates SSHShell.
@@ -45,21 +34,9 @@ public final class SSHShell {
45
34
* @param port the ssh port
46
35
* @param userName the ssh user name
47
36
* @param password the ssh password
48
- * @return the shell
49
37
* @throws JSchException the JSchException
50
- * @throws IOException the IOException
51
38
*/
52
- private SSHShell (String host , int port , String userName , String password )
53
- throws JSchException , IOException {
54
- Closure expectClosure = getExpectClosure ();
55
- for (String linuxPromptPattern : new String []{"\\ >" , "#" , "~#" , "~\\ $" }) {
56
- try {
57
- Match match = new RegExpMatch (linuxPromptPattern , expectClosure );
58
- linuxPromptMatches .add (match );
59
- } catch (MalformedPatternException malformedEx ) {
60
- throw new RuntimeException (malformedEx );
61
- }
62
- }
39
+ private SSHShell (String host , int port , String userName , String password ) throws JSchException {
63
40
JSch jsch = new JSch ();
64
41
this .session = jsch .getSession (userName , host , port );
65
42
session .setPassword (password );
@@ -68,41 +45,6 @@ private SSHShell(String host, int port, String userName, String password)
68
45
session .setConfig (config );
69
46
session .connect (60000 );
70
47
this .channel = (ChannelShell ) session .openChannel ("shell" );
71
- this .expect = new Expect4j (channel .getInputStream (), channel .getOutputStream ());
72
- channel .connect ();
73
- }
74
-
75
- /**
76
- * Creates SSHShell.
77
- *
78
- * @param host the host name
79
- * @param port the ssh port
80
- * @param userName the ssh user name
81
- * @param sshPrivateKey the ssh password
82
- * @return the shell
83
- * @throws JSchException the JSchException
84
- * @throws IOException the IOException
85
- */
86
- private SSHShell (String host , int port , String userName , byte [] sshPrivateKey )
87
- throws JSchException , IOException {
88
- Closure expectClosure = getExpectClosure ();
89
- for (String linuxPromptPattern : new String []{"\\ >" , "#" , "~#" , "~\\ $" }) {
90
- try {
91
- Match match = new RegExpMatch (linuxPromptPattern , expectClosure );
92
- linuxPromptMatches .add (match );
93
- } catch (MalformedPatternException malformedEx ) {
94
- throw new RuntimeException (malformedEx );
95
- }
96
- }
97
- JSch jsch = new JSch ();
98
- jsch .setKnownHosts (System .getProperty ("user.home" ) + "/.ssh/known_hosts" );
99
- jsch .addIdentity (host , sshPrivateKey , (byte []) null , (byte []) null );
100
- this .session = jsch .getSession (userName , host , port );
101
- this .session .setConfig ("StrictHostKeyChecking" , "no" );
102
- this .session .setConfig ("PreferredAuthentications" , "publickey,keyboard-interactive,password" );
103
- session .connect (60000 );
104
- this .channel = (ChannelShell ) session .openChannel ("shell" );
105
- this .expect = new Expect4j (channel .getInputStream (), channel .getOutputStream ());
106
48
channel .connect ();
107
49
}
108
50
@@ -122,45 +64,6 @@ public static SSHShell open(String host, int port, String userName, String passw
122
64
return new SSHShell (host , port , userName , password );
123
65
}
124
66
125
- /**
126
- * Opens a SSH shell.
127
- *
128
- * @param host the host name
129
- * @param port the ssh port
130
- * @param userName the ssh user name
131
- * @param sshPrivateKey the ssh private key
132
- * @return the shell
133
- * @throws JSchException exception thrown
134
- * @throws IOException IO exception thrown
135
- */
136
- public static SSHShell open (String host , int port , String userName , byte [] sshPrivateKey )
137
- throws JSchException , IOException {
138
- return new SSHShell (host , port , userName , sshPrivateKey );
139
- }
140
-
141
- /**
142
- * Runs a given list of commands in the shell.
143
- *
144
- * @param commands the commands
145
- * @return the result
146
- * @throws Exception exception thrown
147
- */
148
- public String runCommands (List <String > commands ) throws Exception {
149
- String output = null ;
150
- try {
151
- for (String command : commands ) {
152
- expect .expect (this .linuxPromptMatches );
153
- expect .send (command );
154
- expect .send ("\r " );
155
- expect .expect (this .linuxPromptMatches );
156
- }
157
- output = shellBuffer .toString ();
158
- } finally {
159
- shellBuffer .setLength (0 );
160
- }
161
- return output ;
162
- }
163
-
164
67
/**
165
68
* Executes a command on the remote host.
166
69
*
@@ -278,9 +181,6 @@ public void upload(InputStream from, String fileName, String toPath, boolean isU
278
181
* Closes shell.
279
182
*/
280
183
public void close () {
281
- if (expect != null ) {
282
- expect .close ();
283
- }
284
184
if (channel != null ) {
285
185
channel .disconnect ();
286
186
}
@@ -289,20 +189,6 @@ public void close() {
289
189
}
290
190
}
291
191
292
- private Closure getExpectClosure () {
293
- return new Closure () {
294
- /**
295
- * @throws Exception thrown Exception
296
- */
297
- public void run (ExpectState expectState ) throws Exception {
298
- String outputBuffer = expectState .getBuffer ();
299
- System .out .println (outputBuffer );
300
- shellBuffer .append (outputBuffer );
301
- expectState .exp_continue ();
302
- }
303
- };
304
- }
305
-
306
192
/**
307
193
* Automatically generate SSH keys.
308
194
*
0 commit comments