Search in sources :

Example 31 with Channel

use of com.jcraft.jsch.Channel in project litle-sdk-for-java by Vantiv.

the class Communication method sendLitleRequestFileToSFTP.

/**
 * This method sends the request file to Litle's server sFTP
 * @param requestFile
 * @param configuration
 * @throws IOException
 */
public void sendLitleRequestFileToSFTP(File requestFile, Properties configuration) throws IOException {
    String username = configuration.getProperty("sftpUsername");
    String password = configuration.getProperty("sftpPassword");
    String hostname = configuration.getProperty("batchHost");
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    JSch jsch = null;
    Session session = null;
    try {
        jsch = new JSch();
        session = jsch.getSession(username, hostname);
        session.setConfig(config);
        session.setPassword(password);
        session.connect();
    } catch (JSchException e) {
        throw new LitleBatchException("Exception connection to Litle", e);
    }
    Channel channel = null;
    try {
        channel = session.openChannel("sftp");
        channel.connect();
    } catch (JSchException e) {
        throw new LitleBatchException("Exception connection to Litle", e);
    }
    ChannelSftp sftp = (ChannelSftp) channel;
    boolean printxml = configuration.getProperty("printxml") != null && configuration.getProperty("printxml").equalsIgnoreCase("true");
    if (printxml) {
        BufferedReader reader = new BufferedReader(new FileReader(requestFile));
        String line = "";
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();
    }
    try {
        sftp.put(requestFile.getAbsolutePath(), "inbound/" + requestFile.getName() + ".prg");
        sftp.rename("inbound/" + requestFile.getName() + ".prg", "inbound/" + requestFile.getName() + ".asc");
    } catch (SftpException e) {
        throw new LitleBatchException("Exception SFTP operation", e);
    }
    channel.disconnect();
    session.disconnect();
}
Also used : JSchException(com.jcraft.jsch.JSchException) Properties(java.util.Properties) Channel(com.jcraft.jsch.Channel) SftpException(com.jcraft.jsch.SftpException) Properties(java.util.Properties) JSch(com.jcraft.jsch.JSch) ChannelSftp(com.jcraft.jsch.ChannelSftp) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Session(com.jcraft.jsch.Session)

Example 32 with Channel

use of com.jcraft.jsch.Channel in project opentest by mcdcorp.

the class PutToSftp method run.

