本文整理汇总了Java中com.jcraft.jsch.ChannelExec.setPty方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelExec.setPty方法的具体用法?Java ChannelExec.setPty怎么用?Java ChannelExec.setPty使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.ChannelExec
的用法示例。
在下文中一共展示了ChannelExec.setPty方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: exec
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
/**
* Connect via ssh and execute a command (e.g. execute "ls -al" in remote host)
*
* Reference: http://www.jcraft.com/jsch/examples/Exec.java.html
*/
public String exec(String command) {
// Open ssh session
try {
channel = connect("exec", command);
ChannelExec chexec = (ChannelExec) channel;
chexec.setInputStream(null);
chexec.setPty(true); // Allocate pseudo-tty (same as "ssh -t")
// We don't need these
//chexec.setErrStream(System.err);
//chexec.setOutputStream(System.out);
// Connect channel
chexec.connect();
// Read input
String result = readChannel(true);
disconnect(false); // Disconnect and get exit code
return result;
} catch (Exception e) {
if (debug) e.printStackTrace();
return null;
}
}
示例3: execute
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
/**
*
* @param command SSH command to execute
* @return the exit code
*/
public int execute(
String command,
boolean waitForCompletion ) {
try {
this.command = command;
execChannel = (ChannelExec) session.openChannel("exec");
execChannel.setCommand(command);
execChannel.setInputStream(null);
execChannel.setPty(true); // Allocate a Pseudo-Terminal. Thus it supports login sessions. (eg. /bin/bash -l)
execChannel.connect(); // there is a bug in the other method channel.connect( TIMEOUT );
stdoutThread = new StreamReader(execChannel.getInputStream(), execChannel, "STDOUT");
stderrThread = new StreamReader(execChannel.getErrStream(), execChannel, "STDERR");
stdoutThread.start();
stderrThread.start();
if (waitForCompletion) {
stdoutThread.getContent();
stderrThread.getContent();
return execChannel.getExitStatus();
}
} catch (Exception e) {
throw new JschSshClientException(e.getMessage(), e);
} finally {
if (waitForCompletion && execChannel != null) {
execChannel.disconnect();
}
}
return -1;
}
示例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: 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;
}
示例6: ssh
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
public Process ssh(String cmd) throws JSchException, IOException {
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(cmd);
channel.setPty(true);
channel.connect();
return new SSHProcess(channel);
}