Java Examples for net.schmizz.sshj.common.StreamCopier

The following java examples will help you to understand the usage of net.schmizz.sshj.common.StreamCopier. These source code samples are taken from different open source projects.

Example 1
Project: sshj-master  File: SFTPFileTransfer.java View source code
private LocalDestFile downloadFile(final StreamCopier.Listener listener, final RemoteResourceInfo remote, final LocalDestFile local) throws IOException {
    final LocalDestFile adjusted = local.getTargetFile(remote.getName());
    final RemoteFile rf = engine.open(remote.getPath());
    try {
        final RemoteFile.ReadAheadRemoteFileInputStream rfis = rf.new ReadAheadRemoteFileInputStream(16);
        final OutputStream os = adjusted.getOutputStream();
        try {
            new StreamCopier(rfis, os, engine.getLoggerFactory()).bufSize(engine.getSubsystem().getLocalMaxPacketSize()).keepFlushing(false).listener(listener).copy();
        } finally {
            rfis.close();
            os.close();
        }
    } finally {
        rf.close();
    }
    return adjusted;
}
Example 2
Project: infovore-master  File: Ssh.java View source code
private void logIntoIp(String ipAddress) throws IOException, InterruptedException {
    final SSHClient ssh = new SSHClient();
    OpenSSHKeyFile k = new OpenSSHKeyFile();
    k.init(new File(dotHaruhi, keyPairName + ".pem"));
    ssh.addHostKeyVerifier(new PromiscuousVerifier());
    ssh.connect(ipAddress);
    try {
        ssh.authPublickey(clusterUsername, k);
        Session that = ssh.startSession();
        try {
            that.allocateDefaultPTY();
            Session.Shell shell = that.startShell();
            new StreamCopier(shell.getInputStream(), System.out).bufSize(shell.getLocalMaxPacketSize()).spawn("stdout");
            new StreamCopier(shell.getErrorStream(), System.err).bufSize(shell.getLocalMaxPacketSize()).spawn("stderr");
            new StreamCopier(System.in, shell.getOutputStream()).bufSize(shell.getRemoteMaxPacketSize()).copy();
        } finally {
            that.close();
        }
    } finally {
        ssh.close();
    }
}
Example 3
Project: Liferay-plugins-master  File: AMIBuilder.java View source code
protected void executeSessionCommand(SSHClient sshClient, String command) throws Exception {
    System.out.println("Executing session command: " + command);
    Session session = sshClient.startSession();
    session.allocateDefaultPTY();
    try {
        Session.Command sessionCommand = session.exec(command);
        if (_output) {
            new StreamCopier(session.getInputStream(), System.out).copy();
        }
        sessionCommand.join();
    } finally {
        session.close();
    }
}
Example 4
Project: sandboxes-master  File: RudimentaryPTY.java View source code
public static void main(String... args) throws IOException {
    final SSHClient ssh = new SSHClient();
    final File knownHostsFile = new File(OpenSSHKnownHosts.detectSSHDir(), "known_hosts");
    ssh.addHostKeyVerifier(new ConsoleKnownHostsVerifier(knownHostsFile, System.console()));
    ssh.connect("localhost");
    try {
        ssh.authPublickey(System.getProperty("user.name"));
        final Session session = ssh.startSession();
        try {
            session.allocateDefaultPTY();
            final Shell shell = session.startShell();
            new StreamCopier(shell.getInputStream(), System.out).bufSize(shell.getLocalMaxPacketSize()).spawn("stdout");
            new StreamCopier(shell.getErrorStream(), System.err).bufSize(shell.getLocalMaxPacketSize()).spawn("stderr");
            // 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
            new StreamCopier(System.in, shell.getOutputStream()).bufSize(shell.getRemoteMaxPacketSize()).copy();
        } finally {
            session.close();
        }
    } finally {
        ssh.disconnect();
    }
}
Example 5
Project: karamel-master  File: RudimentaryPTY.java View source code
public static void main(String... args) throws IOException {
    String privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" + "MIIEogIBAAKCAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzI\n" + "w+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoP\n" + "kcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2\n" + "hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NO\n" + "Td0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcW\n" + "yLbIbEgE98OHlnVYCzRdK8jlqm8t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\n" + "llYOUI+SpLJ6/vURRbHSnnn8a/XG+nzedGH5JGqEJNQsz+xT2axM0/W/CRknmGaJ\n" + "kda/AoGANWrLCz708y7VYgAtW2Uf1DPOIYMdvo6fxIB5i9ZfISgcJ/bbCUkFrhoH\n" + "+vq/5CIWxCPp0f85R4qxxQ5ihxJ0YDQT9Jpx4TMss4PSavPaBH3RXow5Ohe+bYoQ\n" + "NE5OgEXk2wVfZczCZpigBKbKZHNYcelXtTt/nP3rsCuGcM4h53s=\n" + "-----END RSA PRIVATE KEY-----";
    String publicKey = "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key";
    SSHClient client = new SSHClient();
    client.addHostKeyVerifier(new PromiscuousVerifier());
    KeyProvider keys = client.loadKeys(privateKey, publicKey, null);
    client.connect("192.168.33.10", 22);
    client.authPublickey("vagrant", keys);
    try {
        final Session session = client.startSession();
        try {
            session.allocateDefaultPTY();
            final Shell shell = session.startShell();
            new StreamCopier(shell.getInputStream(), System.out).bufSize(shell.getLocalMaxPacketSize()).spawn("stdout");
            new StreamCopier(shell.getErrorStream(), System.err).bufSize(shell.getLocalMaxPacketSize()).spawn("stderr");
            // 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
            new StreamCopier(System.in, shell.getOutputStream()).bufSize(shell.getRemoteMaxPacketSize()).copy();
        } finally {
            session.close();
        }
    } finally {
        client.disconnect();
    }
}