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!

Simple Java SSH Client

807591Mar 28 2008 — edited Mar 30 2008
Hi all,

Am trying to construct a simple, crude but working Java SSH client. Running on Linux, all I expect is for it to just log in through SSH into my localhost, do an ls command then log out. But it seems it doesnt want to log in automatically. My code is as below:

(Before I give my code, just as a note, wiZ4rD is my password so don't be confused about the String)

Here we go:
import java.io.*;

public class SSH
{
	static public void main(String[] args) throws Exception
	{
		// The runtime instance 
                Runtime rt = Runtime.getRuntime();
                // Command array to hold the process to be carried out
                String[] command = {"ssh", "127.0.0.1"};
                // The process hook 
                Process proc = rt.exec(command);

		// Wait for the reading process to be completed
                proc.waitFor();


		OutputStream stdout = proc.getOutputStream();
		stdout.write("wiZ4rD\n".getBytes());
		stdout.write("ls".getBytes());

                // Get the stream from the process
                InputStream stdin = proc.getInputStream();
		
		
                InputStreamReader isr = new InputStreamReader(stdin);
                BufferedReader br = new BufferedReader(isr);
                // Get the stream from the process line by line
		String line = "";
                while ((line = br.readLine()) != null)
                {
                    // And append to the content string the stream read
                    System.out.print(line);
		    
                }
		stdout.write("exit\r".getBytes());
	}
}
I really wonder why it is asking me for password yet I have specified in it to write it out right away...
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 27 2008
Added on Mar 28 2008
11 comments
86 views