1. Brief description
In daily development, many applications need to remotely connect to the server through the SSH protocol to execute commands, upload or download files. JSch (Java Secure Channel) is a powerful Java library that provides convenient interfaces to implement SSH connections, SFTP file transfers, and other remote management functions.
2. Core functions
- SSH remote command execution: Run commands on a remote server through the SSH protocol.
- SFTP file transfer: supports uploading and downloading of files.
- Port forwarding: Supports local port and remote port forwarding.
- Key login: Support authentication through password and private key.
Before using JSch, you need to add its dependencies. Here are JSch's Maven dependencies:
<dependency> <groupId></groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency>
3. Examples of use
3.1 Remotely execute commands
The following code shows how to connect to a remote server and execute commands via SSH:
import ; import ; import ; import ; public class JSchExecExample { public static void main(String[] args) { String host = "your-server-ip"; String user = "your-username"; String password = "your-password"; try { JSch jsch = new JSch(); Session session = (user, host, 22); (password); // Ignore host key check ("StrictHostKeyChecking", "no"); (); // Open the channel for executing the command ChannelExec channel = (ChannelExec) ("exec"); ("ls -l"); // Get command output InputStream inputStream = (); (); byte[] tmp = new byte[1024]; while ((tmp) != -1) { (new String(tmp)); } (); (); } catch (Exception e) { (); } } }
3.2 Upload files to remote server (SFTP)
The following code shows how to upload a file via SFTP:
import ; import ; import ; public class JSchSFTPUploadExample { public static void main(String[] args) { String host = "your-server-ip"; String user = "your-username"; String password = "your-password"; String localFile = "/path/to/local/"; String remoteDir = "/path/to/remote/dir/"; try { JSch jsch = new JSch(); Session session = (user, host, 22); (password); // Ignore host key check ("StrictHostKeyChecking", "no"); (); // Open SFTP channel ChannelSftp channel = (ChannelSftp) ("sftp"); (); // Upload file (localFile, remoteDir); ("File uploaded successfully!"); (); (); } catch (Exception e) { (); } } }
3.3 Download the file to local (SFTP)
import ; import ; import ; public class JSchSFTPDownloadExample { public static void main(String[] args) { String host = "your-server-ip"; String user = "your-username"; String password = "your-password"; String remoteFile = "/path/to/remote/"; String localDir = "/path/to/local/dir/"; try { JSch jsch = new JSch(); Session session = (user, host, 22); (password); // Ignore host key check ("StrictHostKeyChecking", "no"); (); // Open SFTP channel ChannelSftp channel = (ChannelSftp) ("sftp"); (); // Download the file (remoteFile, localDir); ("File downloaded successfully!"); (); (); } catch (Exception e) { (); } } }
3.4 Authentication with private key
If you need to use a private key instead of a password for authentication, you can set it as follows:
import ; import ; import ; import ; public class JSchPrivateKeyExample { public static void main(String[] args) { String host = "your-server-ip"; String user = "your-username"; String privateKey = "/path/to/private/key"; try { JSch jsch = new JSch(); (privateKey); Session session = (user, host, 22); ("StrictHostKeyChecking", "no"); (); ChannelExec channel = (ChannelExec) ("exec"); ("ls -l"); InputStream inputStream = (); (); byte[] tmp = new byte[1024]; while ((tmp) != -1) { (new String(tmp)); } (); (); } catch (Exception e) { (); } } }
3.5 Local port forwarding
Implementing local port forwarding through JSch allows local ports to access remote services:
import ; import ; public class JSchPortForwarding { public static void main(String[] args) { String host = "your-server-ip"; String user = "your-username"; String password = "your-password"; try { JSch jsch = new JSch(); Session session = (user, host, 22); (password); ("StrictHostKeyChecking", "no"); (); // Set local port forwarding int assignedPort = (8888, "localhost", 8080); ("Local port forwarding: localhost:" + assignedPort + " -> localhost:8080"); // Stay connected (60000); (); } catch (Exception e) { (); } } }
4. Summary
The common usage examples of JSch above. Common usage scenarios for JSch are automated remote management, file transfer, SSH tunnel construction, etc. Here are the advantages of JSch:
- Lightweight: A single JAR package is ready to use.
- Rich features: supports SSH, SFTP, port forwarding and other functions.
- Open source free: no additional cost.
This is the article about this technical guide on Java's remote operation of SSH using JSch. For more related Java JSch SSH remote operation content, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!