Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to ssh a server with user and password

629006Sep 10 2009 — edited Oct 26 2009
i want to ssh a server and run some commands ...

i tried that code but i prompt for password is there any api for ssh how can i do that
import java.util.*;
import java.io.*;
import java.sql.*;
import java.text.*;
import java.lang.*;
import java.io.IOException;
import java.net.SocketException;








public class MyClass
{

public static void main( String[] args )
        {

File wd = new File("/bin");
System.out.println(wd);
Process proc = null;
Process proc2 = null;
try {
   proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
}
catch (IOException e) {
   e.printStackTrace();
}
if (proc != null) {
   BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
   PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
   out.println("cd /");
   out.println("ssh root@122.20.19.10");
  out.println("cd /");
 out.println("ls");

  out.println("pwd");




   out.println("exit");

try {
      String line;
      while ((line = in.readLine()) != null) {
         System.out.println(line);
      }
      proc.waitFor();
      in.close();
      out.close();
      proc.destroy();
   }
   catch (Exception e) {
      e.printStackTrace();
   }
}


    }//end of main mehtod

}//END OF CLASS
thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 23 2009
Added on Sep 10 2009
6 comments
2,501 views