-1

My question is related using plink.exe as a Java Process.

This is inside the run() method of a Thread:

Runtime r = Runtime.getRuntime();
Process p = r.exec("plink -ssh " + target_ip_address);

InputStream in = p.getInputStream();
OutputStream out = p.getOutputStream();

This code is inside an infinite while loop

// Wait until "in" has bytes available 
while ( !(in.available() > 0) ) {
  // Here I have some code that will break out of the while after 10 seconds.
// I use Thread.sleep( 100 ) and count the number of times it gets called
}

StringBuilder response = new StringBuilder();
int responseByte = 0;

while ( in.available() > 0 ) {
  responseByte = in.read();
  response.append( (char) responseByte );
}

My two problems are these:

  • If the server's host key is not cached in the registry, in a NORMAL windows command prompt, I will get a message asking to store this key in the cache. In Java, "in" NEVER receives this message, why?

  • How do I check if the password is wrong? Let me explain a little better...

In order to check if I can send the username, password or any command, I do:

if ( response.contains("login as: ") ){
  out.write( username + "\r\n".getBytes() );
  out.flush();
}
else if ( response.contains("password: ") ) {
  out.write( password + "\r\n".getBytes() );
  out.flush();
}

If the password is wrong, in the windows command prompt I get an Access denied message, but in the Java Process InputStream "in", I do not get this. Why is this happening?

Here is a sample of the windows command prompt action:

E:\path\to\plink>plink -ssh 128.128.128.128
The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n) n
login as: username
username@128.128.128.128's password:
Access denied
username@128.128.128.128's password:

So, I was thinking that the InputStream of the Java Process should receive both the key message and the Access denied, but they do not.

BTW, I also tried looking at the error stream

InputStream err = p.getErrorStream();

and doing the same thing that I did for "in", but NOTHING there either.

Is there any way to make those two messages show up in the Process' input stream?

Any help greatly appreciated! Thank you very much!

1 Answers1

0

Sorry for answering my own question, but I found out the problem...

It turns out that the Error stream DOES, in fact, receive both the ssh key message and the "Access denied" messages, however, the "Access denied" messages are received after ALL failed authorization attempts and the connection is closed.

I solved it by just sending the password until the connection is closed. I know it is very bad solution, but I will start migrating the code to use JSch as Martin Prikryl suggested

Thanks and sorry for the inconvenience!