Search in sources :

Example 81 with ChannelExec

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

the class Terminate_a_Container method remove_from_haproxy.

// ///////////////////////////////////////////////////////////
public void remove_from_haproxy() {
    String haproy_ip = "194.249.1.110";
    String haproy_host_user = "root";
    String haproy_host_password = "***************";
    // ////////////////////////////// add new server
    try {
        String command = "sed " + "\'/" + "    server " + i_str + "-www " + new_IP + ":5000 check/d" + "\' /etc/haproxy/haproxy.cfg" + " > /etc/haproxy/test.cfg ; mv /etc/haproxy/test.cfg /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();
        System.out.println("error in remove_from_haproxy() first section");
    }
    // ////////////////////////////// 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();
        // to be deleted !!!!
        System.out.println("error in remove_from_haproxy() second section");
    }
}
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 82 with ChannelExec

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

the class HPA 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 83 with ChannelExec

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

the class TTS2 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 84 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project ant-ivy by apache.

the class Scp method getFileinfo.

/**
 * Initiates an SCP sequence but stops after getting fileinformation header
 *
 * @param remoteFile
 *            to get information for
 * @return the file information got
 * @throws IOException
 *             in case of network problems
 * @throws RemoteScpException
 *             in case of problems on the target system (connection ok)
 */
public FileInfo getFileinfo(String remoteFile) throws IOException, RemoteScpException {
    ChannelExec channel = null;
    FileInfo fileInfo = null;
    if (remoteFile == null) {
        throw new IllegalArgumentException("Null argument.");
    }
    String cmd = "scp -p -f \"" + remoteFile + "\"";
    try {
        channel = getExecChannel();
        channel.setCommand(cmd);
        fileInfo = receiveStream(channel, remoteFile, null);
        channel.disconnect();
    } catch (JSchException e) {
        throw new IOException("Error during SCP transfer. " + e.getMessage(), e);
    } finally {
        if (channel != null) {
            channel.disconnect();
        }
    }
    return fileInfo;
}
Also used : JSchException(com.jcraft.jsch.JSchException) IOException(java.io.IOException) ChannelExec(com.jcraft.jsch.ChannelExec)

Example 85 with ChannelExec

use of com.jcraft.jsch.ChannelExec in project ant-ivy by apache.

the class Scp method put.

/**
 * Copy a local file to a remote site, uses the specified mode when creating the file on the
 * remote side.
 *
 * @param localFile
 *            Path and name of local file. Must be absolute.
 * @param remoteTargetDir
 *            Remote target directory where the file has to end up (optional)
 * @param remoteTargetName
 *            file name to use on the target system
 * @param mode
 *            a four digit string (e.g., 0644, see "man chmod", "man open")
 * @throws IOException
 *             in case of network problems
 * @throws RemoteScpException
 *             in case of problems on the target system (connection ok)
 */
@SuppressWarnings("unused")
public void put(String localFile, String remoteTargetDir, String remoteTargetName, String mode) throws IOException, RemoteScpException {
    ChannelExec channel = null;
    if (localFile == null || remoteTargetName == null) {
        throw new IllegalArgumentException("Null argument.");
    }
    if (mode != null) {
        if (mode.length() != MODE_LENGTH) {
            throw new IllegalArgumentException("Invalid mode.");
        }
        for (char c : mode.toCharArray()) {
            if (!Character.isDigit(c)) {
                throw new IllegalArgumentException("Invalid mode.");
            }
        }
    }
    String cmd = "scp -t ";
    if (mode != null) {
        cmd += "-p ";
    }
    if (remoteTargetDir != null && remoteTargetDir.length() > 0) {
        cmd += "-d " + remoteTargetDir;
    }
    try {
        channel = getExecChannel();
        channel.setCommand(cmd);
        sendFile(channel, localFile, remoteTargetName, mode);
        channel.disconnect();
    } catch (JSchException e) {
        if (channel != null) {
            channel.disconnect();
        }
        throw new IOException("Error during SCP transfer." + e.getMessage(), e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) IOException(java.io.IOException) ChannelExec(com.jcraft.jsch.ChannelExec)

Aggregations

ChannelExec (com.jcraft.jsch.ChannelExec)99 InputStream (java.io.InputStream)63 IOException (java.io.IOException)59 JSchException (com.jcraft.jsch.JSchException)50 Channel (com.jcraft.jsch.Channel)48 Session (com.jcraft.jsch.Session)31 JSch (com.jcraft.jsch.JSch)29 FileInputStream (java.io.FileInputStream)26 InputStreamReader (java.io.InputStreamReader)26 BufferedReader (java.io.BufferedReader)23 OutputStream (java.io.OutputStream)22 File (java.io.File)17 Properties (java.util.Properties)16 FileOutputStream (java.io.FileOutputStream)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 SftpException (com.jcraft.jsch.SftpException)6 GFacException (org.apache.airavata.gfac.core.GFacException)6 ArrayList (java.util.ArrayList)5 Charset (java.nio.charset.Charset)4 UserInfo (com.jcraft.jsch.UserInfo)3