@Ignore( "TODO: Provide a mocked SSH sink " ) @Test( expected=SdiException.class ) public void testChown() throws Throwable { String command1 = "chown www-data:www-data /var/www/oxwall-1.6.0.zip"; myClassUnderTest.executeCmd( command1 ); }
public void executeCmd( String aCommand ) throws SdiException { if ( mySession == null ) { throw new SdiException( "SSH session not initialized.", SdiException.EXIT_CODE_SSH_ERROR ); } if ( !mySession.isConnected() ) { throw new SdiException( "SSH session not connected.", SdiException.EXIT_CODE_SSH_ERROR ); } Channel channel = null; try { channel = mySession.openChannel( "exec" ); myLog.debug( "executing SSH command " + aCommand ); ( (ChannelExec) channel ).setCommand( aCommand ); ByteArrayOutputStream osOut = new ByteArrayOutputStream(); ByteArrayOutputStream osErr = new ByteArrayOutputStream(); try { channel.setInputStream( null ); channel.setOutputStream(osOut, true ); ( (ChannelExec) channel ).setErrStream( osErr, true ); myLog.debug( "connecting the channel" ); channel.connect(); while ( true ) { if ( channel.isClosed() ) { break; } try { myLog.debug( "going to sleep 50 ms" ); Thread.sleep( 50 ); } catch ( Exception ee ) { } } } finally { String out = new String( osOut.toByteArray() ); if ( StringUtils.hasText( out ) ) { myLog.info( "Captured SSH out:\n" + out ); } String err = new String( osErr.toByteArray() ); if ( StringUtils.hasText( err ) ) { myLog.warn( "Captured SSH err:\n" + err ); } } } catch ( Throwable t ) { throw new SdiException( "SSH session not connected.", t, SdiException.EXIT_CODE_SSH_ERROR ); } finally { if ( channel != null ) { int exitStatus = channel.getExitStatus(); myLog.debug( "SSH exit status: " + exitStatus ); myLog.debug( "disconnecting the channel" ); channel.disconnect(); if ( exitStatus != 0 ) { throw new SdiException( "SSH not successful. SSH-Exitstatus: " + exitStatus, SdiException.EXIT_CODE_SSH_ERROR ); } } } }
SshExecutor { public void executeCmd( String aCommand ) throws SdiException { if ( mySession == null ) { throw new SdiException( "SSH session not initialized.", SdiException.EXIT_CODE_SSH_ERROR ); } if ( !mySession.isConnected() ) { throw new SdiException( "SSH session not connected.", SdiException.EXIT_CODE_SSH_ERROR ); } Channel channel = null; try { channel = mySession.openChannel( "exec" ); myLog.debug( "executing SSH command " + aCommand ); ( (ChannelExec) channel ).setCommand( aCommand ); ByteArrayOutputStream osOut = new ByteArrayOutputStream(); ByteArrayOutputStream osErr = new ByteArrayOutputStream(); try { channel.setInputStream( null ); channel.setOutputStream(osOut, true ); ( (ChannelExec) channel ).setErrStream( osErr, true ); myLog.debug( "connecting the channel" ); channel.connect(); while ( true ) { if ( channel.isClosed() ) { break; } try { myLog.debug( "going to sleep 50 ms" ); Thread.sleep( 50 ); } catch ( Exception ee ) { } } } finally { String out = new String( osOut.toByteArray() ); if ( StringUtils.hasText( out ) ) { myLog.info( "Captured SSH out:\n" + out ); } String err = new String( osErr.toByteArray() ); if ( StringUtils.hasText( err ) ) { myLog.warn( "Captured SSH err:\n" + err ); } } } catch ( Throwable t ) { throw new SdiException( "SSH session not connected.", t, SdiException.EXIT_CODE_SSH_ERROR ); } finally { if ( channel != null ) { int exitStatus = channel.getExitStatus(); myLog.debug( "SSH exit status: " + exitStatus ); myLog.debug( "disconnecting the channel" ); channel.disconnect(); if ( exitStatus != 0 ) { throw new SdiException( "SSH not successful. SSH-Exitstatus: " + exitStatus, SdiException.EXIT_CODE_SSH_ERROR ); } } } } }
SshExecutor { public void executeCmd( String aCommand ) throws SdiException { if ( mySession == null ) { throw new SdiException( "SSH session not initialized.", SdiException.EXIT_CODE_SSH_ERROR ); } if ( !mySession.isConnected() ) { throw new SdiException( "SSH session not connected.", SdiException.EXIT_CODE_SSH_ERROR ); } Channel channel = null; try { channel = mySession.openChannel( "exec" ); myLog.debug( "executing SSH command " + aCommand ); ( (ChannelExec) channel ).setCommand( aCommand ); ByteArrayOutputStream osOut = new ByteArrayOutputStream(); ByteArrayOutputStream osErr = new ByteArrayOutputStream(); try { channel.setInputStream( null ); channel.setOutputStream(osOut, true ); ( (ChannelExec) channel ).setErrStream( osErr, true ); myLog.debug( "connecting the channel" ); channel.connect(); while ( true ) { if ( channel.isClosed() ) { break; } try { myLog.debug( "going to sleep 50 ms" ); Thread.sleep( 50 ); } catch ( Exception ee ) { } } } finally { String out = new String( osOut.toByteArray() ); if ( StringUtils.hasText( out ) ) { myLog.info( "Captured SSH out:\n" + out ); } String err = new String( osErr.toByteArray() ); if ( StringUtils.hasText( err ) ) { myLog.warn( "Captured SSH err:\n" + err ); } } } catch ( Throwable t ) { throw new SdiException( "SSH session not connected.", t, SdiException.EXIT_CODE_SSH_ERROR ); } finally { if ( channel != null ) { int exitStatus = channel.getExitStatus(); myLog.debug( "SSH exit status: " + exitStatus ); myLog.debug( "disconnecting the channel" ); channel.disconnect(); if ( exitStatus != 0 ) { throw new SdiException( "SSH not successful. SSH-Exitstatus: " + exitStatus, SdiException.EXIT_CODE_SSH_ERROR ); } } } }  }
SshExecutor { public void executeCmd( String aCommand ) throws SdiException { if ( mySession == null ) { throw new SdiException( "SSH session not initialized.", SdiException.EXIT_CODE_SSH_ERROR ); } if ( !mySession.isConnected() ) { throw new SdiException( "SSH session not connected.", SdiException.EXIT_CODE_SSH_ERROR ); } Channel channel = null; try { channel = mySession.openChannel( "exec" ); myLog.debug( "executing SSH command " + aCommand ); ( (ChannelExec) channel ).setCommand( aCommand ); ByteArrayOutputStream osOut = new ByteArrayOutputStream(); ByteArrayOutputStream osErr = new ByteArrayOutputStream(); try { channel.setInputStream( null ); channel.setOutputStream(osOut, true ); ( (ChannelExec) channel ).setErrStream( osErr, true ); myLog.debug( "connecting the channel" ); channel.connect(); while ( true ) { if ( channel.isClosed() ) { break; } try { myLog.debug( "going to sleep 50 ms" ); Thread.sleep( 50 ); } catch ( Exception ee ) { } } } finally { String out = new String( osOut.toByteArray() ); if ( StringUtils.hasText( out ) ) { myLog.info( "Captured SSH out:\n" + out ); } String err = new String( osErr.toByteArray() ); if ( StringUtils.hasText( err ) ) { myLog.warn( "Captured SSH err:\n" + err ); } } } catch ( Throwable t ) { throw new SdiException( "SSH session not connected.", t, SdiException.EXIT_CODE_SSH_ERROR ); } finally { if ( channel != null ) { int exitStatus = channel.getExitStatus(); myLog.debug( "SSH exit status: " + exitStatus ); myLog.debug( "disconnecting the channel" ); channel.disconnect(); if ( exitStatus != 0 ) { throw new SdiException( "SSH not successful. SSH-Exitstatus: " + exitStatus, SdiException.EXIT_CODE_SSH_ERROR ); } } } }  void executeCmd( String aCommand ); void init(); void close(); }
SshExecutor { public void executeCmd( String aCommand ) throws SdiException { if ( mySession == null ) { throw new SdiException( "SSH session not initialized.", SdiException.EXIT_CODE_SSH_ERROR ); } if ( !mySession.isConnected() ) { throw new SdiException( "SSH session not connected.", SdiException.EXIT_CODE_SSH_ERROR ); } Channel channel = null; try { channel = mySession.openChannel( "exec" ); myLog.debug( "executing SSH command " + aCommand ); ( (ChannelExec) channel ).setCommand( aCommand ); ByteArrayOutputStream osOut = new ByteArrayOutputStream(); ByteArrayOutputStream osErr = new ByteArrayOutputStream(); try { channel.setInputStream( null ); channel.setOutputStream(osOut, true ); ( (ChannelExec) channel ).setErrStream( osErr, true ); myLog.debug( "connecting the channel" ); channel.connect(); while ( true ) { if ( channel.isClosed() ) { break; } try { myLog.debug( "going to sleep 50 ms" ); Thread.sleep( 50 ); } catch ( Exception ee ) { } } } finally { String out = new String( osOut.toByteArray() ); if ( StringUtils.hasText( out ) ) { myLog.info( "Captured SSH out:\n" + out ); } String err = new String( osErr.toByteArray() ); if ( StringUtils.hasText( err ) ) { myLog.warn( "Captured SSH err:\n" + err ); } } } catch ( Throwable t ) { throw new SdiException( "SSH session not connected.", t, SdiException.EXIT_CODE_SSH_ERROR ); } finally { if ( channel != null ) { int exitStatus = channel.getExitStatus(); myLog.debug( "SSH exit status: " + exitStatus ); myLog.debug( "disconnecting the channel" ); channel.disconnect(); if ( exitStatus != 0 ) { throw new SdiException( "SSH not successful. SSH-Exitstatus: " + exitStatus, SdiException.EXIT_CODE_SSH_ERROR ); } } } }  void executeCmd( String aCommand ); void init(); void close();  }