Search in sources :

Example 41 with Channel

use of com.jcraft.jsch.Channel in project bamboobsc by billchen198318.

the class SFtpClientUtils method put.

/**
	 * 本地檔案放到遠端SFTP上
	 * 	
	 * @param user
	 * @param password
	 * @param addr
	 * @param port
	 * @param localFile
	 * @param remoteFile
	 * @throws JSchException
	 * @throws SftpException
	 * @throws Exception
	 */
public static void put(String user, String password, String addr, int port, List<String> localFile, List<String> remoteFile) 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 < localFile.size(); i++) {
            String rf = remoteFile.get(i);
            String lf = localFile.get(i);
            logger.info("put local file: " + lf + " write to " + addr + " :" + rf);
            sftpChannel.put(lf, rf);
            logger.info("success write to " + addr + " :" + rf);
        }
    } 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 42 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, String remoteFile, String localFile) throws JSchException, SftpException, Exception {
    Session session = getSession(user, password, addr, port);
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    logger.info("get remote file: " + remoteFile + " write to:" + localFile);
    try {
        sftpChannel.get(remoteFile, localFile);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        sftpChannel.exit();
        channel.disconnect();
        session.disconnect();
    }
    File f = new File(localFile);
    if (!f.exists()) {
        f = null;
        logger.error("get remote file:" + remoteFile + " fail!");
        throw new Exception("get remote file:" + remoteFile + " fail!");
    }
    f = null;
    logger.info("success write:" + localFile);
}
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 43 with Channel

use of com.jcraft.jsch.Channel in project bamboobsc by billchen198318.

the class SFtpClientUtils method getRemoteFileList.

@SuppressWarnings("unchecked")
public static Vector<LsEntry> getRemoteFileList(String user, String password, String addr, int port, String cwd) throws JSchException, SftpException, Exception {
    Session session = getSession(user, password, addr, port);
    Vector<LsEntry> lsVec = null;
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    try {
        //sftpChannel.lpwd()
        lsVec = (Vector<LsEntry>) sftpChannel.ls(cwd);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        sftpChannel.exit();
        channel.disconnect();
        session.disconnect();
    }
    return lsVec;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Example 44 with Channel

use of com.jcraft.jsch.Channel in project Lucee by lucee.

the class SFTPClientImpl method connect.

@Override
public void connect() throws SocketException, IOException {
    try {
        session = jsch.getSession(username, host.getHostAddress(), port);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        UserInfo ui = new UserInfoImpl(password, null);
        session.setUserInfo(ui);
        if (timeout > 0)
            session.setTimeout(timeout);
        session.connect();
        Channel channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp) channel;
        // check fingerprint
        if (!StringUtil.isEmpty(fingerprint)) {
            if (!fingerprint.equalsIgnoreCase(fingerprint())) {
                disconnect();
                throw new IOException("given fingerprint is not a match.");
            }
        }
        handleSucess();
    } catch (JSchException e) {
        handleFail(e, stopOnError);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) Channel(com.jcraft.jsch.Channel) UserInfo(com.jcraft.jsch.UserInfo) IOException(java.io.IOException)

Example 45 with Channel

use of com.jcraft.jsch.Channel in project n2a by frothga.

the class Connection method execRun.

private static Result execRun(String cmd) throws Exception {
    connect();
    Result result = new Result();
    ByteArrayOutputStream errStream = new ByteArrayOutputStream();
    Channel channel = null;
    try {
        channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(cmd);
        channel.setInputStream(null);
        ((ChannelExec) channel).setErrStream(errStream);
        InputStream in = channel.getInputStream();
        channel.connect();
        StringBuffer b = new StringBuffer();
        byte[] tmp = new byte[1024];
        while (true) {
            while (in.available() > 0) {
                int i = in.read(tmp, 0, 1024);
                if (i < 0) {
                    break;
                }
                b.append(new String(tmp, 0, i));
            }
            if (channel.isClosed()) {
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception ee) {
            }
        }
        result.errorCode = channel.getExitStatus();
        result.error = result.errorCode != 0;
        result.stdOut = b.toString();
    } catch (Exception e) {
        result.error = true;
    } finally {
        if (channel != null) {
            try {
                channel.disconnect();
            } catch (Exception e) {
            }
        }
    }
    result.stdErr = errStream.toString();
    return result;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

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