reduce reliance on implicit channel close; add close() method to SFTP classes; update examples

This commit is contained in:
Shikhar Bhushan
2010-07-28 23:53:26 +01:00
parent fb97ccb67c
commit 2882129211
11 changed files with 70 additions and 43 deletions

View File

@@ -16,6 +16,7 @@
package examples;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import java.io.IOException;
@@ -25,18 +26,20 @@ public class Exec {
public static void main(String... args)
throws IOException {
SSHClient ssh = new SSHClient();
final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost");
try {
ssh.authPublickey(System.getProperty("user.name"));
Command cmd = ssh.startSession().exec("ping -c 1 google.com");
System.out.print(cmd.getOutputAsString());
System.out.println("\n** exit status: " + cmd.getExitStatus());
final Session session = ssh.startSession();
try {
final Command cmd = session.exec("ping -c 1 google.com");
System.out.print(cmd.getOutputAsString());
System.out.println("\n** exit status: " + cmd.getExitStatus());
} finally {
session.close();
}
} finally {
ssh.disconnect();

View File

@@ -37,35 +37,35 @@ class RudimentaryPTY {
ssh.addHostKeyVerifier(new ConsoleKnownHostsVerifier(khFile, System.console()));
ssh.connect("localhost");
Shell shell = null;
try {
ssh.authPublickey(System.getProperty("user.name"));
final Session session = ssh.startSession();
session.allocateDefaultPTY();
try {
shell = session.startShell();
session.allocateDefaultPTY();
new StreamCopier("stdout", shell.getInputStream(), System.out)
.bufSize(shell.getLocalMaxPacketSize())
.start();
final Shell shell = session.startShell();
new StreamCopier("stderr", shell.getErrorStream(), System.err)
.bufSize(shell.getLocalMaxPacketSize())
.start();
new StreamCopier("stdout", shell.getInputStream(), System.out)
.bufSize(shell.getLocalMaxPacketSize())
.start();
// Now make System.in act as stdin. To exit, hit Ctrl+D (since that results in an EOF on System.in)
// This is kinda messy because java only allows console input after you hit return
// But this is just an example... a GUI app could implement a proper PTY
StreamCopier.copy(System.in, shell.getOutputStream(), shell.getRemoteMaxPacketSize(), true);
new StreamCopier("stderr", shell.getErrorStream(), System.err)
.bufSize(shell.getLocalMaxPacketSize())
.start();
// Now make System.in act as stdin. To exit, hit Ctrl+D (since that results in an EOF on System.in)
// This is kinda messy because java only allows console input after you hit return
// But this is just an example... a GUI app could implement a proper PTY
StreamCopier.copy(System.in, shell.getOutputStream(), shell.getRemoteMaxPacketSize(), true);
} finally {
session.close();
}
} finally {
if (shell != null)
shell.close();
ssh.disconnect();
}
}

View File

@@ -16,6 +16,7 @@
package examples;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.SFTPClient;
import java.io.IOException;
@@ -24,14 +25,19 @@ public class SFTPDownload {
public static void main(String[] args)
throws IOException {
SSHClient ssh = new SSHClient();
final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost");
try {
ssh.authPublickey(System.getProperty("user.name"));
final String src = "test_file";
final String target = "/tmp/";
ssh.newSFTPClient().get(src, target);
final SFTPClient sftp = ssh.newSFTPClient();
try {
sftp.get(src, target);
} finally {
sftp.close();
}
} finally {
ssh.disconnect();
}

View File

@@ -16,6 +16,7 @@
package examples;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.SFTPClient;
import java.io.File;
import java.io.IOException;
@@ -25,14 +26,19 @@ public class SFTPUpload {
public static void main(String[] args)
throws IOException {
SSHClient ssh = new SSHClient();
final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost");
try {
ssh.authPublickey(System.getProperty("user.name"));
final String src = System.getProperty("user.home") + File.separator + "test_file";
final String target = "/tmp/";
ssh.newSFTPClient().put(src, target);
final SFTPClient sftp = ssh.newSFTPClient();
try {
sftp.put(src, target);
} finally {
sftp.close();
}
} finally {
ssh.disconnect();
}

View File

@@ -29,7 +29,7 @@ public class X11 {
public static void main(String... args)
throws IOException, InterruptedException {
SSHClient ssh = new SSHClient();
final SSHClient ssh = new SSHClient();
// Compression makes X11 more feasible over slower connections
// ssh.useCompression();
@@ -55,7 +55,7 @@ public class X11 {
*/
sess.reqX11Forwarding("MIT-MAGIC-COOKIE-1", "b0956167c9ad8f34c8a2788878307dc9", 0);
Command cmd = sess.exec("/usr/X11/bin/xcalc");
final Command cmd = sess.exec("/usr/X11/bin/xcalc");
new StreamCopier("stdout", cmd.getInputStream(), System.out).start();
new StreamCopier("stderr", cmd.getErrorStream(), System.err).start();