本文整理汇总了Java中com.jcraft.jsch.ChannelExec.getOutputStream方法的典型用法代码示例。如果您正苦于以下问题:Java ChannelExec.getOutputStream方法的具体用法?Java ChannelExec.getOutputStream怎么用?Java ChannelExec.getOutputStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.ChannelExec
的用法示例。
在下文中一共展示了ChannelExec.getOutputStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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(); }
}
}
示例2: 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();
}
示例3: 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));
}
示例4: 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);
}
}
示例5: 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);
}
示例6: 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);
}
}
示例7: 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;
}
示例8: JSchCliProcess
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
JSchCliProcess(Session session, ChannelExec channel)
throws IOException
{
super(channel.getInputStream(), channel.getErrStream(), channel.getOutputStream());
this.session = session;
this.channel = channel;
}
示例9: create
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
@Override
public ExecutableChannel create() throws Exception
{
// http://stackoverflow.com/questions/6265278/whats-the-exact-differences-between-jsch-channelexec-and-channelshell
this.session = acquire(SshConnection.builder().from(JschSshClient.this.connection).sessionTimeout(0).build());
executor = (ChannelExec) session.openChannel("exec");
executor.setCommand(command);
executor.setErrStream(new ByteArrayOutputStream());
InputStream inputStream = executor.getInputStream();
InputStream errStream = executor.getErrStream();
OutputStream outStream = executor.getOutputStream();
executor.connect();
return new ExecutableChannel(outStream, inputStream, errStream, new Supplier<Integer>()
{
@Override
public Integer get()
{
int exitStatus = executor.getExitStatus();
return exitStatus != -1 ? exitStatus : null;
}
}, new Closeable()
{
@Override
public void close() throws IOException
{
clear();
}
});
}
示例10: execute
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
void execute(Session sshSession) throws SshException
{
if (this.scpFile == null)
{
throw new SshException("scpFile property has not been set and is null" ,
new NullPointerException());
}
InputStream in = null;
OutputStream out = null;
InputStream fis = null;
ChannelExec channel = null;
try
{
try
{
final String cmd = SCP_UPLOAD_COMMAND + this.scpFile.getRemotePath();
channel = this.connectToChannel(sshSession,cmd);
fis = new FileInputStream(scpFile.getLocalFile());
in = channel.getInputStream();
out = channel.getOutputStream();
if (checkAck(in) != 0)
{
throw new SshException("Acknowledgement check failed: Initializing "
+ "session returned a status code other than 0");
}
sendFileSizeAndRemotePath(scpFile, out);
if (checkAck(in) != 0)
{
throw new SshException("Scp upload failed. Reason: sending filesize "
+ "and filename returned a status code other than 0.");
}
sendPayloadToServer(out, fis);
sendEOFToServer(out);
if (checkAck(in) != 0)
{
throw new SshException("Scp upload failed. Reason: sending the "
+ " file payload resulted a status code other than 0");
}
}
finally
{
if (out != null)
out.close();
if (in != null)
in.close();
if (fis != null)
fis.close();
if (channel != null)
channel.disconnect();
}
}
catch (Exception e)
{
throw new SshException(e);
}
}
示例11: execute
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
void execute(Session sshSession) throws SshException
{
InputStream in = null;
OutputStream out = null;
FileOutputStream fos = null;
ChannelExec channel = null;
try
{
try
{
long fileSize;
String cmd = SCP_DOWNLOAD_COMMAND + this.scpFile.getRemotePath();
channel = connectToChannel(sshSession,cmd);
fos = new FileOutputStream(scpFile.getLocalFile());
in = channel.getInputStream();
out = channel.getOutputStream();
sendAck(out);
fileSize = getFileSizeFromStream(in);
skipFileName(in);
sendAck(out);
writePayloadToFile(in,out,fos,fileSize);
}
finally
{
if (out != null)
out.close();
if (in != null)
in.close();
if (fos != null)
fos.close();
if (channel != null)
channel.disconnect();
}
}
catch (Exception e)
{
throw new SshException(e);
}
}
示例12: scpGet
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
@Override
public InputStream scpGet(String remoteFilePath) throws Throwable {
open();
// exec 'scp -f remoteFilePath' remotely
String command = "scp -f " + remoteFilePath;
ChannelExec channel = (ChannelExec) _jschSession.openChannel("exec");
channel.setCommand(command);
// get I/O streams for remote scp
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
return new ScpInputStream(channel, in, out);
}
示例13: JschScpClient
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
JschScpClient(JschConnection connection, ChannelExec channel, String remoteBaseDir, String encoding,
Integer dirMode, Integer fileMode, boolean compress, boolean preserve, boolean verbose) throws Throwable {
super(connection, channel, remoteBaseDir, encoding, dirMode, fileMode, compress, preserve, verbose);
_cin = new BufferedInputStream(channel.getInputStream(), 8192);
_cout = new BufferedOutputStream(channel.getOutputStream());
}
示例14: scpToRemote
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
public static void scpToRemote(String host, File lfile, String remotePath, TextStream tout) throws JSchException, IOException {
Session session = getDefaultSession(host);
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("scp -t "+remotePath);
// get I/O streams for remote scp
OutputStream out=channel.getOutputStream();
InputStream in=channel.getInputStream();
channel.connect();
if(checkAck(in)!=0){
System.exit(0);
}
// send "C0644 filesize filename", where filename should not include '/'
long filesize=lfile.length();
String command="C0644 "+filesize+" " + lfile.getName();
command+="\n";
out.write(command.getBytes()); out.flush();
int ok = checkAck(in);
if(ok != 0){
throw new JSchException("checkAck failure, "+ok);
}
tout.appendText(String.format("Transferring %d bytes from: %s\n", filesize, lfile.getAbsolutePath()));
// send a content of lfile
FileInputStream fis = new FileInputStream(lfile);
byte[] buf = new byte[1024];
int len = fis.read(buf, 0, buf.length);
long transferred = len;
long tenPct = filesize / 10;
int count = 0;
while(len > 0){
out.write(buf, 0, len);
len = fis.read(buf, 0, buf.length);
transferred += len;
if(transferred >= tenPct) {
count ++;
tenPct = transferred + filesize / 10;
tout.appendText(String.format("%d%%\n", 10*count));
}
}
fis.close();
// send '\0'
buf[0]=0;
out.write(buf, 0, 1);
out.flush();
ok = checkAck(in);
if(ok != 0){
throw new JSchException("checkAck failure, "+ok);
}
out.close();
channel.disconnect();
session.disconnect();
}
示例15: testSCP
import com.jcraft.jsch.ChannelExec; //导入方法依赖的package包/类
@Test
public void testSCP() throws Exception {
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("scp -t /usr/local/bin/ConfigManager-service.jar");
// get I/O streams for remote scp
OutputStream out=channel.getOutputStream();
InputStream in=channel.getInputStream();
channel.connect();
if(checkAck(in)!=0){
System.exit(0);
}
File lfile = new File("/Users/starksm/Dev/IoT/BLE/RaspberryPiBeaconParser/ConfigManager/build/libs/ConfigManager-service.jar");
// send "C0644 filesize filename", where filename should not include '/'
long filesize=lfile.length();
String command="C0644 "+filesize+" " + lfile.getName();
/*???
if(lfile.lastIndexOf('/')>0){
command+=lfile.substring(lfile.lastIndexOf('/')+1);
}
else{
command+=lfile;
}
*/
command+="\n";
out.write(command.getBytes()); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
// send a content of lfile
FileInputStream fis=new FileInputStream(lfile);
byte[] buf=new byte[1024];
while(true){
int len=fis.read(buf, 0, buf.length);
if(len<=0) break;
out.write(buf, 0, len); //out.flush();
}
fis.close();
// send '\0'
buf[0]=0;
out.write(buf, 0, 1);
out.flush();
if(checkAck(in)!=0){
throw new IOException("checkAck error");
}
out.close();
channel.disconnect();
session.disconnect();
}