JSch library to execute shell commands on a Unix

In today’s tutorial, we will explore the example of JSch. JSch is a tool in Java that allows us to establish an SSH connection. I have previously written a program to connect to a remote database on an SSH server. Today, I will be sharing a program that connects to an SSH-enabled server and executes shell commands. For this task, I will be utilizing JSch to establish the connection from a Java program.

Example of JSch

You have the option to download the JSch jar from its official website or obtain the JSch jars through the provided maven dependency.

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.53</version>
</dependency>

Here is a basic program written in JSch to execute the command “ls -ltr” on the server.

import java.io.InputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;


public class JSchExampleSSHConnection {

	/**
	 * JSch Example Tutorial
	 * Java SSH Connection Program
	 */
	public static void main(String[] args) {
	    String host="ssh.scdev.com";
	    String user="sshuser";
	    String password="sshpwd";
	    String command1="ls -ltr";
	    try{
	    	
	    	java.util.Properties config = new java.util.Properties(); 
	    	config.put("StrictHostKeyChecking", "no");
	    	JSch jsch = new JSch();
	    	Session session=jsch.getSession(user, host, 22);
	    	session.setPassword(password);
	    	session.setConfig(config);
	    	session.connect();
	    	System.out.println("Connected");
	    	
	    	Channel channel=session.openChannel("exec");
	        ((ChannelExec)channel).setCommand(command1);
	        channel.setInputStream(null);
	        ((ChannelExec)channel).setErrStream(System.err);
	        
	        InputStream in=channel.getInputStream();
	        channel.connect();
	        byte[] tmp=new byte[1024];
	        while(true){
	          while(in.available()>0){
	            int i=in.read(tmp, 0, 1024);
	            if(i<0)break;
	            System.out.print(new String(tmp, 0, i));
	          }
	          if(channel.isClosed()){
	            System.out.println("exit-status: "+channel.getExitStatus());
	            break;
	          }
	          try{Thread.sleep(1000);}catch(Exception ee){}
	        }
	        channel.disconnect();
	        session.disconnect();
	        System.out.println("DONE");
	    }catch(Exception e){
	    	e.printStackTrace();
	    }

	}

}

If you encounter any issues with running the JSch example program, please inform me. This program demonstrates a simple method in JSch to establish an SSH connection in a Java program. The JSch jar file can be obtained from the official website.

 

More Tutorials

Common errors that occur when using Nginx for connections.(Opens in a new browser tab)

Using Telnet Commands in Linux/Unix(Opens in a new browser tab)

Server Configurations Frequently Used for Your Web Application(Opens in a new browser tab)

Spring Boot CLI(Opens in a new browser tab)

multithreading in Java that you need to know(Opens in a new browser tab)

Leave a Reply 0

Your email address will not be published. Required fields are marked *