Search in sources :

Example 26 with Channel

use of com.jcraft.jsch.Channel in project ASAP by salmant.

the class DM method How_many_existing_servers.

// ///////////////////////////////////////////
public static int How_many_existing_servers() {
    int i = 0;
    String haproy_ip = "194.249.1.110";
    String haproy_host_user = "root";
    String haproy_host_password = "********************";
    try {
        String command = "cat /etc/haproxy/haproxy.cfg";
        JSch jsch = new JSch();
        com.jcraft.jsch.Session session = jsch.getSession(haproy_host_user, haproy_ip, 22);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        ;
        session.setPassword(haproy_host_password);
        session.connect();
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        channel.setInputStream(null);
        ((ChannelExec) channel).setErrStream(System.err);
        InputStream input = channel.getInputStream();
        channel.connect();
        try {
            InputStreamReader inputReader = new InputStreamReader(input);
            BufferedReader bufferedReader = new BufferedReader(inputReader);
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains("-www"))
                    i++;
            // System.out.println(line);
            }
            bufferedReader.close();
            inputReader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            return i;
        }
        channel.disconnect();
        session.disconnect();
        return i;
    } catch (Exception ex) {
        ex.printStackTrace();
        return i;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) Properties(java.util.Properties) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader)

Example 27 with Channel

use of com.jcraft.jsch.Channel in project ASAP by salmant.

the class DM method restart_haproxy.

// ///////////////////////////////////////////
public static void restart_haproxy() {
    String haproy_ip = "194.249.1.110";
    String haproy_host_user = "root";
    String haproy_host_password = "**************************";
    try {
        String command = "sh /haproxy.sh";
        JSch jsch = new JSch();
        com.jcraft.jsch.Session session = jsch.getSession(haproy_host_user, haproy_ip, 22);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        ;
        session.setPassword(haproy_host_password);
        session.connect();
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        channel.setInputStream(null);
        ((ChannelExec) channel).setErrStream(System.err);
        InputStream input = channel.getInputStream();
        channel.connect();
        try {
            InputStreamReader inputReader = new InputStreamReader(input);
            BufferedReader bufferedReader = new BufferedReader(inputReader);
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
            // System.out.println(line);
            }
            bufferedReader.close();
            inputReader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        channel.disconnect();
        session.disconnect();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) Properties(java.util.Properties) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader)

Example 28 with Channel

use of com.jcraft.jsch.Channel in project ASAP by salmant.

the class Run_a_Container method add_to_haproxy.

