0

What will be the java code for connecting to linux box ? Manually I connect through Putty so my question how shall I integrate in java and connect Linux box Or there is any other way for connecting it.

I tried this code it didn't work. Please

import java.io.InputStream;
import java.io.OutputStream;

public class Test {

  public static void main( String[] args ) throws Exception {

      String[] cm = {
         "ssh= C:\\users\\...\\putty.exe",
          "[email protected]" ,
          "df -h"
      };
      Process p = Runtime.getRuntime().exec( cm );
      InputStream in = p.getInputStream();
      OutputStream out = p.getOutputStream();
      out.write( "password".getBytes() );
  }
}

1 Answer 1

1

SSH library for Java

You probably want to use an SSH library for this. See the above question for answers.

Manually invoking Putty is far too complicated and your code isn't doing what you think it will. First of all, the stdin of your child process is not going to end up in the Putty window. Second, any time a password is being read from a console it isn't (usually) reading from stdin directly.

But why are you doing this? I can see two possible reasons

Do you want to make a Java program launch Putty programatically?

In this case, your idea of launching a child process will work, however you will need to have an ssh key setup between Putty and the Linux machine to avoid manually entering the password each time.

Do you want to open a connection to a Linux machine and run some commands through it?

You might want to familiarize yourself with SSH and what it can do first. Chances are you can probably set up a simple shell script on the server, log in with a Java ssh client and run it through this connection.

I tried this code it didn't work.

Also, welcome to StackOverflow. Saying "it didn't work" without providing details about the problem doesn't help us help you. What errors messages did you see? What went wrong and what were you expecting to happen?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mike ! Next time I will sure keep in mind to put error messages along with the question!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.