-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathssh.java
66 lines (52 loc) · 2.21 KB
/
ssh.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
import com.fizzed.blaze.Config;
import com.fizzed.blaze.Contexts;
import static com.fizzed.blaze.Contexts.prompt;
import com.fizzed.blaze.util.MutableUri;
import com.fizzed.blaze.ssh.SshSession;
import org.slf4j.Logger;
import static com.fizzed.blaze.SecureShells.sshConnect;
import static com.fizzed.blaze.SecureShells.sshExec;
import com.fizzed.blaze.util.CaptureOutput;
import com.fizzed.blaze.util.Streamables;
public class ssh {
final private Logger log = Contexts.logger();
final private Config config = Contexts.config();
public void main() throws Exception {
// simple for skipping this example in try_all.java
boolean in_try_all_example = config.value("examples.try_all", Boolean.class).getOr(false);
if (in_try_all_example) {
return;
}
// get or prompt for uri to ssh to
MutableUri uri = config.value("ssh.uri", MutableUri.class).getOrNull();
if (uri == null) {
String s = prompt().allowBlank(false).prompt("Enter ssh uri (e.g. ssh://user@host)> ");
uri = MutableUri.of(s);
}
try (SshSession session = sshConnect(uri).run()) {
// remote working directory
CaptureOutput capture = Streamables.captureOutput();
sshExec(session)
.command("pwd")
.pipeOutput(capture)
.run();
log.info("Remote working dir is {}", capture.toString().trim());
// who are we logged in as? (optimized capture example since its
// a common pattern)
String whoami
= sshExec(session)
.command("whoami")
.runCaptureOutput()
.toString();
log.info("Logged in as {}", whoami.trim());
log.info("Listing current directory...");
// list directory to stdout
Integer exitValue
= sshExec(session)
.command("ls")
.arg("-la")
.run();
log.info("Last exec exited with {}", exitValue);
}
}
}