@Override
public void run() {
    super.run();
    String sftpHost = this.readStringArgument("sftpHost");
    Integer sftpPort = this.readIntArgument("sftpPort", 22);
    String userName = this.readStringArgument("userName");
    String password = this.readStringArgument("password");
    String sourceDir = this.readStringArgument("sourceDir");
    String sourceFileName = this.readStringArgument("sourceFile");
    String destinationDir = this.readStringArgument("destinationDir");
    String destinationFileName = this.readStringArgument("destinationFile", sourceFileName);
    Session session = null;
    Channel channel = null;
    ChannelSftp channelSftp = null;
    try {
        JSch jsch = new JSch();
        session = jsch.getSession(userName, sftpHost, sftpPort);
        session.setPassword(password);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        this.log.trace("Connected to SFTP host");
        channel = session.openChannel("sftp");
        channel.connect();
        this.log.trace("The SFTP channel was opened and connected");
        channelSftp = (ChannelSftp) channel;
        channelSftp.cd(destinationDir);
        File sourceFile = new File(sourceDir, sourceFileName);
        FileInputStream inputStream = new FileInputStream(sourceFile);
        channelSftp.put(inputStream, destinationFileName);
        inputStream.close();
    } catch (Exception ex) {
        throw new RuntimeException("SFTP transfer failed", ex);
    } finally {
        if (channelSftp != null) {
            channelSftp.exit();
        }
        if (channel != null) {
            channel.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }
}
Also used : Channel(com.jcraft.jsch.Channel) JSch(com.jcraft.jsch.JSch) FileInputStream(java.io.FileInputStream) ChannelSftp(com.jcraft.jsch.ChannelSftp) File(java.io.File) Session(com.jcraft.jsch.Session)

Example 33 with Channel

use of com.jcraft.jsch.Channel in project KeyBox by skavanagh.

the class SSHUtil method pushUpload.

/**
 * distributes uploaded item to system defined
 *
 * @param hostSystem  object contains host system information
 * @param session     an established SSH session
 * @param source      source file
 * @param destination destination file
 * @return status uploaded file
 */
public static HostSystem pushUpload(HostSystem hostSystem, Session session, String source, String destination) {
    hostSystem.setStatusCd(HostSystem.SUCCESS_STATUS);
    Channel channel = null;
    ChannelSftp c = null;
    try (FileInputStream file = new FileInputStream(source)) {
        channel = session.openChannel("sftp");
        channel.setInputStream(System.in);
        channel.setOutputStream(System.out);
        channel.connect(CHANNEL_TIMEOUT);
        c = (ChannelSftp) channel;
        destination = destination.replaceAll("~\\/|~", "");
        c.put(file, destination);
    } catch (JSchException | IOException | SftpException ex) {
        log.info(ex.toString(), ex);
        hostSystem.setErrorMsg(ex.getMessage());
        hostSystem.setStatusCd(HostSystem.GENERIC_FAIL_STATUS);
    }
    // exit
    if (c != null) {
        c.exit();
    }
    // disconnect
    if (channel != null) {
        channel.disconnect();
    }
    return hostSystem;
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 34 with Channel

use of com.jcraft.jsch.Channel in project coprhd-controller by CoprHD.

the class CinderUtils method checkProviderConnection.

private static void checkProviderConnection(StorageProvider storageProvider) throws JSchException, SftpException, IOException {
    ChannelSftp sftp = null;
    Session session = null;
    try {
        JSch jsch = new JSch();
        session = jsch.getSession(storageProvider.getUserName(), storageProvider.getIPAddress(), storageProvider.getPortNumber());
        session.setPassword(storageProvider.getPassword());
        Hashtable<String, String> config = new Hashtable<String, String>();
        config.put(STRICT_HOST_KEY_CHECKING, NO);
        session.setConfig(config);
        session.connect(timeout);
        _log.debug("Session Connected...");
        Channel channel = session.openChannel("sftp");
        sftp = (ChannelSftp) channel;
        sftp.connect(connectTimeout);
    } finally {
        if (sftp != null) {
            sftp.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Hashtable(java.util.Hashtable) Channel(com.jcraft.jsch.Channel) JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Example 35 with Channel

use of com.jcraft.jsch.Channel in project coprhd-controller by CoprHD.

the class IsilonSshApi method executeSsh.

/**
 * Executes a command on the Isilon CLI.
 *
 * @param command command to execute on the Isilon CLI.
 * @param request payload for the command
 * @return result of executing the command.
 */
public IsilonXMLApiResult executeSsh(String command, String request) {
    IsilonXMLApiResult result = new IsilonXMLApiResult();
    if ((_host == null) || (_userName == null) || (_password == null)) {
        _log.error("Invalid connection parameter");
        result.setCommandFailed();
        return result;
    }
    String cmd = "isi " + command + " " + request;
    _log.info("executeSsh: cmd: " + cmd);
    InputStream in = null;
    Session session = null;
    Channel channel = null;
    try {
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        JSch jsch = new JSch();
        session = jsch.getSession(_userName, _host, DEFAULT_PORT);
        session.setPassword(_password);
        session.setConfig(config);
        session.connect();
        channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(cmd);
        channel.setInputStream(null);
        in = channel.getInputStream();
        channel.connect();
        byte[] tmp = new byte[BUFFER_SIZE];
        StringBuilder cmdResults = new StringBuilder();
        while (true) {
            while (in.available() > 0) {
                int i = in.read(tmp, 0, BUFFER_SIZE);
                if (i < 0)
                    break;
                cmdResults.append(new String(tmp, 0, i));
            }
            if (channel.isClosed()) {
                _log.info("Ssh exit status: " + channel.getExitStatus());
                result.setMessage(cmdResults.toString());
                // Set the command result status.
                if (channel.getExitStatus() == 0) {
                    StringTokenizer st = new StringTokenizer(cmdResults.toString());
                    if (st.hasMoreTokens()) {
                        // data mover name
                        st.nextToken();
                    }
                    String res = "";
                    if (st.hasMoreTokens()) {
                        // contains status or result.
                        res = st.nextToken();
                    }
                    if (res.equalsIgnoreCase("done")) {
                        result.setCommandSuccess();
                    } else if (res.equalsIgnoreCase("error")) {
                        result.setCommandFailed();
                    } else {
                        result.setCommandSuccess();
                    }
                } else {
                    result.setCommandFailed();
                }
                break;
            }
            try {
                Thread.sleep(_respDelay);
            } catch (InterruptedException e) {
                _log.error("VNX File executeSsh Communication thread interrupted for command: " + cmd, e);
            }
        }
        _log.info("executeSsh: Done");
    } catch (Exception e) {
        _log.error("VNX File executeSsh connection failed while attempting to execute: " + cmd, e);
        result.setCommandFailed();
        result.setMessage(e.getMessage());
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
        if (channel != null) {
            channel.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }
    return result;
}
Also used : InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) StringTokenizer(java.util.StringTokenizer) Session(com.jcraft.jsch.Session)

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