本文整理汇总了Java中com.jcraft.jsch.ChannelExec.setOutputStream方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelExec.setOutputStream方法的具体用法?Java ChannelExec.setOutputStream怎么用?Java ChannelExec.setOutputStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.ChannelExec
的用法示例。
在下文中一共展示了ChannelExec.setOutputStream方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createTCPProxy
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
private void createTCPProxy() {
try {
final String cleanCommand = String.format("sudo docker ps -a | grep 'hours ago' | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm",
containerName,
this.targetHost,
this.targetPort,
sourceIPs,
ImageRegistry.get().tcpProxy());
LOGGER.info("Establishing SSH shell");
ssh = SshUtil.getInstance()
.createSshSession(TestConfiguration.proxyHostUsername(), getProxyHost());
ssh.connect();
LOGGER.debug("Connected to ssh console");
final ChannelExec cleanChannel = (ChannelExec) ssh.openChannel("exec");
cleanChannel.setPty(true);
cleanChannel.setCommand(cleanCommand);
cleanChannel.setInputStream(null);
cleanChannel.setOutputStream(System.err);
LOGGER.debug("Executing command: '{}'", cleanCommand);
cleanChannel.connect();
cleanChannel.disconnect();
final String command = String.format("sudo docker run -it --name %s --net=host --rm -e AB_OFF=true -e TARGET_HOST=%s -e TARGET_PORT=%s -e TARGET_VIA=%s %s",
containerName,
this.targetHost,
this.targetPort,
sourceIPs,
ImageRegistry.get().tcpProxy());
LOGGER.info("Establishing SSH shell");
ssh = SshUtil.getInstance()
.createSshSession(TestConfiguration.proxyHostUsername(), getProxyHost());
ssh.connect();
LOGGER.debug("Connected to ssh console");
final ChannelExec channel = (ChannelExec) ssh.openChannel("exec");
channel.setPty(true);
channel.setCommand(command);
channel.setInputStream(null);
channel.setOutputStream(System.err);
LOGGER.debug("Executing command: '{}'", command);
channel.connect();
final LineIterator li = IOUtils.lineIterator(new InputStreamReader(channel.getInputStream()));
final Pattern portLine = Pattern.compile(".*Listening on port ([0-9]*).*$");
listenPort = 0;
while (li.hasNext()) {
final String line = li.next();
LOGGER.trace("Shell line: {}", line);
final Matcher m = portLine.matcher(line);
if (m.matches()) {
listenPort = Integer.parseInt(m.group(1));
LOGGER.info("Connection listening on port {}", listenPort);
break;
}
}
channel.disconnect();
} catch (final JSchException | IOException e) {
LOGGER.debug("Error in creating SSH connection to proxy host", e);
throw new IllegalStateException("Cannot open SSH connection", e);
}
}
示例2: runJavaApp
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
/**
* Runs that java app with the specified command and then takes the console output from target to host machine
*
* @param path
* @param cmd
* @throws IOException
*/
private void runJavaApp(@NotNull final String path, @NotNull final String cmd) throws IOException, RuntimeConfigurationException {
consoleView.print(NEW_LINE + EmbeddedLinuxJVMBundle.getString("pi.deployment.build") + NEW_LINE + NEW_LINE, ConsoleViewContentType.SYSTEM_OUTPUT);
Session session = connect(ssh.get());
consoleView.setSession(session);
try {
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setOutputStream(System.out, true);
channelExec.setErrStream(System.err, true);
List<String> commands = Arrays.asList(
String.format("%s kill -9 $(ps -efww | grep \"%s\"| grep -v grep | tr -s \" \"| cut -d\" \" -f2)", params.isRunAsRoot() ? "sudo" : "", params.getMainclass()),
String.format("cd %s", path),
String.format("tar -xvf %s.tar", consoleView.getProject().getName()),
"rm *.tar",
cmd);
for (String command : commands) {
consoleView.print(EmbeddedLinuxJVMBundle.getString("pi.deployment.command") + command + NEW_LINE, ConsoleViewContentType.SYSTEM_OUTPUT);
}
channelExec.setCommand(LinuxCommand.builder().commands(commands).build().toString());
channelExec.connect();
checkOnProcess(channelExec);
} catch (JSchException e) {
setErrorOnUI(e.getMessage());
}
}
示例3: exec
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
@Override
public int exec() throws IOException {
try {
final ChannelExec channel = ChannelExec.class.cast(
this.session.openChannel("exec")
);
channel.setErrStream(this.stderr, false);
channel.setOutputStream(this.stdout, false);
channel.setInputStream(this.stdin, false);
channel.setCommand(this.command);
channel.connect();
Logger.info(this, "$ %s", this.command);
return this.exec(channel);
} catch (final JSchException ex) {
throw new IOException(ex);
} finally {
this.session.disconnect();
}
}
示例4: executeCommand
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
public int executeCommand(boolean setPty, String command, CommandResultConsumer consumer) {
Session ssh = SshUtil.getInstance().createSshSession(username, hostname);
int resultCode = 255;
try {
ssh.connect();
LOGGER.debug("Connected to ssh console");
ChannelExec channel = (ChannelExec) ssh.openChannel("exec");
channel.setPty(setPty);
channel.setCommand(command);
channel.setInputStream(null);
channel.setOutputStream(System.err);
LOGGER.debug("Executing command: '{}'", command);
channel.connect();
try {
if (consumer != null) {
consumer.consume(channel.getInputStream());
}
} catch (IOException ex) {
throw new RuntimeException("Unable to read console output", ex);
} finally {
channel.disconnect();
}
resultCode = channel.getExitStatus();
} catch (JSchException x) {
LOGGER.error("Error SSH to " + username + "@" + hostname, x);
} finally {
ssh.disconnect();
}
return resultCode;
}
示例5: 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();
}
}
}
示例6: sessionConnectGenerateChannel
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
/**
* Session connect generate channel.
*
* @param session
* the session
* @return the channel
* @throws JSchException
* the j sch exception
*/
public Channel sessionConnectGenerateChannel(Session session)
throws JSchException {
// set timeout
session.connect(sshMeta.getSshConnectionTimeoutMillis());
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(sshMeta.getCommandLine());
// if run as super user, assuming the input stream expecting a password
if (sshMeta.isRunAsSuperUser()) {
try {
channel.setInputStream(null, true);
OutputStream out = channel.getOutputStream();
channel.setOutputStream(System.out, true);
channel.setExtOutputStream(System.err, true);
channel.setPty(true);
channel.connect();
out.write((sshMeta.getPassword()+"\n").getBytes());
out.flush();
} catch (IOException e) {
logger.error("error in sessionConnectGenerateChannel for super user", e);
}
} else {
channel.setInputStream(null);
channel.connect();
}
return channel;
}
示例7: 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();
}
示例8: 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();
}
示例9: 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();
}
示例10: getGames
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
public void getGames(final Context con) {
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("cd " + device.directory + "; java -jar limelight.jar list " + device.hostIP);
channel.connect();
boolean gamesIncoming = false;
ArrayList<String> gameNames = new ArrayList<>();
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;
String tmpString = new String(tmp, 0, i);
System.out.println(tmpString);
if(gamesIncoming){
gameNames.add(tmpString);
}
if(tmpString.contains("Search apps")){
gamesIncoming = true;
}
}
if (channel.isClosed()) {
if (in.available() > 0) continue;
System.out.println("exit-status: " + channel.getExitStatus());
dispatchEventBus(con, new GotGamesEvent(gameNames));
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
channel.disconnect();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
};
thread.start();
}