Documentation
Introduction
Open Source Release Policy
Export Compliance
Using BouncyCastle
Installing the API
Enabling Logging
Per-Connection Logging
Third Party Dependencies
Generating SSH Keys
Using BouncyCastle
Using SSH Public Keys in Java
Supporting ED25519/Curve25519
Supporting Compression
Integrating Licensing into Maven Builds
Open Source Release Policy
Export Compliance
Using BouncyCastle
Installing the API
Enabling Logging
Per-Connection Logging
Third Party Dependencies
Generating SSH Keys
Using BouncyCastle
Using SSH Public Keys in Java
Supporting ED25519/Curve25519
Supporting Compression
Integrating Licensing into Maven Builds
Client API
Quick Start
Creating an SSH Client
Public Key Authentication
Keyboard Interactive Authentication
Public Key Authentication with sshagent
Executing Single Commands
Executing Commands within a Shell
Transferring Files
Connecting through Proxies
Display the Authentication Banner
Using the SFTP Client
Port Forwarding
Creating an SSH Client
Public Key Authentication
Keyboard Interactive Authentication
Public Key Authentication with sshagent
Executing Single Commands
Executing Commands within a Shell
Transferring Files
Connecting through Proxies
Display the Authentication Banner
Using the SFTP Client
Port Forwarding
Working Examples
Server API
Creating an SSH Server
Configuring Listening Interfaces
Configuring Host Keys
Password Authentication
Public Key Authentication
Challenge-Response Authentication
Configuring Port Forwarding
Configuring SFTP
Supporting SCP
Implementing your own File System
Creating an Interactive Terminal
Proxy Protocol Support
Configuring Listening Interfaces
Configuring Host Keys
Password Authentication
Public Key Authentication
Challenge-Response Authentication
Configuring Port Forwarding
Configuring SFTP
Supporting SCP
Implementing your own File System
Creating an Interactive Terminal
Proxy Protocol Support
Copy a Directory using SCP and Java SSH
Introduction
SCP is very useful for the bulk transfer of directories across the network. In this example, we create a temporary local file structure, copy it over the remote server, and then copy it back.
SCP is not as powerful as SFTP and provides only file transfer functions. SFTP provides full file system access with the ability to get the file information, transfer files, create folders, delete files, and much more.
Source Code
package examples;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import com.sshtools.client.SshClient;
import com.sshtools.client.scp.ScpClient;
import com.sshtools.common.files.direct.DirectFileFactory;
import com.sshtools.common.permissions.PermissionDeniedException;
import com.sshtools.common.ssh.ChannelOpenException;
import com.sshtools.common.ssh.SshException;
import com.sshtools.common.util.IOUtils;
import com.sshtools.common.util.Utils;
public class SCPDirectory {
public static void main(String[] args) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
String hostname = Utils.prompt(reader, "Hostname", "localhost");
int port = 22;
if (Utils.hasPort(hostname)) {
port = Utils.getPort(hostname);
}
String username = Utils.prompt(reader, "Username", System.getProperty("user.name"));
String password = Utils.prompt(reader, "Password");
/**
* SCP is very useful for the bulk transfer of directory across the network. In this
* example we create a temporary local file structure and copy it to the remote
* server, then copy it back.
*/
try (SshClient ssh = new SshClient(hostname, port, username, password.toCharArray())) {
ScpClient scp = new ScpClient(new DirectFileFactory(new File(".")), ssh);
createFile("tmp_dir/1.dat", "10kb");
createFile("tmp_dir/2.dat", "100kb");
createFile("tmp_dir/3.dat", "1mb");
createFile("tmp_dir/child/4.dat", "10kb");
createFile("tmp_dir/child/5.dat", "16kb");
scp.put("tmp_dir", ".", true, null, true);
scp.get("tmp_return", "tmp_dir", true, null);
ssh.disconnect();
} catch (PermissionDeniedException | ChannelOpenException e) {
e.printStackTrace();
}
} catch (IOException | SshException e) {
e.printStackTrace();
}
}
private static File createFile(String filename, String size) throws IOException {
File file = new File(filename);
file.getParentFile().mkdirs();
file.createNewFile();
RandomAccessFile localFile = new RandomAccessFile(file, "rw");
localFile.setLength(IOUtils.fromByteSize(size));
localFile.close();
return file;
}
}
Code language: JavaScript (javascript)