[JSch-users] SSH exec hangs on HP Unix
Status: Alpha
Brought to you by:
ymnk
From: Guy <guy...@gm...> - 2007-04-03 09:32:30
|
Hi all, i am using jsch-0.1.31 to connect to a remote machine and call scripts. For some reason, my code works on a windows machine (Windows XP SP2), but it does not work on a HP Unix server (i think HP UX 11.11). I use password authentication by putting it in a UserInfo class. Dont mind the return object and how it is created. What matters is that if i call this method twice, the second command hangs on HP Unix. For example if i call Entry entry = exec.execCommand(". /etc/profile;. ./.profile;ace_test ls -al /"); System.out.println(entry.getAttribute("output").getValues()[0].toString()); entry = exec.execCommand(". /etc/profile;. ./.profile;ace_test ls -l"); System.out.println(entry.getAttribute("output").getValues()[0].toString()); entry = exec.execCommand(". /etc/profile;. ./.profile;ace_test ll"); System.out.println(entry.getAttribute("output").getValues()[0].toString()); entry = exec.execCommand(". /etc/profile;. ./.profile;ace_test ls "); System.out.println(entry.getAttribute("output").getValues()[0].toString()); It will hang on the second execCommand and consume 100% CPU. Here is the execCommand method: public Entry execCommand(String command) throws Exception { logger.info("Starting execCommand"); Entry returnObject = new Entry(); BufferedReader bufRead = null; try{ StringBuffer strBuff = new StringBuffer(); if (!connected) { createSession(); } Channel channel=session.openChannel("exec"); ((ChannelExec)channel).setCommand(command); InputStream instream = channel.getInputStream() ; // I know, I could do this line and the line below in one, but you *need* the inputstream for status input InputStreamReader input = new java.io.InputStreamReader ( instream ) ; bufRead = new java.io.BufferedReader ( input ) ; logger.info("executing command '" + command + "'"); channel.connect(); while ( true ) { while ( bufRead.ready () ) { // line at a time, note the buffer can empty with more data available, use the isEOF.. below to find out when it's truly donw strBuff.append(bufRead.readLine()).append("\n"); } if ( channel.isEOF() && instream.available() == 0 ) { // tells us when the command has completed returning data break ; } } logger.info("Command executed."); returnObject.addAttributeValue("output", strBuff.toString()); returnObject.addAttributeValue("exitstatus", "" + channel.getExitStatus()); channel.disconnect(); } catch(Exception e){ connected = false; logger.error("An unexpected error occured when executing command '" + command + "': " + e.getClass().getName() + " " + e.getMessage()); throw e; } finally { try{bufRead.close();}catch(Exception e){} } logger.info("Ending execCommand"); return returnObject; } Any help would be appreciated !!! Guy |