// ///////////////////////////////////////////////////////////
public void add_to_haproxy() {
    String haproy_ip = "194.249.1.110";
    String haproy_host_user = "root";
    String haproy_host_password = "*****************";
    // ////////////////////////////// add new server
    try {
        String command = "echo " + "\"" + "    server " + i_str + "-www " + new_IP + ":5000 check" + "\"" + ">> /etc/haproxy/haproxy.cfg";
        JSch jsch = new JSch();
        com.jcraft.jsch.Session session = jsch.getSession(haproy_host_user, haproy_ip, 22);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        ;
        session.setPassword(haproy_host_password);
        session.connect();
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        channel.setInputStream(null);
        ((ChannelExec) channel).setErrStream(System.err);
        InputStream input = channel.getInputStream();
        channel.connect();
        try {
            InputStreamReader inputReader = new InputStreamReader(input);
            BufferedReader bufferedReader = new BufferedReader(inputReader);
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
            // System.out.println(line);
            }
            bufferedReader.close();
            inputReader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        channel.disconnect();
        session.disconnect();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    // ////////////////////////////// reload HAProxy
    try {
        String command = "sh /haproxy.sh";
        JSch jsch = new JSch();
        com.jcraft.jsch.Session session = jsch.getSession(haproy_host_user, haproy_ip, 22);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        ;
        session.setPassword(haproy_host_password);
        session.connect();
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        channel.setInputStream(null);
        ((ChannelExec) channel).setErrStream(System.err);
        InputStream input = channel.getInputStream();
        channel.connect();
        try {
            InputStreamReader inputReader = new InputStreamReader(input);
            BufferedReader bufferedReader = new BufferedReader(inputReader);
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
            // System.out.println(line);
            }
            bufferedReader.close();
            inputReader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        channel.disconnect();
        session.disconnect();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) Properties(java.util.Properties) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader)

Example 29 with Channel

use of com.jcraft.jsch.Channel in project onebusaway-application-modules by camsys.

the class GtfsRealtimeSource method readFeedFromUrl.

/**
 * This method was added to allow accessing an SFTP feed, since the Java URL
 * class does not yet support the SFTP protocol.
 *
 * @param url of the SFTP feed to read
 * @return a {@link FeedMessage} constructed from the protocol buffer content
 *         of the specified url
 * @throws IOException
 */
private FeedMessage readFeedFromUrl(String url) throws IOException {
    Session session = null;
    Channel channel = null;
    ChannelSftp downloadChannelSftp = null;
    InputStream in = null;
    JSch jsch = new JSch();
    // Parse SFTP URL
    int idx = url.indexOf("//") + 2;
    int idx2 = url.indexOf(":", idx);
    String user = url.substring(idx, idx2);
    idx = idx2 + 1;
    idx2 = url.indexOf("@");
    String pw = url.substring(idx, idx2);
    url = url.substring(idx2 + 1);
    idx = url.indexOf(":");
    String host = url.substring(0, idx);
    String rdir = "";
    idx = url.indexOf("/") + 1;
    idx2 = url.lastIndexOf("/");
    if (idx2 > idx) {
        rdir = url.substring(idx, idx2);
    } else {
        idx2 = idx - 1;
    }
    String rfile = url.substring(idx2 + 1);
    try {
        session = jsch.getSession(user, host, 22);
        session.setPassword(pw);
        session.setConfig("StrictHostKeyChecking", "no");
        // Set timeout to 10 seconds
        session.connect(10000);
        channel = session.openChannel("sftp");
        channel.connect();
        downloadChannelSftp = (ChannelSftp) channel;
        downloadChannelSftp.cd(downloadChannelSftp.getHome() + "/" + rdir);
        File downloadFile = new File(downloadChannelSftp.getHome() + "/" + rfile);
        in = downloadChannelSftp.get(downloadFile.getName());
        return FeedMessage.parseFrom(in, _registry);
    } catch (JSchException ex) {
        _log.error("connection issue with sftp url " + url);
        return getDefaultFeedMessage();
    } catch (SftpException e) {
        _log.error("connection issue with sftp");
        e.printStackTrace();
        return getDefaultFeedMessage();
    } finally {
        try {
            if (channel != null)
                channel.disconnect();
            if (session != null)
                session.disconnect();
            if (in != null)
                in.close();
        } catch (IOException ex) {
            _log.error("error closing url stream " + url);
        }
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) SftpException(com.jcraft.jsch.SftpException) ServiceAlertLocalizedString(org.onebusaway.transit_data_federation.impl.service_alerts.ServiceAlertLocalizedString) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) File(java.io.File) Session(com.jcraft.jsch.Session)

Example 30 with Channel

use of com.jcraft.jsch.Channel in project structr by structr.

the class SSHTest method setupSftpClient.

/**
 * Creates an FTP client, a backend user and logs this user in.
 *
 * @param username
 * @return
 */
protected ChannelSftp setupSftpClient(final String username, final String password) {
    try (final Tx tx = app.tx()) {
        ftpUser = createFTPUser(username, password);
        tx.success();
    } catch (FrameworkException fex) {
        logger.error("Unable to create SFTP user", fex);
    }
    JSch jsch = new JSch();
    try {
        final Session session = jsch.getSession(username, host, sshPort);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword(password);
        session.connect(1000);
        final Channel channel = session.openChannel("sftp");
        channel.connect(1000);
        return (ChannelSftp) channel;
    } catch (JSchException ex) {
        ex.printStackTrace();
    }
    return null;
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Channel(com.jcraft.jsch.Channel) JSch(com.jcraft.jsch.JSch) 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