  | Mailing List | | Home | | Forum Home | | JBoss - Java Application Server | | Struts - A MVC web framework | | Tomcat - JSP/Servlet container | | iText - An open source PDF Java Library | | JDOM - JDOM XML Parser | | J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition | | J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog | | Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology | | JSP - A mailing list about Java Server Pages specification and reference | |
Struts & Hibernate
|
|
|
  | | | use of RunTime class | use of RunTime class 2003-07-28 - By Prashant
Back Hi Group, We are developing an application which requires connecting to the remote server using ssh protocol. If I have to connect to that server from command prompt then I type command something like below,
ssh -l <usrname> -p <portname> <ip address> Then the system shows following line <Hostname> password:
Then the remote server ask for password (We don't want to use public and private key automatic validation) ,which I enter manually and I get the connection to the remote server.
Now the same functionality we want to duplicate in a java program. So we thought of using Runtime class to execute the command mentioned above. The sample code for that is given below
import java.io.*;
public class one{
public one() throws Exception{ Runtime run=Runtime.getRuntime(); System.out.println("came here up"); Process pr=run.exec("ssh -l <usrname> -p <portname> <ip address>" ); System.out.println("came here down"); InputStream in=pr.getInputStream(); System.out.println(in.available()); BufferedReader buff=new BufferedReader(new InputStreamReader(pr .getInputStream())); System.out.println("1"); String line=buff.readLine(); System.out.println("2"); System.out.println(line); }
public static void main(String a[]){ try{ new one(); }catch(Exception ee){ ee.printStackTrace(); } }
}//end of class
We are getting output till 'System.out.println("1")' , but the ' System.out .println("2") ' is not coming at all on the screen. The process remains in the hanged status and I think it expects some user input. But I don't understand why it is not printing '<Hostname> password: ' line though I am capturing the input stream of the process.
Can anyone tell me why this is happening. If anybody has already implemented such kind of functionality where 'java program will supply the password to connect to the remote server' then kindly suggest us how to do that.
Thanx in advance
Prashant
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META content="text/html; charset=iso-8859 (See http://iso-8859.ora-code.com)-1" http-equiv=Content-Type> <META content="MSHTML 5.00.2919.6307" name=GENERATOR> <STYLE></STYLE> </HEAD> <BODY bgColor=#ffffff> <DIV><FONT face=Verdana size=2>Hi Group,</FONT></DIV> <DIV><FONT face=Verdana size=2> We are developing an application which requires connecting to the remote server using ssh protocol. If I have to connect to that server from command prompt then I type command something like below,</FONT></DIV> <DIV> </DIV> <DIV><FONT face=Verdana size=2> <FONT color=#ff0000>ssh -l <usrname> -p <portname> <ip address></FONT></FONT></DIV> <DIV><FONT face=Verdana size=2> Then the system shows following line</FONT></DIV> <DIV><FONT face=Verdana size=2> <FONT color=#ff0000><Hostname> password: </FONT></FONT></DIV> <DIV> </DIV> <DIV> </DIV> <DIV><FONT face=Verdana size=2> Then the remote server ask for password (We don't want to use public and private key automatic validation) ,which I enter manually and I get the connection to the remote server.</FONT></DIV> <DIV> </DIV> <DIV><FONT face=Verdana size=2> Now the same functionality we want to duplicate in a java program. So we thought of using Runtime class to execute the command mentioned above. The sample code for that is given below</FONT></DIV> <DIV> </DIV> <DIV> </DIV> <DIV><FONT face=Verdana size=2> import java.io.*;<BR></FONT></DIV> <DIV><FONT face=Verdana size=2> public class one{</FONT></DIV> <DIV> </DIV> <DIV><FONT face=Verdana size=2> public one() throws Exception{<BR> Runtime run=Runtime.getRuntime();<BR> System.out.println("came here up");<BR> Process pr=run.exec("ssh -l <usrname> -p <portname> <ip address>");<BR> System.out.println("came here down");<BR> InputStream in=pr.getInputStream();<BR> System.out.println(in.available());<BR> BufferedReader buff=new BufferedReader (new InputStreamReader(pr.getInputStream()));<BR> System.out.println("1");<BR> String line=buff.readLine();<BR> System.out.println("2");<BR></FONT><FONT face=Verdana size=2> System.out.println(line);<BR> }</FONT></DIV> <DIV> </DIV> <DIV><FONT face=Verdana size=2> public static void main(String a[]){<BR> try{<BR> new one();<BR> }catch(Exception ee){<BR>   ; ee.printStackTrace();<BR> }<BR> }</FONT></DIV> <DIV> </DIV> <DIV><FONT face=Verdana size=2> }//end of class</FONT></DIV> <DIV> </DIV> <DIV> </DIV> <DIV><FONT face=Verdana size=2> We are getting output till 'System.out.println("1")' , but the ' System.out.println("2") ' is not coming at all on the screen. The process remains in the hanged status and I think it expects some user input. But I don't understand why it is not printing '<Hostname> password: ' line though I am capturing the input stream of the process.</FONT></DIV> <DIV><FONT face=Verdana size=2></FONT> </DIV> <DIV> </DIV> <DIV><FONT face=Verdana size=2> Can anyone tell me why this is happening. If anybody has already implemented such kind of functionality where 'java program will supply the password to connect to the remote server'</FONT> <FONT face=Verdana size=2> then kindly suggest us how to do that.</FONT></DIV> <DIV> </DIV> <DIV><FONT face=Verdana size=2>Thanx in advance</FONT></DIV> <DIV> </DIV> <DIV><FONT face=Verdana size=2>Prashant</FONT></DIV></BODY></HTML>
|
|
 |