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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Improving Repeated Analytics Workloads With Databricks Disk Cache
  • 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

Trending

  • Beyond @Transactional: Solving the Dual-Write Problem in Distributed Microservices
  • Going All In On the JVM 25 Virtual Threads
  • The Twelve-Factor Agents: Building Production-Ready LLM Applications
  • Data Integration Landscape 2026: Event Streaming, API, and Batch in the Era of Agentic AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

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
·
Apr. 09, 21 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Improving Repeated Analytics Workloads With Databricks Disk Cache
  • 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

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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