Search in sources :

Example 6 with Channel

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();
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) File(java.io.File) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Example 7 with Channel

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();
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Example 8 with Channel

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;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) Channel(com.jcraft.jsch.Channel) FileInputStream(java.io.FileInputStream) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

Example 9 with Channel

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();
}
Also used : InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) ChannelExec(com.jcraft.jsch.ChannelExec)

Example 10 with Channel

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();
}
Also used : StringOutputStream(org.eclipse.linuxtools.internal.systemtap.ui.ide.StringOutputStream) Channel(com.jcraft.jsch.Channel) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore)

Aggregations

Channel (com.jcraft.jsch.Channel)63 InputStream (java.io.InputStream)38 ChannelExec (com.jcraft.jsch.ChannelExec)33 IOException (java.io.IOException)33 JSch (com.jcraft.jsch.JSch)32 JSchException (com.jcraft.jsch.JSchException)30 Session (com.jcraft.jsch.Session)27 FileInputStream (java.io.FileInputStream)19 ChannelSftp (com.jcraft.jsch.ChannelSftp)18 OutputStream (java.io.OutputStream)17 BufferedReader (java.io.BufferedReader)15 File (java.io.File)13 InputStreamReader (java.io.InputStreamReader)13 Properties (java.util.Properties)13 SftpException (com.jcraft.jsch.SftpException)12 FileOutputStream (java.io.FileOutputStream)10 UserInfo (com.jcraft.jsch.UserInfo)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 GFacException (org.apache.airavata.gfac.core.GFacException)4 SSHApiException (org.apache.airavata.gfac.core.SSHApiException)3