give examples some love

This commit is contained in:
Shikhar Bhushan
2010-02-28 20:13:36 +01:00
parent b09729c623
commit 7106eed7a5
9 changed files with 62 additions and 43 deletions

View File

@@ -18,6 +18,8 @@ package examples;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import java.io.IOException;
/** This examples demonstrates how a remote command can be executed. */
public class Exec {
@@ -25,7 +27,8 @@ public class Exec {
// BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
// }
public static void main(String... args) throws Exception {
public static void main(String... args)
throws IOException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();

View File

@@ -17,6 +17,7 @@ package examples;
import net.schmizz.sshj.SSHClient;
import java.io.IOException;
import java.net.InetSocketAddress;
/**
@@ -30,7 +31,8 @@ public class LocalPF {
// BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
// }
public static void main(String... args) throws Exception {
public static void main(String... args)
throws IOException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
@@ -45,7 +47,8 @@ public class LocalPF {
* google.com:80
*/
ssh.newLocalPortForwarder(new InetSocketAddress("localhost", 8080), "google.com", 80).listen();
ssh.newLocalPortForwarder(new InetSocketAddress("localhost", 8080), "google.com", 80)
.listen();
} finally {
ssh.disconnect();

View File

@@ -18,10 +18,8 @@ package examples;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.forwarded.RemotePortForwarder.Forward;
import net.schmizz.sshj.connection.channel.forwarded.SocketForwardingConnectListener;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.PatternLayout;
import java.io.IOException;
import java.net.InetSocketAddress;
/**
@@ -30,11 +28,12 @@ import java.net.InetSocketAddress;
*/
public class RemotePF {
static {
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
}
// static {
// BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
// }
public static void main(String... args) throws Exception {
public static void main(String... args)
throws IOException {
SSHClient client = new SSHClient();
client.loadKnownHosts();
@@ -47,10 +46,16 @@ public class RemotePF {
* We make _server_ listen on port 8080, which forwards all connections to us as a channel, and we further
* forward all such channels to google.com:80
*/
client.getRemotePortForwarder().bind(new Forward(8080), //
new SocketForwardingConnectListener(new InetSocketAddress("google.com", 80)));
client.getRemotePortForwarder().bind(
// where the server should listen
new Forward(8080),
// what we do with incoming connections that are forwarded to us
new SocketForwardingConnectListener(new InetSocketAddress("google.com", 80)
));
client.getTransport()
.setHeartbeatInterval(30);
client.getTransport().setHeartbeatInterval(30);
// Something to hang on to so that the forwarding stays
client.getTransport().join();

View File

@@ -32,11 +32,13 @@ class RudimentaryPTY {
// BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
// }
public static void main(String... args) throws IOException {
public static void main(String... args)
throws IOException {
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(new ConsoleHostKeyVerifier(new File(OpenSSHKnownHosts.detectSSHDir(), "known_hosts"), System.console()));
final File khFile = new File(OpenSSHKnownHosts.detectSSHDir(), "known_hosts");
ssh.addHostKeyVerifier(new ConsoleHostKeyVerifier(khFile, System.console()));
ssh.connect("localhost");

View File

@@ -16,25 +16,26 @@
package examples;
import net.schmizz.sshj.SSHClient;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.PatternLayout;
import java.io.IOException;
/** This example demonstrates downloading of a file over SCP from the SSH server. */
public class SCPDownload {
static {
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
}
// static {
// BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
// }
public static void main(String[] args) throws Exception {
public static void main(String[] args)
throws IOException {
SSHClient ssh = new SSHClient();
// ssh.useCompression(); // => significant speedup for large file transfers on fast links
// ssh.useCompression(); // Can lead to significant speedup
ssh.loadKnownHosts();
ssh.connect("localhost");
try {
ssh.authPublickey(System.getProperty("user.name"));
ssh.newSCPFileTransfer().download("well", "/tmp/");
ssh.newSCPFileTransfer()
.download("well", "/tmp/");
} finally {
ssh.disconnect();
}

View File

@@ -16,29 +16,29 @@
package examples;
import net.schmizz.sshj.SSHClient;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.PatternLayout;
import java.io.IOException;
/** This example demonstrates uploading of a file over SCP to the SSH server. */
public class SCPUpload {
static {
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
}
// static {
// BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
// }
public static void main(String[] args) throws Exception {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost");
try {
ssh.authPublickey(System.getProperty("user.name"));
// Compression = significant speedup for large file transfers on fast links
// present here to demo algorithm renegotiation - could have just put this before connect()
// Present here to demo algorithm renegotiation - could have just put this before connect()
ssh.useCompression();
ssh.newSCPFileTransfer().upload("/Users/shikhar/well", "/tmp/");
ssh.newSCPFileTransfer()
.upload("/Users/shikhar/well", "/tmp/");
} finally {
ssh.disconnect();
}

View File

@@ -16,18 +16,18 @@
package examples;
import net.schmizz.sshj.SSHClient;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.PatternLayout;
import java.io.IOException;
/** This example demonstrates downloading of a file over SFTP from the SSH server. */
public class SFTPDownload {
static {
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
}
// static {
// BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
// }
public static void main(String[] args) throws Exception {
public static void main(String[] args)
throws IOException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost");

View File

@@ -17,6 +17,8 @@ package examples;
import net.schmizz.sshj.SSHClient;
import java.io.IOException;
/** This example demonstrates uploading of a file over SFTP to the SSH server. */
public class SFTPUpload {
@@ -25,7 +27,8 @@ public class SFTPUpload {
// BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
// }
public static void main(String[] args) throws Exception {
public static void main(String[] args)
throws IOException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost");

View File

@@ -21,6 +21,7 @@ import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import net.schmizz.sshj.connection.channel.forwarded.SocketForwardingConnectListener;
import java.io.IOException;
import java.net.InetSocketAddress;
/** This example demonstrates how forwarding X11 connections from a remote host can be accomplished. */
@@ -30,7 +31,8 @@ public class X11 {
// BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
// }
public static void main(String... args) throws Exception {
public static void main(String... args)
throws IOException, InterruptedException {
SSHClient ssh = new SSHClient();
// Compression makes X11 more feasible over slower connections