I am new to this kind of Java application and looking for some sample code on how to connect to a remote server using SSH , execute commands, and get output back using Java as programming language.
JSch is the Java implementation of SSH2 that allows us to connect to an SSH server and use port forwarding, X11 forwarding, and file transfer. Also, it is licensed under the BSD style license and provides us with an easy way to establish an SSH connection with Java.
You could install an SSH server on your remote desktop and you can write a Java program using jcraft and jsch libraries on your local machine to make an SSH connection to your remote desktop.
Have a look at Runtime.exec() Javadoc
Process p = Runtime.getRuntime().exec("ssh myhost"); PrintStream out = new PrintStream(p.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); out.println("ls -l /home/me"); while (in.ready()) { String s = in.readLine(); System.out.println(s); } out.println("exit"); p.waitFor();
JSch is a pure Java implementation of SSH2 that helps you run commands on remote machines. You can find it here, and there are some examples here.
You can use exec.java
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With