Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run SSH commands on remote system using Java? [closed]

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.

like image 244
sweety Avatar asked Mar 25 '10 09:03

sweety


People also ask

Can you SSH with Java?

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.

How do I connect to a remote computer using 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.


2 Answers

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(); 
like image 85
bobah Avatar answered Oct 16 '22 16:10

bobah


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.

like image 38
mehdi ali soltani Avatar answered Oct 16 '22 17:10

mehdi ali soltani