DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Architecting Proactive IT: NinjaOne Remote Monitoring and Management
  • 7 Techniques That Supercharged My Claude-Assisted Development
  • Beyond Request-Response: Architecting Stateful Agentic Chatbots with the Command and State Patterns
  • From 13,000 to 20,000+ Endpoints: Architecting Forensics for the Remote Workforce

Trending

  • I Built a Java Version Manager by Fixing Other Tools' Open Bugs
  • From APIs to Agents: How Back-End Engineering is Evolving in 2026
  • What Is Agentic Test Creation and How Is It Different from AI Test Generation?
  • Why MCP Servers Lose Session State Behind Load Balancers

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.

By 
Manoj Pawar user avatar
Manoj Pawar
·
Apr. 09, 21 · Code Snippet
Likes (5)
Comment
Save
Tweet
Share
8.3K Views

Join the DZone community and get the full member experience.

Join For Free

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);
remote Command (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Architecting Proactive IT: NinjaOne Remote Monitoring and Management
  • 7 Techniques That Supercharged My Claude-Assisted Development
  • Beyond Request-Response: Architecting Stateful Agentic Chatbots with the Command and State Patterns
  • From 13,000 to 20,000+ Endpoints: Architecting Forensics for the Remote Workforce

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook