SshUtil { public static String executeCommand( Session sessionObj, String command ) throws JSchException, IOException { StringBuilder builder = null; logger.debug( "Starting to execute command [" + command + "]" ); if ( sessionObj != null && command != null && !"".equals( command ) ) { builder = new StringBuilder(); Channel channel = null; int arrMaxSize = 1024; try { channel = sessionObj.openChannel( "exec" ); ( (ChannelExec) channel ).setCommand( command ); channel.setInputStream( null ); ( (ChannelExec) channel ).setErrStream( System.err ); InputStream in = channel.getInputStream(); channel.connect(); byte[] tmp = new byte[arrMaxSize]; while ( true ) { while ( in.available() > 0 ) { int i = in.read( tmp, 0, arrMaxSize ); if ( i < 0 ) break; builder.append( new String( tmp, 0, i ) ); } if ( channel.isClosed() ) { break; } try { Thread.sleep( 500 ); } catch ( Exception ee ) { } } if ( channel.isClosed() && channel.getExitStatus() != 0 ) { logger.debug( "Command exited with error code " + channel.getExitStatus() ); } } catch ( Exception e ) { logger.error( "Received exception during command execution", e ); } finally { if ( channel != null && channel.isConnected() ) { channel.disconnect(); } logger.debug( "End of execution of command [" + command + "]" ); } return builder.toString(); } return null; }  static Session getSessionObject( String userName, String password, String hostName, int port,
                                            Properties sessionConfig ); static String executeCommand( Session sessionObj, String command ); static void upload( Session session, InputStream localInputStream, String remoteFile ); static void download( Session session, OutputStream localOutputStream, String remoteFile ); static SftpATTRS lstat( Session session, String remoteFile );  }
@Test public void testExecuteCommand() throws JSchException { logger.info( "[TS] : testExecuteCommand" ); String username = "root"; String password = "root123"; String hostname = "10.28.197.23"; int port = 22; Properties sessionConfig = new java.util.Properties(); sessionConfig.put( "StrictHostKeyChecking", "no" ); Session sessionobj = null; sessionobj = SshUtil.getSessionObject( username, password, hostname, port, sessionConfig ); sessionobj.connect( 6000 ); String command = "vmware -v"; String expResult = ""; String result = null; try { result = SshUtil.executeCommand( sessionobj, command ); } catch ( Exception ex ) { logger.error( "Exception occured while executing command.", ex ); } logger.info( "[TS] : Expected result : Result of the command is NOT NULL, actual result : Result of the command = " + result ); assertNotNull( result ); }
@Test public void testExecuteCommandWithNullSessionObj() { logger.info( "[TS] : testExecuteCommandWithNullSessionObj" ); Session sessionobj = null; String command = "random cmd"; String expResult = ""; String result = null; try { result = SshUtil.executeCommand( sessionobj, command ); } catch ( JSchException ex ) { } catch ( IOException ex ) { } logger.info( "[TS] : Expected result : Result of the command is NULL, actual result : Result of the command = " + result ); assertNull( result ); }