本文整理匯總了Java中com.jcraft.jsch.ChannelExec.getInputStream方法的典型用法代碼示例。如果您正苦於以下問題:Java ChannelExec.getInputStream方法的具體用法?Java ChannelExec.getInputStream怎麽用?Java ChannelExec.getInputStream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.jcraft.jsch.ChannelExec
的用法示例。
在下文中一共展示了ChannelExec.getInputStream方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: executeSSH
import com.jcraft.jsch.ChannelExec; //導入方法依賴的package包/類
private boolean executeSSH(){
//get deployment descriptor, instead of this hard coded.
// or execute a script on the target machine which download artifact from nexus
String command ="nohup java -jar -Dserver.port=8091 ./work/codebox/chapter6/chapter6.search/target/search-1.0.jar &";
try{
System.out.println("Executing "+ command);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session=jsch.getSession("rajeshrv", "localhost", 22);
session.setPassword("rajeshrv");
session.setConfig(config);
session.connect();
System.out.println("Connected");
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand(command);
channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
int index = 0;
while ((line = reader.readLine()) != null) {
System.out.println(++index + " : " + line);
}
channelExec.disconnect();
session.disconnect();
System.out.println("Done!");
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
示例2: upload
import com.jcraft.jsch.ChannelExec; //導入方法依賴的package包/類
@Override
public void upload(Path file, String remotePath)
{
Session session = null;
try {
session = createSession();
LOGGER.info("Uploading {} onto {}@{}:{}:{}", file, session.getUserName(), session.getHost(), session.getPort(), remotePath);
ChannelExec channel = (ChannelExec) session.openChannel("exec");
String command = "scp -t " + remotePath;
channel.setCommand(command);
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
sendSCPFile(file, channel, in, out);
}
catch (JSchException | IOException exception) {
Throwables.propagate(exception);
}
finally {
if (session != null) { session.disconnect(); }
}
}
示例3: testExec
import com.jcraft.jsch.ChannelExec; //導入方法依賴的package包/類
@Test(timeout = 5000)
public void testExec(TestContext context) throws Exception {
startShell();
Session session = createSession("paulo", "secret", false);
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("the-command arg1 arg2");
channel.connect();
InputStream in = channel.getInputStream();
StringBuilder input = new StringBuilder();
while (!input.toString().equals("the_output")) {
int a = in.read();
if (a == -1) {
break;
}
input.append((char)a);
}
OutputStream out = channel.getOutputStream();
out.write("the_input".getBytes());
out.flush();
while (channel.isConnected()) {
Thread.sleep(1);
}
assertEquals(2, channel.getExitStatus());
session.disconnect();
}
示例4: exec
import com.jcraft.jsch.ChannelExec; //導入方法依賴的package包/類
@SuppressWarnings("resource")
public String exec(String command, InputStream opt) throws JSchException, IOException {
ChannelExec channel = (ChannelExec) getSession().openChannel("exec");
try {
channel.setCommand(command);
channel.setInputStream(opt);
InputStream in = channel.getInputStream();
InputStream err = channel.getErrStream();
channel.connect();
Scanner s = new Scanner(err).useDelimiter("\\A");
error = s.hasNext() ? s.next() : null;
s = new Scanner(in).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
} finally {
channel.disconnect();
}
}
示例5: scpPut
import com.jcraft.jsch.ChannelExec; //導入方法依賴的package包/類
@Override
public OutputStream scpPut(String remoteFilePath, long length, String mode) throws Throwable {
open();
if (!Pattern.matches("\\d{4}", mode)) {
throw new IllegalArgumentException("Invalid file mode: " + mode);
}
String dir = PathUtil.getParentDirectory(remoteFilePath);
String fileName = PathUtil.getFileName(remoteFilePath);
// exec 'scp -t -d remoteTargetDirectory' remotely
String command = "scp -t -d \"" + dir + "\"";
final ChannelExec channel = (ChannelExec) _jschSession.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out = channel.getOutputStream();
final InputStream in = channel.getInputStream();
channel.connect();
return new ScpOutputStream(channel, in, out, new FileInfo(mode, length, fileName));
}
示例6: execCommand
import com.jcraft.jsch.ChannelExec; //導入方法依賴的package包/類
private static String execCommand(Session session, TextStream out, String command) throws JSchException, IOException {
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setInputStream(null);
channel.setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int count = 0;
String line = br.readLine();
StringWriter sw = new StringWriter();
while (line != null) {
String info = String.format("%d: %s\n", count++, line);
sw.append(info);
out.appendText(info);
line = br.readLine();
}
session.disconnect();
return sw.toString();
}
示例7: startCommand
import com.jcraft.jsch.ChannelExec; //導入方法依賴的package包/類
/**
* Starts the specified command (executable + params) on the specified ExecutionEnvironment.
*
* @param env - environment to execute in
* @param command - executable + params to execute
* @param params - (optional) channel params. May be null.
* @return I/O streams and opened execution JSch channel. Never returns NULL.
* @throws IOException - if unable to aquire an execution channel
* @throws JSchException - if JSch exception occured
* @throws InterruptedException - if the thread was interrupted
*/
public static ChannelStreams startCommand(final ExecutionEnvironment env, final String command, final ChannelParams params)
throws IOException, JSchException, InterruptedException {
JSchWorker<ChannelStreams> worker = new JSchWorker<ChannelStreams>() {
@Override
public ChannelStreams call() throws JSchException, IOException, InterruptedException {
ChannelExec echannel = (ChannelExec) ConnectionManagerAccessor.getDefault().openAndAcquireChannel(env, "exec", true); // NOI18N
if (echannel == null) {
throw new IOException("Cannot open exec channel on " + env + " for " + command); // NOI18N
}
echannel.setCommand(command);
echannel.setXForwarding(params == null ? false : params.x11forward);
InputStream is = echannel.getInputStream();
InputStream es = echannel.getErrStream();
OutputStream os = new ProtectedOutputStream(echannel, echannel.getOutputStream());
Authentication auth = Authentication.getFor(env);
echannel.connect(auth.getTimeout() * 1000);
return new ChannelStreams(echannel, is, es, os);
}
@Override
public String toString() {
return command;
}
};
return start(worker, env, 2);
}
示例8: 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;
}
示例9: executeCmd
import com.jcraft.jsch.ChannelExec; //導入方法依賴的package包/類
public StringPair executeCmd(String USERNAME,String PASSWORD,String host,String command)
{
StringPair result = new StringPair();
try
{
JSch jsch = new JSch();
Session session = jsch.getSession(USERNAME, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(PASSWORD);
session.connect();
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand(command);
channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//while((line = reader.readLine()) != null)
result.hostName = reader.readLine();
result.ip=reader.readLine();
reader.close();
int exitStatus = channelExec.getExitStatus();
channelExec.disconnect();
session.disconnect();
if(exitStatus < 0){
System.out.println("Done, but exit status not set!");
}
else if(exitStatus > 0){
System.out.println("Done, but with error!");
}
else{
System.out.println("Done!");
}
return result;
}
catch(Exception e)
{
System.err.println("Error: " + e);
return result;
}
}
示例10: execCmd
import com.jcraft.jsch.ChannelExec; //導入方法依賴的package包/類
private SSHCommand execCmd(SSHCommand cmd) throws JSchException, IOException {
Log.d("-------", "COMMAND: "+cmd.getCommand());
ChannelExec channel = (ChannelExec) currSSHSess.openChannel("exec");
channel.setCommand(cmd.getCommand());
channel.setInputStream(null);
final InputStream
in = channel.getInputStream(),
err = channel.getErrStream();
channel.connect();
final BufferedReader
readerIn = new BufferedReader(new InputStreamReader(in)),
readerEr = new BufferedReader(new InputStreamReader(err));
final StringBuilder
errStr = new StringBuilder(),
inStr = new StringBuilder();
String line;
while ((line = readerIn.readLine()) != null) {
inStr.append(line).append('\n');
progress(line + "\n");
}
while ((line = readerEr.readLine()) != null) {
errStr.append(line).append('\n');
progress(line + "\n");
}
channel.disconnect();
cmd.setCmdOut(inStr.toString());
cmd.setErrorOut(errStr.toString());
return cmd;
}
示例11: 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());
}
示例12: execute
import com.jcraft.jsch.ChannelExec; //導入方法依賴的package包/類
void execute(Session sshSession) throws SshException
{
InputStream in = null;
ChannelExec channel = null;
if (command == null)
{
throw new IllegalStateException("command attribute of " +
this.getClass().getName() + " can't be null.") ;
}
try
{
try
{
channel = this.connectToChannel(sshSession,command);
in = channel.getInputStream();
streamOutput(channel,in);
}
finally
{
if (in != null)
in.close();
if (channel != null)
channel.disconnect();
}
}
catch (Exception e)
{
throw new SshException(e);
}
}
示例13: exec
import com.jcraft.jsch.ChannelExec; //導入方法依賴的package包/類
public void exec(String cmdToRun) throws IOException, JSchException, InterruptedException {
ChannelExec channelExec = (ChannelExec) remoteClient.getSession().openChannel("exec");
channelExec.setCommand(cmdToRun);
channelExec.setInputStream(null);
InputStream [] inputs = new InputStream [] {channelExec.getInputStream(), channelExec.getExtInputStream(), channelExec.getErrStream()};
channelExec.connect();
final long now = System.currentTimeMillis();
boolean gotResult = false;
while (!gotResult && System.currentTimeMillis() - now < 1000) {
for (int i = 0; i < inputs.length; ++i) {
InputStream input = inputs[i];
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
StringBuilder builder = new StringBuilder();
if (reader.ready()) {
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
gotResult = true;
System.out.println("Got reply from input#" + i + ":\n" + builder.toString());
break;
}
}
Thread.sleep(50);
}
int exitStatus = channelExec.getExitStatus();
channelExec.disconnect();
System.out.println("Executing command exit status: " + exitStatus);
}
示例14: write
import com.jcraft.jsch.ChannelExec; //導入方法依賴的package包/類
private void write(ChannelExec c, String name, InputStream data, ScpConfiguration cfg) throws IOException {
OutputStream os = c.getOutputStream();
InputStream is = c.getInputStream();
try {
writeFile(name, data, os, is, cfg);
} finally {
IOHelper.close(is, os);
}
}
示例15: execute
import com.jcraft.jsch.ChannelExec; //導入方法依賴的package包/類
public int execute(String[] commands, OutputStream stdout, OutputStream stderr) {
try {
ChannelExec ch = (ChannelExec) getSession().openChannel(CHANNEL_EXEC);
String command = buildCommand(commands);
ch.setCommand(command);
try {
InputStream out = ch.getInputStream();
InputStream err = ch.getErrStream();
ch.connect();
if (stdout != null) {
ByteStreams.copy(out, stdout);
}
if (stderr != null) {
ByteStreams.copy(err, stderr);
}
int exitCode;
while ((exitCode = ch.getExitStatus()) == -1) {
Thread.sleep(100);
}
return exitCode;
} finally {
ch.disconnect();
}
} catch (Exception e) {
throw new SshException(e);
}
}