How to Execute Linux/Unix Command on a Remote Server Using SSH [Snippets]
The JSsh-client is a Java-based library build on top of the JSch Library to perform an operation on the remote server over SSH and SFTP channel.
The JSsh-client is a Java-based library build on top of the JSch Library to perform an operation on the remote server over SSH and SFTP channels. It has the following features:
- Test SSH connection with a remote SSH server.
- Execute a command on a remote server using Execute Channel.
- Execute commands on a remote server using Shell channel.
- Upload/Download the file to/from the remote server from/to local using the SFTP channel.
How to Use It?
Step 1: Add Maven Dependency to POM.xml [Use Lastest Version]
Current version: 0.0.2
<dependency>
<groupId>io.github.manojpawar94</groupId>
<artifactId>jssh-client</artifactId>
<version>${current_verion}</version>
</dependency>
Step 2: Get Instance of JSshClient
JSshClient is an interface. It has a singleton implementation class JscraftClient. We can get an instance of the class as below:
JSshClient client = JscraftClient.getInstance();
Step 3: Create the JSshProxy Object instance
It has various options to set based upon our needs. It supports both password-based and SSH key-based authentication mechanisms. To set authentication mechanism we need to set respective attributes to the JSshProxy object.
java.util.Properties properties = new java.util.Properties();
JSshProxy<?> jSshProxy = JSshProxy.builder()
.hostname("123.12.34.5")
.port(22)
.username("test")
.password("test@123")
//.privateKey("")
.knownHostsFileName("")
.sessionTimeOut(10000)
.channelTimeOut(5000)
.properties(properties)
.enablePty(true)
//.ptyType("ANSI") used only for shell channel operation
.build();
Step 4a: Test SSH Connection With Remote Server
JSshProxy<?> jSshProxy = JSshProxy.builder()
.hostname("123.12.34.5")
.username("test")
.password("test@123")
.enablePty(true)
.build();
JSshResult jSshResult = client.testSSH(jSshProxy);
Step 4b: Execute a Command on A Remote Server Using Execute Channel
String command = "ls -lrt";
JSshProxy<String> jSshProxy = JSshProxy.builder()
.hostname("123.12.34.5")
.username("test")
.password("test@123")
.enablePty(true)
.task(command)
.build();
JSshResult jSshResult = client.execute(jSshProxy);
Step 4c: Execute a Command on A Remote Server Using Execute Channel
ShellCommand command = ShellCommand.builder()
.command("ls -lrt")
.command("pwd")
.command("who i am")
.build();
JSshProxy<ShellCommand> jSshProxy = JSshProxy.builder()
.hostname("123.12.34.5")
.username("test")
.password("test@123")
.enablePty(true)
.task(command)
.build();
JSshResult jSshResult = client.shell(jSshProxy);
Step 4d: Upload the File to The Remote Server From Local Using the SFTP Channel
String from = "C:\\Users\\sample_file_to_upload.txt";
String to = "/home/user/upload";
SftpCommand command = SftpCommand.builder()
.from(from)
.to(to)
.sftpType(SftpType.PUT)
.build();
JSshProxy<ShellCommand> jSshProxy = JSshProxy.builder()
.hostname("123.12.34.5")
.username("test")
.password("test@123")
.enablePty(true)
.task(command)
.build();
JSshResult jSshResult = client.sftp(jSshProxy);
Step 4e: Download the File From The Remote Server to Local Using the SFTP Channel
String from = "/home/user/sample_file_to_download.txt";
String to = "C:\\Users\\Downloads";
SftpCommand command = SftpCommand.builder()
.from(from)
.to(to)
.sftpType(SftpType.GET)
.build();
JSshProxy<ShellCommand> jSshProxy = JSshProxy.builder()
.hostname("123.12.34.5")
.username("test")
.password("test@123")
.enablePty(true)
.task(command)
.build();
JSshResult jSshResult = client.sftp(jSshProxy);Opinions expressed by DZone contributors are their own.