本文整理汇总了Java中com.jcraft.jsch.Channel.getExitStatus方法的典型用法代码示例。如果您正苦于以下问题:Java Channel.getExitStatus方法的具体用法?Java Channel.getExitStatus怎么用?Java Channel.getExitStatus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.Channel
的用法示例。
在下文中一共展示了Channel.getExitStatus方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeCommandNoResponse
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
public static void executeCommandNoResponse( Session sessionObj, String command )
throws JSchException, IOException
{
logger.debug( "Starting to execute command [" + maskedPasswordString( command ) + "]" );
if ( sessionObj != null && command != null && !"".equals( command ) )
{
Channel channel = null;
try
{
channel = sessionObj.openChannel( "exec" );
( (ChannelExec) channel ).setCommand( command );
channel.setInputStream( null );
( (ChannelExec) channel ).setErrStream( System.err );
channel.getInputStream();
channel.connect();
/* We do not care about whether the command succeeds or not */
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 [" + maskedPasswordString( command ) + "]" );
}
}
}
示例2: executeCommandNoResponse
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
public static void executeCommandNoResponse( Session sessionObj, String command )
throws JSchException, IOException
{
logger.debug( "Starting to execute command [" + command + "]" );
if ( sessionObj != null && command != null && !"".equals( command ) )
{
Channel channel = null;
try
{
channel = sessionObj.openChannel( "exec" );
( (ChannelExec) channel ).setCommand( command );
channel.setInputStream( null );
( (ChannelExec) channel ).setErrStream( System.err );
channel.getInputStream();
channel.connect();
/* We do not care about whether the command succeeds or not */
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 + "]" );
}
}
}
示例3: exec
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@SneakyThrows
public static int exec(@Nonnull String host,
int port,
@Nonnull String username,
@Nonnull String password,
@Nonnull String cmd) {
JSch jSch = new JSch();
Session session = jSch.getSession(username, host, port);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(cmd);
((ChannelExec) channel).setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream inputStream = channel.getInputStream();
channel.connect();
log.info("# successful connect to server [{}:{}]", host, port);
log.info("# exec cmd [{}]", cmd);
StringBuilder sb = new StringBuilder();
byte[] bytes = new byte[1024];
int exitStatus;
while (true) {
while (inputStream.available() > 0) {
int i = inputStream.read(bytes, 0, 1024);
if (i < 0) {
break;
}
sb.append(new String(bytes, 0, i, StandardCharsets.UTF_8));
}
if (channel.isClosed()) {
if (inputStream.available() > 0) {
continue;
}
exitStatus = channel.getExitStatus();
break;
}
Thread.sleep(1000);
}
if (StringUtils.isNotEmpty(sb)) {
log.info("# cmd-rs \n" + sb);
}
channel.disconnect();
session.disconnect();
log.info("# successful disconnect to server [{}:{}]", host, port);
return exitStatus;
}
示例4: executeCommand
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
private void executeCommand(String command) throws JSchException, IOException, InterruptedException {
connectIfNot();
Channel channel = session.openChannel("exec");
try {
((ChannelExec) channel).setCommand(command);
((ChannelExec) channel).setErrStream(System.err);
((ChannelExec) channel).setPty(true);
((ChannelExec) channel).setPtyType("vt100");
channel.setInputStream(null);
channel.setOutputStream(System.out);
InputStream in = channel.getInputStream();
logger.info("ssh exec command => {}", command);
channel.connect();
byte[] buffer = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(buffer, 0, 1024);
if (i < 0) break;
messageLogger.info(new String(buffer, 0, i, Charsets.UTF_8));
}
if (channel.isClosed()) {
logger.info("ssh exec exit status => " + channel.getExitStatus());
break;
}
Thread.sleep(1000);
}
if (channel.getExitStatus() != 0) {
throw new JSchException("failed to run command, command=" + command);
}
} finally {
channel.disconnect();
}
}
示例5: runCommand
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
* @param positiveExitCodes The exit codes to consider the command a success. This will normally be only 0, but in case of
* f.ex. 'diff' 1 is also ok.
*/
public void runCommand(String server, String command, int commandTimeout, String quotes, int[] positiveExitCodes)
throws Exception {
RemoteCommand remoteCommand = new RemoteCommand(server, command, quotes);
log.info("Running JSch command: " + remoteCommand);
BufferedReader inReader = null;
BufferedReader errReader = null;
JSch jsch = new JSch();
Session session = jsch.getSession("test", TestEnvironment.DEPLOYMENT_SERVER);
setupJSchIdentity(jsch);
session.setConfig("StrictHostKeyChecking", "no");
long startTime = System.currentTimeMillis();
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(remoteCommand.commandAsString());
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(null);
InputStream in = channel.getInputStream();
InputStream err = ((ChannelExec) channel).getErrStream();
channel.connect(1000);
log.debug("Channel connected, command: " + remoteCommand.commandAsString());
inReader = new BufferedReader(new InputStreamReader(in));
errReader = new BufferedReader(new InputStreamReader(err));
int numberOfSecondsWaiting = 0;
int maxNumberOfSecondsToWait = 60*10;
while (true) {
if (channel.isClosed()) {
log.info("Command finished in "
+ (System.currentTimeMillis() - startTime) / 1000
+ " seconds. " + "Exit code was "
+ channel.getExitStatus());
boolean errorOccured = true;
for (int positiveExit:positiveExitCodes) {
if (positiveExit == channel.getExitStatus()) {
errorOccured = false;
break;
}
}
if (errorOccured || err.available() > 0) {
throw new RuntimeException("Failed to run command, exit code " + channel.getExitStatus());
}
break;
} else if ( numberOfSecondsWaiting > maxNumberOfSecondsToWait) {
log.info("Command not finished after " + maxNumberOfSecondsToWait + " seconds. " +
"Forcing disconnect.");
channel.disconnect();
break;
}
try {
Thread.sleep(1000);
String s;
while ((s = inReader.readLine()) != null) {
if (!s.trim().isEmpty()) log.debug("ssh: " + s);
}
while ((s = errReader.readLine()) != null) {
if (!s.trim().isEmpty()) log.warn("ssh error: " + s);
}
} catch (InterruptedException ie) {
}
}
}
开发者ID:netarchivesuite,项目名称:netarchivesuite-svngit-migration,代码行数:74,代码来源:TestEnvironmentManager.java
示例6: executeCommand
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
* It executes the command and returns the response back to the calling function. This function will expect Session
* object and command string as parameters .
*
* @param sessionObj
* @param command
* @return
* @throws JSchException
* @throws IOException
*/
public static String executeCommand( Session sessionObj, String command )
throws JSchException, IOException
{
StringBuilder builder = null;
logger.debug( "Starting to execute command [" + maskedPasswordString( 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 [" + maskedPasswordString( command ) + "]" );
}
return builder.toString();
}
return null;
}
示例7: executeCommand
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
* It executes the command and returns the response back to the calling function. This function will expect Session
* object and command string as parameters .
*
* @param sessionObj
* @param command
* @return
* @throws JSchException
* @throws IOException
*/
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;
}
示例8: executeCommand
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
* It executes the command and returns the response back to the calling function. This function will expect Session
* object and command string as parameters .
*
* @param sessionObj
* @param command
* @return
* @throws JSchException
* @throws IOException
*/
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;
}
示例9: executeCommand
import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
private CommandResult executeCommand(String command) throws JSchException, IOException {
interactor.onOutput("Executing shell command '{}'...", command);
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
ByteArrayOutputStream err = new ByteArrayOutputStream();
((ChannelExec) channel).setErrStream(err);
InputStream in = channel.getInputStream();
channel.connect();
StringBuilder sb = new StringBuilder();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
sb.append(new String(tmp, 0, i));
}
if (channel.isClosed())
break;
try {
Thread.sleep(500);
} catch (Exception ee) {
logger.warn(ee.getMessage(), ee);
}
}
channel.disconnect();
interactor.onOutput("Execution of command '{}' terminated with exit status {}.",
command, channel.getExitStatus());
return new CommandResult(sb.toString().trim(), err.toString().trim(), channel.getExitStatus());
}