mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-08 00:00:54 +03:00
give examples some love
This commit is contained in:
@@ -18,6 +18,8 @@ package examples;
|
|||||||
import net.schmizz.sshj.SSHClient;
|
import net.schmizz.sshj.SSHClient;
|
||||||
import net.schmizz.sshj.connection.channel.direct.Session.Command;
|
import net.schmizz.sshj.connection.channel.direct.Session.Command;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/** This examples demonstrates how a remote command can be executed. */
|
/** This examples demonstrates how a remote command can be executed. */
|
||||||
public class Exec {
|
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")));
|
// 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();
|
SSHClient ssh = new SSHClient();
|
||||||
ssh.loadKnownHosts();
|
ssh.loadKnownHosts();
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package examples;
|
|||||||
|
|
||||||
import net.schmizz.sshj.SSHClient;
|
import net.schmizz.sshj.SSHClient;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
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")));
|
// 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();
|
SSHClient ssh = new SSHClient();
|
||||||
|
|
||||||
ssh.loadKnownHosts();
|
ssh.loadKnownHosts();
|
||||||
@@ -45,7 +47,8 @@ public class LocalPF {
|
|||||||
* google.com:80
|
* google.com:80
|
||||||
*/
|
*/
|
||||||
|
|
||||||
ssh.newLocalPortForwarder(new InetSocketAddress("localhost", 8080), "google.com", 80).listen();
|
ssh.newLocalPortForwarder(new InetSocketAddress("localhost", 8080), "google.com", 80)
|
||||||
|
.listen();
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
ssh.disconnect();
|
ssh.disconnect();
|
||||||
|
|||||||
@@ -18,10 +18,8 @@ package examples;
|
|||||||
import net.schmizz.sshj.SSHClient;
|
import net.schmizz.sshj.SSHClient;
|
||||||
import net.schmizz.sshj.connection.channel.forwarded.RemotePortForwarder.Forward;
|
import net.schmizz.sshj.connection.channel.forwarded.RemotePortForwarder.Forward;
|
||||||
import net.schmizz.sshj.connection.channel.forwarded.SocketForwardingConnectListener;
|
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;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -30,11 +28,12 @@ import java.net.InetSocketAddress;
|
|||||||
*/
|
*/
|
||||||
public class RemotePF {
|
public class RemotePF {
|
||||||
|
|
||||||
static {
|
// static {
|
||||||
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
|
// 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();
|
SSHClient client = new SSHClient();
|
||||||
client.loadKnownHosts();
|
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
|
* 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
|
* forward all such channels to google.com:80
|
||||||
*/
|
*/
|
||||||
client.getRemotePortForwarder().bind(new Forward(8080), //
|
client.getRemotePortForwarder().bind(
|
||||||
new SocketForwardingConnectListener(new InetSocketAddress("google.com", 80)));
|
// 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
|
// Something to hang on to so that the forwarding stays
|
||||||
client.getTransport().join();
|
client.getTransport().join();
|
||||||
|
|
||||||
|
|||||||
@@ -32,11 +32,13 @@ class RudimentaryPTY {
|
|||||||
// BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
|
// 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();
|
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");
|
ssh.connect("localhost");
|
||||||
|
|
||||||
|
|||||||
@@ -16,25 +16,26 @@
|
|||||||
package examples;
|
package examples;
|
||||||
|
|
||||||
import net.schmizz.sshj.SSHClient;
|
import net.schmizz.sshj.SSHClient;
|
||||||
import org.apache.log4j.BasicConfigurator;
|
|
||||||
import org.apache.log4j.ConsoleAppender;
|
import java.io.IOException;
|
||||||
import org.apache.log4j.PatternLayout;
|
|
||||||
|
|
||||||
/** This example demonstrates downloading of a file over SCP from the SSH server. */
|
/** This example demonstrates downloading of a file over SCP from the SSH server. */
|
||||||
public class SCPDownload {
|
public class SCPDownload {
|
||||||
|
|
||||||
static {
|
// static {
|
||||||
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
|
// 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();
|
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.loadKnownHosts();
|
||||||
ssh.connect("localhost");
|
ssh.connect("localhost");
|
||||||
try {
|
try {
|
||||||
ssh.authPublickey(System.getProperty("user.name"));
|
ssh.authPublickey(System.getProperty("user.name"));
|
||||||
ssh.newSCPFileTransfer().download("well", "/tmp/");
|
ssh.newSCPFileTransfer()
|
||||||
|
.download("well", "/tmp/");
|
||||||
} finally {
|
} finally {
|
||||||
ssh.disconnect();
|
ssh.disconnect();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,29 +16,29 @@
|
|||||||
package examples;
|
package examples;
|
||||||
|
|
||||||
import net.schmizz.sshj.SSHClient;
|
import net.schmizz.sshj.SSHClient;
|
||||||
import org.apache.log4j.BasicConfigurator;
|
|
||||||
import org.apache.log4j.ConsoleAppender;
|
import java.io.IOException;
|
||||||
import org.apache.log4j.PatternLayout;
|
|
||||||
|
|
||||||
/** This example demonstrates uploading of a file over SCP to the SSH server. */
|
/** This example demonstrates uploading of a file over SCP to the SSH server. */
|
||||||
public class SCPUpload {
|
public class SCPUpload {
|
||||||
|
|
||||||
static {
|
// static {
|
||||||
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
|
// 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();
|
SSHClient ssh = new SSHClient();
|
||||||
ssh.loadKnownHosts();
|
ssh.loadKnownHosts();
|
||||||
ssh.connect("localhost");
|
ssh.connect("localhost");
|
||||||
try {
|
try {
|
||||||
ssh.authPublickey(System.getProperty("user.name"));
|
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.useCompression();
|
||||||
|
|
||||||
ssh.newSCPFileTransfer().upload("/Users/shikhar/well", "/tmp/");
|
ssh.newSCPFileTransfer()
|
||||||
|
.upload("/Users/shikhar/well", "/tmp/");
|
||||||
} finally {
|
} finally {
|
||||||
ssh.disconnect();
|
ssh.disconnect();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,18 +16,18 @@
|
|||||||
package examples;
|
package examples;
|
||||||
|
|
||||||
import net.schmizz.sshj.SSHClient;
|
import net.schmizz.sshj.SSHClient;
|
||||||
import org.apache.log4j.BasicConfigurator;
|
|
||||||
import org.apache.log4j.ConsoleAppender;
|
import java.io.IOException;
|
||||||
import org.apache.log4j.PatternLayout;
|
|
||||||
|
|
||||||
/** This example demonstrates downloading of a file over SFTP from the SSH server. */
|
/** This example demonstrates downloading of a file over SFTP from the SSH server. */
|
||||||
public class SFTPDownload {
|
public class SFTPDownload {
|
||||||
|
|
||||||
static {
|
// static {
|
||||||
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d [%-15.15t] %-5p %-30.30c{1} - %m%n")));
|
// 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();
|
SSHClient ssh = new SSHClient();
|
||||||
ssh.loadKnownHosts();
|
ssh.loadKnownHosts();
|
||||||
ssh.connect("localhost");
|
ssh.connect("localhost");
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ package examples;
|
|||||||
|
|
||||||
import net.schmizz.sshj.SSHClient;
|
import net.schmizz.sshj.SSHClient;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/** This example demonstrates uploading of a file over SFTP to the SSH server. */
|
/** This example demonstrates uploading of a file over SFTP to the SSH server. */
|
||||||
public class SFTPUpload {
|
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")));
|
// 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();
|
SSHClient ssh = new SSHClient();
|
||||||
ssh.loadKnownHosts();
|
ssh.loadKnownHosts();
|
||||||
ssh.connect("localhost");
|
ssh.connect("localhost");
|
||||||
|
|||||||
@@ -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.direct.Session.Command;
|
||||||
import net.schmizz.sshj.connection.channel.forwarded.SocketForwardingConnectListener;
|
import net.schmizz.sshj.connection.channel.forwarded.SocketForwardingConnectListener;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
/** This example demonstrates how forwarding X11 connections from a remote host can be accomplished. */
|
/** 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")));
|
// 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();
|
SSHClient ssh = new SSHClient();
|
||||||
|
|
||||||
// Compression makes X11 more feasible over slower connections
|
// Compression makes X11 more feasible over slower connections
|
||||||
|
|||||||
Reference in New Issue
Block a user