Executing commands on a remote machine from Java with JSch - LinuxConfig.org

SSH is an every-day tool of any Linux System Administration job. It is an easy and secure way to access remote machines on the network, transfer data and execute remote commands. Apart from interactive mode, there are many tools exist that enable automation of remote tasks that also rely on the existing ssh server/client architecture.

This is a companion discussion topic for the original entry at https://linuxconfig.org/executing-commands-on-a-remote-machine-from-java-with-jsch

can you please help to get sudo access using “shell” channel? I am able to get it through “exec” channel but, struggling to capture the output. want to go with “shell” channel.

Hi Vaibhav_Nandkule,

Welcome to our forums.

In this article the example code all the output provided by the session is stored in a StringBuilder instance named outBuff. If you would like to act depending on the response of the remote machine, you’ll have to parse the output, and build decision logic into your application depending on the answers you identified in the output.