本文整理汇总了Java中com.jcraft.jsch.ChannelExec.isClosed方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelExec.isClosed方法的具体用法?Java ChannelExec.isClosed怎么用?Java ChannelExec.isClosed使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.ChannelExec
的用法示例。
在下文中一共展示了ChannelExec.isClosed方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: executeCommand
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
/**
* Convenience method to execute a command on the given remote session, using the given string
* as part of the error if it fails to run.
*
* @param roboRioSession The ssh session of the roboRio
* @param command The command to execute
* @param errorString The error string to put in the exception if an error occurs. The return
* code will be appended to the end
* @throws JSchException If an ssh error occurs
* @throws IOException Thrown if there is an io error, or if the command fails to run
*/
private void executeCommand(Session roboRioSession, String command, String errorString) throws JSchException, IOException {
// Extract the JRE
m_logger.debug("Running command " + command);
ChannelExec channel = (ChannelExec) roboRioSession.openChannel(EXEC_COMMAND);
channel.setCommand(command);
channel.connect();
int sleepCount = 0;
// Sleep for up to 10 seconds while we wait for the command to execute, checking every 100 milliseconds
do {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
m_logger.warn("Interrupted exception while waiting for command " + command + " to finish", e);
}
} while (!channel.isClosed() && sleepCount++ < 100);
int res = channel.getExitStatus();
if (res != SUCCESS) {
m_logger.debug("Error with command " + command);
throw new IOException(errorString + " " + res);
}
channel.disconnect();
}
示例2: streamOutput
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
private void streamOutput(ChannelExec channel, InputStream in)
throws IOException
{
byte[] buf = new byte[1024];
while (true)
{
while (in.available() > 1)
{
int bytesRead = in.read(buf);
if (bytesRead < 0 ) break;
this.out.write(buf);
}
if (channel.isClosed()) break;
sleepForOneSecondAndIgnoreInterrupt();
}
}
示例3: readOutput
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
private String readOutput(String command, ChannelExec channel, InputStream in) {
StringBuffer sb = new StringBuffer();
try {
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()) {
if (in.available() > 0) continue;
log.debug("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
} catch (IOException e) {
throw new RuntimeException("failed to exec " + command, e);
}
return sb.toString();
}
示例4: getExecRemoteCommandReturnInfo
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
/**
* Reads std and error remote output which are then wrapped inside {@link RemoteCommandReturnInfo}
*
* @param channelExec the channel where the command is sent for execution
* @param timeout the length of time in ms in which the method waits for a available byte(s) as a result of command
* @return {@link RemoteCommandReturnInfo}
*/
private RemoteCommandReturnInfo getExecRemoteCommandReturnInfo(final ChannelExec channelExec, final long timeout)
throws IOException, JSchException {
final String output = scrubberService.scrub(readExecRemoteOutput(channelExec, timeout));
LOGGER.debug("remote output = {}", output);
String errorOutput = null;
// wait for the channel to close before checking the exit status
final long startTime = System.currentTimeMillis();
while (!channelExec.isClosed()) {
if ((System.currentTimeMillis() - startTime) > CHANNEL_EXEC_CLOSE_TIMEOUT) {
errorOutput = MessageFormat.format("Wait for channel to close timeout! Timeout = {0} ms",
CHANNEL_EXEC_CLOSE_TIMEOUT);
LOGGER.error(errorOutput);
break;
}
}
LOGGER.debug("Channel exec exit status = {}", channelExec.getExitStatus());
if (channelExec.getExitStatus() != 0 && channelExec.getExitStatus() != -1) {
errorOutput = readExecRemoteOutput(channelExec, timeout);
LOGGER.debug("remote error output = {}", errorOutput);
}
return new RemoteCommandReturnInfo(channelExec.getExitStatus(), output, errorOutput);
}
示例5: executeSshCommand
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
/**
* Connect to a remote host via SSH and execute a command.
*
* @param host
* Host to connect to
* @param user
* User to login with
* @param password
* Password for the user
* @param command
* Command to execute
* @return The result of the command execution
*/
private Result executeSshCommand(final String host, final String user,
final String password, final String command) {
StringBuilder result = new StringBuilder();
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setUserInfo(createUserInfo(password));
session.connect(5000);
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setInputStream(null);
channel.setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
result.append(new String(tmp, 0, i));
}
if (channel.isClosed()) {
break;
}
}
channel.disconnect();
session.disconnect();
} catch (Exception jex) {
return createResult(Result.Status.ERROR, jex.getMessage());
}
return createResult(Result.Status.OK, result.toString());
}
示例6: run
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
@Override
public void run() {
TaskStage stage = TaskStage.FINISHED;
Map<String, String> commandResponse = new HashMap<>();
Session session = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(this.auth.userEmail, this.state.host,
this.state.port);
jsch.addIdentity("KeyPair",
EncryptionUtils.decrypt(this.auth.privateKey).getBytes(), null, null);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
for (String cmd : this.state.commands) {
// Create a channel for each command.
ChannelExec channel = (ChannelExec) session
.openChannel("exec");
channel.setCommand(cmd);
ByteArrayOutputStream out = new ByteArrayOutputStream();
channel.setOutputStream(out);
channel.setErrStream(out);
channel.connect();
while (!channel.isClosed()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
// add command output/err to response map.
commandResponse.put(cmd, out.toString());
int exitStatus = channel.getExitStatus();
channel.disconnect();
if (exitStatus != 0) {
// last command failed, sendSelfPatch the task.
stage = TaskStage.FAILED;
break;
}
}
// patch task state
sendSelfPatch(this.state, stage, commandResponse, null);
} catch (Throwable t) {
fail(this.state, t);
return;
} finally {
if (session != null) {
session.disconnect();
}
}
}
示例7: remoteExecute
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
public static String remoteExecute(String hostname, int port, String username, File keyFile,
final String passphrase, String command) throws JSchException, IOException {
Session session = createSession(hostname, port, username, keyFile, passphrase);
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
InputStream in = channel.getInputStream();
channel.setErrStream(System.err);
channel.connect();
ByteArrayOutputStream os = new ByteArrayOutputStream();
final byte[] tmp = new byte[8192];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 8192);
os.write(tmp, 0, i);
if (i < 0) {
break;
}
}
if (channel.isClosed()) {
break;
}
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
channel.disconnect();
session.disconnect();
return os.toString();
}
示例8: sendCommand
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
private void sendCommand(int idConnection, String command, CallbackContext callbackContext) throws JSchException, IOException, JSONException {
Session session = this.getSession(idConnection);
ChannelExec channel = (ChannelExec)session.openChannel("exec");
channel.setCommand(command);
ByteArrayOutputStream output = new ByteArrayOutputStream();
channel.setOutputStream(output);
channel.connect();
while (!channel.isClosed()) {
try {
Thread.sleep(25);
} catch (Exception e) {
callbackContext.error(e.toString());
channel.disconnect();
return;
}
}
JSONObject ret = new JSONObject();
ret.put("text", output.toString());
ret.put("status", channel.getExitStatus());
callbackContext.success(ret);
channel.disconnect();
}
示例9: sshShowConsole
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
public void sshShowConsole(String command) throws Exception {
JSch jsch = new JSch();
// connect session
Session session = jsch.getSession(userId, hostname, 22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.connect();
InputStream in = channel.getInputStream();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
channel.disconnect();
session.disconnect();
}
示例10: execute
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
/**
* No thread safe
*/
public void execute(String command, RemoteObserver observer) throws IOException, JSchException {
if (command == null) {
return;
}
this.command = command;
channel = (ChannelExec) session.openChannel("exec");
channel.setEnv("LANG", "UTF-8");
channel.setCommand(command);
// channel.setInputStream(null);
// channel.setOutputStream(System.out);
// FileOutputStream fos=new FileOutputStream("/tmp/stderr");
// ((ChannelExec)channel).setErrStream(fos);
Stream stream_out = new Stream(channel.getInputStream(), channel, false, observer);
Stream stream_err = new Stream(channel.getErrStream(), channel, true, observer);
channel.connect();
stream_out.start();
stream_err.start();
while (channel.isClosed() == false) {
try {
Thread.sleep(100);
} catch (Exception ee) {
}
}
observer.endRemoteExec(channel.getExitStatus());
channel.disconnect();
}
示例11: executeCommand
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
/**
*
* {@link org.apache.commons.vfs2.provider.sftp.SftpFileSystem#executeCommand(java.lang.String, java.lang.StringBuilder) }
*/
private int executeCommand( String command, StringBuilder output ) throws JSchException, IOException {
this.ensureSession();
ChannelExec channel = (ChannelExec) this.session.openChannel( "exec" );
channel.setCommand( command );
channel.setInputStream( (InputStream) null );
InputStreamReader stream = new InputStreamReader( channel.getInputStream() );
channel.setErrStream( System.err, true );
channel.connect();
char[] buffer = new char[128];
int read;
while ( ( read = stream.read( buffer, 0, buffer.length ) ) >= 0 ) {
output.append( buffer, 0, read );
}
stream.close();
while ( !channel.isClosed() ) {
try {
Thread.sleep( 100L );
} catch ( Exception exc ) {
log.logMinimal( "Warning: Error session closing. " + exc.getMessage() );
}
}
channel.disconnect();
return channel.getExitStatus();
}
示例12: streamOutput
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
private void streamOutput(ChannelExec channel, InputStream in)
throws IOException {
byte[] buf = new byte[1024];
while (true) {
while (in.available() > 1) {
int bytesRead = in.read(buf);
if (bytesRead < 0) break;
this.out.write(buf);
}
if (channel.isClosed()) break;
sleepForOneSecondAndIgnoreInterrupt();
}
}
示例13: readExecRemoteOutput
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
/**
* Read the remote output for the exec channel. This is the code provided on the jcraft site with a timeout added.
*
* @param channelExec the exec channel
* @param timeout the maximum period of time for reading the channel output
* @return the output from the channel
* @throws IOException for any issues encoutered when retrieving the input stream from the channel
*/
private String readExecRemoteOutput(ChannelExec channelExec, long timeout) throws IOException {
final BufferedInputStream bufIn = new BufferedInputStream(channelExec.getInputStream());
final StringBuilder outputBuilder = new StringBuilder();
final byte[] tmp = new byte[BYTE_CHUNK_SIZE];
final long startTime = System.currentTimeMillis();
final long readLoopSleepTime = Long.parseLong(ApplicationProperties.get(
JSCH_EXEC_READ_REMOTE_OUTPUT_LOOP_SLEEP_TIME.getPropertyName(), READ_LOOP_SLEEP_TIME_DEFAULT_VALUE));
while (true) {
// read the stream
while (bufIn.available() > 0) {
int i = bufIn.read(tmp, 0, BYTE_CHUNK_SIZE);
if (i < 0) {
break;
}
outputBuilder.append(new String(tmp, 0, i, StandardCharsets.UTF_8));
}
// check if the channel is closed
if (channelExec.isClosed()) {
// check for any more bytes on the input stream
sleep(readLoopSleepTime);
if (bufIn.available() > 0) {
continue;
}
LOGGER.debug("exit-status: {}", channelExec.getExitStatus());
break;
}
// check timeout
if ((System.currentTimeMillis() - startTime) > timeout) {
LOGGER.warn("Remote exec output reading timeout!");
break;
}
// If for some reason the channel is not getting closed, we should not hog CPU time with this loop hence
// we sleep for a while
sleep(readLoopSleepTime);
}
return outputBuilder.toString();
}
示例14: playGame
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
public void playGame(final Context con, final String gameName) {
Thread thread = new Thread() {
@Override
public void run() {
try {
// SSH Channel
ChannelExec channel = (ChannelExec) session.openChannel("exec");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
channel.setOutputStream(baos);
// Execute command
channel.setCommand(buildPlayGameCommand(con, gameName));
channel.connect();
InputStream in = channel.getInputStream();
byte[] tmp = new byte[1024];
String tmpString = "";
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) break;
tmpString = tmpString+new String(tmp, 0, i);
System.out.print(new String(tmp, 0, i));
if(tmpString.contains("input connection")){
SSHBus.post(new GameLoadingEvent(true));
} else {
SSHBus.post(new GameLoadingEvent(tmpString));
}
}
if (channel.isClosed()) {
if (in.available() > 0) continue;
System.out.println("exit-status: " + channel.getExitStatus());
SSHBus.post(new GameLoadingEvent(true));
break;
}
try {
Thread.sleep(500);
} catch (Exception ee) {
}
}
channel.disconnect();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
};
thread.start();
}
示例15: doesLimelightExist
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
public void doesLimelightExist(final Context con, final Device device) {
this.device = device;
Thread thread = new Thread() {
@Override
public void run() {
try {
// SSH Channel
ChannelExec channel = (ChannelExec) session.openChannel("exec");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
channel.setOutputStream(baos);
// Execute command
channel.setCommand("[ -f "+ device.directory +"/limelight.jar ] && echo \"yes\" || echo \"no\"");
channel.connect();
InputStream in = channel.getInputStream();
byte[] tmp = new byte[1024];
String tmpString = "";
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) break;
tmpString = tmpString+new String(tmp, 0, i);
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
if (in.available() > 0) continue;
System.out.println("exit-status: " + channel.getExitStatus());
if(tmpString.contains("yes")){
dispatchEventBus(con,new LimelightExistsEvent(true));
} else {
dispatchEventBus(con,new LimelightExistsEvent(false));
}
break;
}
try {
Thread.sleep(500);
} catch (Exception ee) {
}
}
channel.disconnect();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
};
thread.start();
}