Search in sources :

Example 61 with Channel

use of com.jcraft.jsch.Channel 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 62 with Channel

use of com.jcraft.jsch.Channel 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 63 with Channel

use of com.jcraft.jsch.Channel in project OsmAnd-tools by osmandapp.

the class IndexUploader method uploadToSSH.

public void uploadToSSH(File f, String description, String size, String date, UploadSSHCredentials cred) throws IOException, JSchException {
    log.info("Uploading file " + f.getName() + " " + size + " MB " + date + " of " + description);
    // Upload to ftp
    JSch jSch = new JSch();
    boolean knownHosts = false;
    if (cred.knownHosts != null) {
        jSch.setKnownHosts(cred.knownHosts);
        knownHosts = true;
    }
    if (cred.privateKey != null) {
        jSch.addIdentity(cred.privateKey);
    }
    String serverName = cred.url;
    if (serverName.startsWith("ssh://")) {
        serverName = serverName.substring("ssh://".length());
    }
    Session session = jSch.getSession(cred.user, serverName);
    if (cred.password != null) {
        session.setPassword(cred.password);
    }
    if (!knownHosts) {
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
    }
    String rfile = cred.path + "/" + f.getName();
    String lfile = f.getAbsolutePath();
    session.connect();
    // exec 'scp -t rfile' remotely
    String command = "scp -p -t \"" + rfile + "\"";
    Channel channel = session.openChannel("exec");
    ((ChannelExec) channel).setCommand(command);
    // get I/O streams for remote scp
    OutputStream out = channel.getOutputStream();
    InputStream in = channel.getInputStream();
    channel.connect();
    if (checkAck(in) != 0) {
        channel.disconnect();
        session.disconnect();
        return;
    }
    // send "C0644 filesize filename", where filename should not include '/'
    long filesize = (new File(lfile)).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) {
        channel.disconnect();
        session.disconnect();
        return;
    }
    // send a content of lfile
    FileInputStream fis = new FileInputStream(lfile);
    byte[] buf = new byte[1024];
    try {
        int len;
        while ((len = fis.read(buf, 0, buf.length)) > 0) {
            // out.flush();
            out.write(buf, 0, len);
        }
    } finally {
        fis.close();
    }
    fis = null;
    // send '\0'
    buf[0] = 0;
    out.write(buf, 0, 1);
    out.flush();
    if (checkAck(in) != 0) {
        channel.disconnect();
        session.disconnect();
        return;
    }
    out.close();
    channel.disconnect();
    session.disconnect();
    log.info("Finish uploading file index");
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) ZipOutputStream(java.util.zip.ZipOutputStream) CodedOutputStream(com.google.protobuf.CodedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) JSch(com.jcraft.jsch.JSch) ChannelExec(com.jcraft.jsch.ChannelExec) FileInputStream(java.io.FileInputStream) RandomAccessFile(java.io.RandomAccessFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) Session(com.jcraft.jsch.Session)

Aggregations

Channel (com.jcraft.jsch.Channel)63 InputStream (java.io.InputStream)38 ChannelExec (com.jcraft.jsch.ChannelExec)33 JSch (com.jcraft.jsch.JSch)33 IOException (java.io.IOException)32 JSchException (com.jcraft.jsch.JSchException)30 Session (com.jcraft.jsch.Session)28 FileInputStream (java.io.FileInputStream)20 ChannelSftp (com.jcraft.jsch.ChannelSftp)18 OutputStream (java.io.OutputStream)18 BufferedReader (java.io.BufferedReader)15 File (java.io.File)14 InputStreamReader (java.io.InputStreamReader)13 Properties (java.util.Properties)13 SftpException (com.jcraft.jsch.SftpException)12 FileOutputStream (java.io.FileOutputStream)11 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