forked from sbabcoc/Remote-Session
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SshUtils.java
784 lines (701 loc) · 30.1 KB
/
SshUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
package com.nordstrom.remote;
import static java.lang.Thread.sleep;
import static org.apache.commons.io.FilenameUtils.getFullPath;
import static org.apache.commons.io.FilenameUtils.getName;
import static org.apache.commons.lang3.StringUtils.trim;
import com.google.common.collect.ImmutableMap;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.nordstrom.remote.RemoteConfig.RemoteSettings;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Map;
import java.util.Properties;
/**
* This class includes several methods for interacting with remote hosts via SSH through the {@link JSch} library.
* The implementation was copied verbatim from a post on
* <a href='http://stackoverflow.com/questions/2405885/run-a-command-over-ssh-with-jsch'>Stack Overflow</a>.
* JavaDoc has been added for completeness and comprehensibility.
*
* <p>Usage:</p>
* <pre><code> String remoteCommandOutput = exec("ssh://user:pass@host/work/dir/path", "ls -t | head -n1");
* String remoteShellOutput = shell("ssh://user:pass@host/work/dir/path", "ls");
* shell("ssh://user:pass@host/work/dir/path", "ls", System.out);
* shell("ssh://user:pass@host", System.in, System.out);
* sftp("file:/C:/home/file.txt", "ssh://user:pass@host/home");
* sftp("ssh://user:pass@host/home/file.txt", "file:/C:/home");</code></pre>
*
* @author <a href='http://stackoverflow.com/users/448078/mykhaylo-adamovych'>Mykhaylo Adamovych</a>
*
*/
public final class SshUtils {
private static final Logger LOG = LoggerFactory.getLogger(SshUtils.class);
private static final String SSH = "ssh";
private static final String FILE = "file";
public enum ChannelType {
SESSION("session"),
SHELL("shell"),
EXEC("exec"),
X11("x11"),
AGENT_FORWARDING("auth-agent@openssh.com"),
DIRECT_TCPIP("direct-tcpip"),
FORWARDED_TCPIP("forwarded-tcpip"),
SFTP("sftp"),
SUBSYSTEM("subsystem");
private String name;
ChannelType(String name) {
this.name = name;
}
}
private SshUtils() {
throw new AssertionError("SshUtils is a static utility class that cannot be instantiated");
}
/**
* Perform the specified SSH file transfer.
*
* <pre><code> sftp("file:/C:/home/file.txt", "ssh://user:pass@host/home");
* sftp("ssh://user:pass@host/home/file.txt", "file:/C:/home");</code></pre>
*
* <p><b>NOTE</b>: The transferred file retains its original name. If specified, the name component of {@code toUrl}
* will be ignored.<br>
* <b>NOTE</b>: As indicated by the examples, source and target URIs must refer to opposing locations:
* {@code file} for local file system and {@code ssh} for remote file system.</p>
*
* <p>For upload: <b>fromUri</b> = {@code file}; <b>toUri</b> = {@code ssh}<br>
* For download: <b>fromUri</b> = {@code ssh}; <b>toUri</b> = {@code file}</p>
*
* @param fromUri source file {@link URI} as a string
* @param toUri target folder {@link URI} as a string
*/
public static void sftp(String fromUri, String toUri) {
URI from = URI.create(fromUri);
URI to = URI.create(toUri);
if (SSH.equals(to.getScheme()) && FILE.equals(from.getScheme())) {
upload(from, to);
} else if (SSH.equals(from.getScheme()) && FILE.equals(to.getScheme())) {
download(from, to);
} else {
throw new IllegalArgumentException("Source and target URIs must refer to opposing locations");
}
}
/**
* Perform the specified SSH file upload.
*
* @param from source local file URI ({@code file} protocol)
* @param to target remote folder URI ({@code ssh} protocol)
*/
private static void upload(URI from, URI to) {
try (SessionHolder<ChannelSftp> session = new SessionHolder<>(ChannelType.SFTP, to);
FileInputStream fis = new FileInputStream(new File(from))) {
LOG.info("Uploading {} --> {}", from, session.getMaskedUri());
ChannelSftp channel = session.getChannel();
channel.connect();
channel.cd(to.getPath());
channel.put(fis, getName(from.getPath()));
} catch (Exception e) {
throw new RemoteFileUploadFailedException("Cannot upload file", e);
}
}
/**
* Perform the specified SSH file download.
*
* @param from source remote file URI ({@code ssh} protocol)
* @param to target local folder URI ({@code file} protocol)
*/
private static void download(URI from, URI to) {
File out = new File(new File(to), getName(from.getPath()));
try (SessionHolder<ChannelSftp> session = new SessionHolder<>(ChannelType.SFTP, from);
OutputStream os = new FileOutputStream(out);
BufferedOutputStream bos = new BufferedOutputStream(os)) {
LOG.info("Downloading {} --> {}", session.getMaskedUri(), to);
ChannelSftp channel = session.getChannel();
channel.connect();
channel.cd(getFullPath(from.getPath()));
channel.get(getName(from.getPath()), bos);
} catch (Exception e) {
throw new RemoteFileDownloadFailedException("Cannot download file", e);
}
}
/**
* Open an SSH shell session with the specified input and output streams.
*
* <pre><code> shell("ssh://user:pass@host", System.in, System.out);</code></pre>
*
* @param connectUri SSH connection URI
* @param is input stream object
* @param os output stream object
*/
public static void shell(String connectUri, InputStream is, OutputStream os) {
try (SessionHolder<ChannelShell> session = new SessionHolder<>(ChannelType.SHELL, URI.create(connectUri))) {
shell(session, is, os);
}
}
/**
* Open an SSH shell session and execute the specified command.
*
* <pre><code> String remoteOutput = shell("ssh://user:pass@host/work/dir/path", "ls");</code></pre>
*
* @param connectUri SSH connection URI
* @param command shell command string
* @return shell command output
*/
public static String shell(String connectUri, String command) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
shell(connectUri, command, baos);
return baos.toString();
} catch (RuntimeException e) {
LOG.warn(baos.toString());
throw e;
}
}
/**
* Open an SSH shell session and execute the specified script, using the provided stream for output.
*
* <pre><code> shell("ssh://user:pass@host/work/dir/path", "ls", System.out);</code></pre>
*
* @param connectUri SSH connection URI
* @param script shell command string
* @param out output stream object
*/
public static void shell(String connectUri, String script, OutputStream out) {
try (SessionHolder<ChannelShell> session = new SessionHolder<>(ChannelType.SHELL, URI.create(connectUri));
PipedOutputStream pipe = new PipedOutputStream();
PipedInputStream in = new PipedInputStream(pipe);
PrintWriter pw = new PrintWriter(pipe)) {
if (session.getWorkDir() != null) {
pw.println("cd " + session.getWorkDir());
}
pw.println(script);
pw.println("exit");
pw.flush();
shell(session, in, out);
} catch (IOException e) {
throw new RemoteInputStreamInstantiationException(e);
}
}
/**
* Private worker method for SSH shell interactions.
*
* @param session wrapped {@link ChannelShell} session object
* @param is {@link InputStream} object
* @param os {@link OutputStream} object
*/
private static void shell(SessionHolder<ChannelShell> session, InputStream is, OutputStream os) {
try {
ChannelShell channel = session.getChannel();
channel.setInputStream(is, true);
channel.setOutputStream(os, true);
LOG.info("Starting shell for " + session.getMaskedUri());
session.execute();
session.assertExitStatus("Check shell output for error details.");
} catch (JSchException e) {
throw new RemoteExecutionFailedException(session, e);
} catch (InterruptedException e) {
// set the 'interrupted' flag
Thread.currentThread().interrupt();
}
}
/**
* Open an SSH remote execution channel and execute the specified command.
*
* <pre><code> System.out.println(exec("ssh://user:pass@host/work/dir/path", "ls -t | head -n1"));</code></pre>
*
* @param connectUri SSH connection URI
* @param command command to be executed
* @return output from executed command
*/
public static String exec(String connectUri, String command) {
try (SessionHolder<ChannelExec> session = new SessionHolder<>(ChannelType.EXEC, URI.create(connectUri))) {
String changeDir = "";
String workDir = session.getWorkDir();
if (workDir != null) {
changeDir = "cd " + workDir + " && ";
}
return exec(session, changeDir + command);
}
}
/**
* Execute the specified command via the specified remote channel.
*
* @param session wrapped {@link ChannelExec} session object
* @param command command to be executed
* @return output from executed command
*/
public static String exec(SessionHolder<ChannelExec> session, String command) {
String output = null;
try (PipedOutputStream errPipe = new PipedOutputStream();
PipedInputStream errIs = new PipedInputStream(errPipe);
InputStream is = session.getChannel().getInputStream()) {
ChannelExec channel = session.getChannel();
channel.setInputStream(null);
channel.setErrStream(errPipe);
channel.setCommand(command);
LOG.info("Starting exec for " + session.getMaskedUri());
session.execute();
output = IOUtils.toString(is, Charset.defaultCharset());
session.assertExitStatus(IOUtils.toString(errIs, Charset.defaultCharset()));
} catch (IOException e) {
throw new RemoteInputStreamInstantiationException(e);
} catch (JSchException e) {
throw new RemoteExecutionFailedException(session, output, e);
} catch (InterruptedException e) {
// set the 'interrupted' flag
Thread.currentThread().interrupt();
}
return trim(output);
}
/**
* This is a wrapper class for objects that extend the {@link Channel} class.
* @author <a href='http://stackoverflow.com/users/448078/mykhaylo-adamovych'>Mykhaylo Adamovych</a>
*
* @param <C> channel type wrapped by this session holder
*/
public static class SessionHolder<C extends Channel> implements Closeable {
private static final int SESSION_CONNECT_TIMEOUT;
private static final int SSH_PORT_NUMBER;
private static final int TERMINAL_HEIGHT;
private static final int TERMINAL_WIDTH;
private static final int TERMINAL_H_RESOLUTION;
private static final int TERMINAL_V_RESOLUTION;
private static final int COMPLETION_CHECK_INTERVAL;
private static final int DISCONNECT_CHECK_ATTEMPTS;
private static final int DISCONNECT_CHECK_INTERVAL;
private ChannelType channelType;
private URI uri;
private Session session;
private C channel;
static {
RemoteConfig config = RemoteConfig.getConfig();
SESSION_CONNECT_TIMEOUT = config.getInt(RemoteSettings.SESSION_CONNECT_TIMEOUT.key());
SSH_PORT_NUMBER = config.getInt(RemoteSettings.SSH_PORT_NUMBER.key());
TERMINAL_HEIGHT = config.getInt(RemoteSettings.TERMINAL_HEIGHT.key());
TERMINAL_WIDTH = config.getInt(RemoteSettings.TERMINAL_WIDTH.key());
TERMINAL_H_RESOLUTION = config.getInt(RemoteSettings.TERMINAL_H_RESOLUTION.key());
TERMINAL_V_RESOLUTION = config.getInt(RemoteSettings.TERMINAL_V_RESOLUTION.key());
COMPLETION_CHECK_INTERVAL = config.getInt(RemoteSettings.COMPLETION_CHECK_INTERVAL.key());
DISCONNECT_CHECK_ATTEMPTS = config.getInt(RemoteSettings.DISCONNECT_CHECK_ATTEMPTS.key());
DISCONNECT_CHECK_INTERVAL = config.getInt(RemoteSettings.DISCONNECT_CHECK_INTERVAL.key());
}
/**
* Constructor #1 for wrapped SSH channel object
*
* @param channelType desired channel type
* @param uri SSH connection URI
*/
public SessionHolder(ChannelType channelType, URI uri) {
this(channelType, uri, ImmutableMap.of("StrictHostKeyChecking", "no"));
}
/**
* Constructor #2 for wrapped SSH channel object
*
* @param channelType desired channel type
* @param uri SSH connection URI
* @param props SSH session properties
*/
public SessionHolder(ChannelType channelType, URI uri, Map<String, String> props) {
this.channelType = channelType;
this.uri = uri;
this.session = newSession(props);
this.channel = newChannel(session);
}
/**
* Create a new SSH session with the specified properties.
*
* @param props session configuration properties
* @return new SSH session object
*/
private Session newSession(Map<String, String> props) {
try {
Properties config = new Properties();
config.putAll(props);
JSch jsch = new JSch();
String pass = getPass();
if (pass == null) {
RemoteConfig remoteConfig = RemoteConfig.getConfig();
Path keyPath = remoteConfig.getKeyPath();
if (keyPath == null) {
throw new RemoteCredentialsUnspecifiedException();
}
String keyPass = remoteConfig.getString(RemoteSettings.SSH_KEY_PASS.key());
if (keyPass != null) {
Path pubPath = keyPath.resolveSibling(keyPath.getFileName() + ".pub");
jsch.addIdentity(keyPath.toString(), pubPath.toString(), keyPass.getBytes());
} else {
jsch.addIdentity(keyPath.toString());
}
if ( ! remoteConfig.getBoolean(RemoteSettings.IGNORE_KNOWN_HOSTS.key())) {
Path knownHosts = keyPath.resolveSibling("known_hosts");
if (knownHosts.toFile().exists()) {
jsch.setKnownHosts(knownHosts.toString());
}
}
}
Session newSession = jsch.getSession(getUser(), getHost(), getPort());
if (pass != null) {
newSession.setPassword(pass);
}
newSession.setDaemonThread(true);
newSession.setConfig(config);
newSession.connect(SESSION_CONNECT_TIMEOUT);
return newSession;
} catch (JSchException e) {
throw new RemoteSessionInstantiationException("Cannot create session for " + getMaskedUri(), e);
}
}
/**
* Creates a new channel over the specified SSH session
*
* @param session a connection to an SSH server
* @return a new channel of the type specified for this {@link SessionHolder}, initialized, but not connected
*/
@SuppressWarnings("unchecked")
private C newChannel(Session session) {
try {
Channel newChannel = session.openChannel(channelType.name);
if (channelType == ChannelType.SHELL) {
((ChannelShell) newChannel).setPtyType("ANSI",
TERMINAL_WIDTH, TERMINAL_HEIGHT, TERMINAL_H_RESOLUTION, TERMINAL_V_RESOLUTION);
}
return (C) newChannel;
} catch (JSchException e) {
throw new RemoteChannelInstantiationException("Cannot create " + channelType + " channel for " + getMaskedUri(), e);
}
}
/**
* Verify that the remote task completed normally
*
* @param taskOutput output from the remote task
* @throws RemoteExecutionFailedException if exit status is non-zero
*/
public void assertExitStatus(String taskOutput) {
if (getExitStatus() != 0) {
throw new RemoteExecutionFailedException(this, taskOutput);
}
}
/**
* Opens the channel to the remote session, starts the configured task, and waits for end-of-file to be received.
*
* @throws JSchException a timeout or other connection issue was detected
* @throws InterruptedException any thread has interrupted the current thread
*/
public void execute() throws JSchException, InterruptedException {
channel.connect();
channel.start();
while (!channel.isEOF()) {
sleep(COMPLETION_CHECK_INTERVAL);
}
}
/**
* Get the SSH session for this {@link SessionHolder}
*
* @return SSH session object
*/
public Session getSession() {
return session;
}
/**
* Get the channel to the remote session created for this {@link SessionHolder}
*
* @return SSH session channel object
*/
public C getChannel() {
return channel;
}
/**
* Get a channel stream object for this session.
*
* @return stream object for performing channel I/O
*/
public ChannelStreams<C> getChannelStream() {
return new ChannelStreams<>(channel);
}
/**
* Disconnect the channel and session created for this {@link SessionHolder}
*/
@Override
public void close() {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
/**
* Get the URI for this {@link SessionHolder} with password redacted
*
* @return SSH connection URI without password
*/
public String getMaskedUri() {
if (getPass() != null) {
return uri.toString().replaceFirst(":[^:]*?@", "@");
}
return uri.toString();
}
/**
* Get the host of the URI for this {@link SessionHolder}
*
* @return host component of the SSH connection URI
*/
public String getHost() {
return uri.getHost();
}
/**
* Get the port of the URI for this {@link SessionHolder}
*
* @return port component of the SSH connection URI
*/
public int getPort() {
if (uri.getPort() >= 0) {
return uri.getPort();
}
return SSH_PORT_NUMBER;
}
/**
* Get the user specified in the URI for this {@link SessionHolder}
*
* @return user specified in the SSH connection URI
*/
public String getUser() {
String userInfo = uri.getUserInfo();
if (userInfo != null) {
return userInfo.split(":")[0];
}
return null;
}
/**
* Get the password specified in the URI for this {@link SessionHolder}
*
* @return password specified in the SSH connection URI
*/
private String getPass() {
String userInfo = uri.getUserInfo();
if (userInfo != null) {
String[] userBits = userInfo.split(":");
if (userBits.length > 1) {
return userBits[1];
}
}
return null;
}
/**
* Get the target directory specified in the URI for this {@link SessionHolder}
*
* @return path component of the SSH connection URI
*/
public String getWorkDir() {
return uri.getPath();
}
/**
* Get the channel exit status
*
* @return channel exit status
*/
public int getExitStatus() {
return channel.getExitStatus();
}
/**
* Disconnect channel and session.
*
* @param waitClose 'true' to delay disconnect until the channel is closed; 'false' to disconnect immediately
*/
public void disconnect(boolean waitClose) {
if (waitClose) {
waitChannel();
}
close();
}
/**
* Wait for channel to close.<br>
* <b>NOTE</b>: This method polls the channel 'closed' state a maximum of {@link #DISCONNECT_CHECK_ATTEMPTS} times,
* delaying {@link #DISCONNECT_CHECK_INTERVAL} milliseconds between each check.
*/
public void waitChannel() {
try {
// Wait until channel is finished (otherwise redirections will not work)
for (int i = DISCONNECT_CHECK_ATTEMPTS; !channel.isClosed() && i > 0; i--) {
Thread.sleep(DISCONNECT_CHECK_INTERVAL);
}
} catch (InterruptedException e) {
// set the 'interrupted' flag
Thread.currentThread().interrupt();
}
}
}
/**
* This class encapsulates input/output operation for the channel attached to this session.
*
* @param <C> channel type wrapped by this session holder
*/
public static class ChannelStreams<C extends Channel> {
private static final int CHECK_INTERVAL;
private static final int BUFFER_SIZE;
static {
RemoteConfig config = RemoteConfig.getConfig();
CHECK_INTERVAL = config.getInt(RemoteSettings.CHANNEL_CHECK_INTERVAL.key());
BUFFER_SIZE = config.getInt(RemoteSettings.CHANNEL_BUFFER_SIZE.key());
}
private C channel;
private InputStream in;
private OutputStream out;
private byte[] tmp = new byte[BUFFER_SIZE];
/**
* Constructor for channel I/O object
*
* @param channel the channel to which I/O operation will be directed
*/
public ChannelStreams(C channel) {
this.channel = channel;
try {
in = channel.getInputStream();
} catch (IOException e) {
throw new IllegalStateException("Failed to acquire channel input stream", e);
}
try {
out = channel.getOutputStream();
} catch (IOException e) {
throw new IllegalStateException("Failed to acquire channel output stream", e);
}
}
/**
* Read the input from the channel.
*
* @param waitClose 'true' to poll for input until the channel closes; 'false' to return available input
* @return channel input (may be empty)
* @throws InterruptedException if this thread was interrupted
* @throws IOException if an I/O error occurs
*/
public String readChannel(boolean waitClose) throws InterruptedException, IOException {
StringBuilder stdout = new StringBuilder();
while (appendAvailable(stdout) && waitClose && !channel.isClosed()) { //NOSONAR
Thread.sleep(CHECK_INTERVAL);
}
return stdout.toString();
}
/**
* Append available input to the specified string builder.
*
* @param stdout {@link StringBuilder} object
* @return this method always returns 'true'
* @throws IOException if an I/O error occurs
*/
private boolean appendAvailable(StringBuilder stdout) throws IOException {
String recv = readAvailable();
if (recv != null) {
stdout.append(recv);
}
return true;
}
/**
* Read available input from the specified stream.
*
* @return available input (may be 'null')
* @throws IOException if an I/O error occurs
*/
private String readAvailable() throws IOException {
if (in.available() > 0) {
int i = in.read(tmp, 0, BUFFER_SIZE);
if (i != -1) {
return new String(tmp, 0, i);
}
}
return null;
}
/**
* Wait for input to be available.
*
* @throws InterruptedException if this thread was interrupted
* @throws IOException if an I/O error occurs
*/
public void waitForInput() throws InterruptedException, IOException {
while (in.available() == 0) {
Thread.sleep(CHECK_INTERVAL);
}
}
/**
* Wait for the specified prompt to be received from the remote host.
*
* @param prompt prompt to wait for
* @param maxWait maximum interval in milliseconds to wait for the specified prompt; -1 to wait indefinitely
* @return all of the input that was received while waiting for the prompt
* @throws InterruptedException if this thread was interrupted
* @throws IOException if an I/O error occurs
*/
public String waitForPrompt(String prompt, long maxWait) throws InterruptedException, IOException {
return waitForPrompt(prompt, maxWait, null);
}
/**
* Wait for the specified prompt to be received from the remote host.
*
* @param prompt prompt to wait for
* @param maxWait maximum interval in milliseconds to wait for the specified prompt; -1 to wait indefinitely
* @param logger SLF4J {@link Logger} object for output (may be 'null')
* @return all of the input that was received while waiting for the prompt
* @throws InterruptedException if this thread was interrupted
* @throws IOException if an I/O error occurs
*/
public String waitForPrompt(String prompt, long maxWait, Logger logger) throws InterruptedException, IOException {
StringBuilder input = new StringBuilder();
long maxTime = System.currentTimeMillis() + maxWait;
while (appendAndCheckFor(prompt, input, logger) && ((maxWait == -1) || (System.currentTimeMillis() <= maxTime))) {
Thread.sleep(CHECK_INTERVAL);
}
return input.toString();
}
/**
* Append available channel input to the supplied string builder and check for the specified prompt.
*
* @param prompt prompt to check for
* @param input {@link StringBuilder} object
* @param logger SLF4J {@link Logger} object for output (may be 'null')
* @return 'false' is prompt is found or channel is closed; otherwise 'true'
* @throws InterruptedException if this thread was interrupted
* @throws IOException if an I/O error occurs
*/
private boolean appendAndCheckFor(String prompt, StringBuilder input, Logger logger) throws InterruptedException, IOException {
String recv = readChannel(false);
if ( ! ((recv == null) || recv.isEmpty())) {
input.append(recv);
if (logger != null) {
logger.debug(recv);
}
if (input.toString().contains(prompt)) {
return false;
}
}
return !channel.isClosed();
}
/**
* Write the specified string to the remote host, followed by a carriage return
*
* @param line the line of text to be written
* @throws IOException if an I/O error occurs
*/
public void writeln(String line) throws IOException {
out.write((line + "\n").getBytes());
out.flush();
}
}
}