use of com.jcraft.jsch.Channel in project bamboobsc by billchen198318.
the class SFtpClientUtils method get.
/**
* 抓遠端檔案然後存到本機 , 多筆
*
* @param user
* @param password
* @param addr
* @param port
* @param remoteFile
* @param localFile
* @throws JSchException
* @throws SftpException
* @throws Exception
*/
public static void get(String user, String password, String addr, int port, List<String> remoteFile, List<String> localFile) throws JSchException, SftpException, Exception {
Session session = getSession(user, password, addr, port);
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
try {
for (int i = 0; i < remoteFile.size(); i++) {
String rf = remoteFile.get(i);
String lf = localFile.get(i);
logger.info("get remote file: " + rf + " write to:" + lf);
sftpChannel.get(rf, lf);
File f = new File(lf);
if (!f.exists()) {
f = null;
logger.error("get remote file:" + rf + " fail!");
throw new Exception("get remote file:" + rf + " fail!");
}
f = null;
logger.info("success write:" + lf);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
sftpChannel.exit();
channel.disconnect();
session.disconnect();
}
}
use of com.jcraft.jsch.Channel in project bamboobsc by billchen198318.
the class SFtpClientUtils method rm.
public static void rm(String user, String password, String addr, int port, List<String> fileName) throws JSchException, SftpException, Exception {
if (fileName == null || fileName.size() < 1) {
return;
}
Session session = getSession(user, password, addr, port);
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
try {
for (String f : fileName) {
sftpChannel.rm(f);
logger.warn("success remove file from " + addr + " :" + fileName);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
sftpChannel.exit();
channel.disconnect();
session.disconnect();
}
}
use of com.jcraft.jsch.Channel in project n2a by frothga.
the class Connection method send.
public static Result send(File srcFile, String destPath) throws Exception {
connect();
Result result = new Result();
FileInputStream fis = null;
OutputStream out = null;
Channel channel = null;
try {
String lfile = srcFile.getAbsolutePath();
// exec 'scp -t rfile' remotely
String command = "scp -p -t '" + destPath + "'";
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp
out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
if (checkAck(in) != 0) {
result.error = true;
return result;
}
// send "C0644 filesize filename", where filename should not include '/'
long fileSize = srcFile.length();
command = "C0644 " + fileSize + " ";
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) {
result.error = true;
return result;
}
// send a content of lfile
fis = new FileInputStream(lfile);
byte[] buf = new byte[1024];
while (true) {
int len = fis.read(buf, 0, buf.length);
if (len <= 0) {
break;
}
// out.flush();
out.write(buf, 0, len);
}
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
if (checkAck(in) != 0) {
result.error = true;
return result;
}
} catch (Exception e) {
result.error = true;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
}
}
if (out != null) {
try {
out.close();
} catch (Exception e) {
}
}
if (channel != null) {
try {
channel.disconnect();
} catch (Exception e) {
}
}
}
return result;
}
use of com.jcraft.jsch.Channel in project Lucee by lucee.
the class SSHManager method sendCommand.
public String sendCommand(String command) throws JSchException, IOException {
StringBuilder outputBuffer = new StringBuilder();
Channel channel = sesConnection.openChannel("exec");
((ChannelExec) channel).setCommand(command);
InputStream commandOutput = channel.getInputStream();
channel.connect();
int readByte = commandOutput.read();
while (readByte != 0xffffffff) {
outputBuffer.append((char) readByte);
readByte = commandOutput.read();
}
channel.disconnect();
return outputBuffer.toString();
}
use of com.jcraft.jsch.Channel in project linuxtools by eclipse.
the class TapsetParser method runRemoteStapAttempt.
private String runRemoteStapAttempt(String[] args, boolean getErrors) throws JSchException {
StringOutputStream str = new StringOutputStream();
StringOutputStream strErr = new StringOutputStream();
IPreferenceStore p = ConsoleLogPlugin.getDefault().getPreferenceStore();
String user = p.getString(ConsoleLogPreferenceConstants.SCP_USER);
String host = p.getString(ConsoleLogPreferenceConstants.HOST_NAME);
String password = p.getString(ConsoleLogPreferenceConstants.SCP_PASSWORD);
int port = p.getInt(ConsoleLogPreferenceConstants.PORT_NUMBER);
Channel channel = LinuxtoolsProcessFactory.execRemoteAndWait(args, str, strErr, user, host, password, port, EnvironmentVariablesPreferencePage.getEnvironmentVariables());
if (channel == null) {
return null;
}
channel.getSession().disconnect();
channel.disconnect();
return (!getErrors ? str : strErr).toString();
}
Aggregations