Search in sources :

Example 26 with JSch

use of com.jcraft.jsch.JSch in project azure-tools-for-java by Microsoft.

the class SparkSubmitHelper method sftpFileToEmulator.

public String sftpFileToEmulator(String localFile, String folderPath, IClusterDetail clusterDetail) throws IOException, HDIException, JSchException, SftpException {
    EmulatorClusterDetail emulatorClusterDetail = (EmulatorClusterDetail) clusterDetail;
    final File file = new File(localFile);
    try (FileInputStream fileInputStream = new FileInputStream(file)) {
        try (BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
            String sshEndpoint = emulatorClusterDetail.getSSHEndpoint();
            URL url = new URL(sshEndpoint);
            String host = url.getHost();
            int port = url.getPort();
            JSch jsch = new JSch();
            Session session = jsch.getSession(emulatorClusterDetail.getHttpUserName(), host, port);
            session.setPassword(emulatorClusterDetail.getHttpPassword());
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            String[] folders = folderPath.split("/");
            for (String folder : folders) {
                if (folder.length() > 0) {
                    try {
                        channel.cd(folder);
                    } catch (SftpException e) {
                        channel.mkdir(folder);
                        channel.cd(folder);
                    }
                }
            }
            channel.put(bufferedInputStream, file.getName());
            channel.disconnect();
            session.disconnect();
            return file.getName();
        }
    }
}
Also used : EmulatorClusterDetail(com.microsoft.azure.hdinsight.sdk.cluster.EmulatorClusterDetail) SftpException(com.jcraft.jsch.SftpException) JSch(com.jcraft.jsch.JSch) FileInputStream(java.io.FileInputStream) URL(java.net.URL) ChannelSftp(com.jcraft.jsch.ChannelSftp) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) Session(com.jcraft.jsch.Session)

Example 27 with JSch

use of com.jcraft.jsch.JSch in project azure-tools-for-java by Microsoft.

the class AddNewEmulatorForm method preEmulatorSetupCheck.

private void preEmulatorSetupCheck() {
    isCarryOnNextStep = true;
    errorMessage = null;
    emulatorSetupLog = new StringBuilder();
    errorMessageField.setText("");
    emulatorLogPanel.setVisible(false);
    clusterName = clusterNameField.getText().trim();
    userName = userNameField.getText().trim();
    password = String.valueOf(passwordField.getPassword());
    livyEndpoint = livyEndpointField.getText().trim().replaceAll("/+$", "");
    sshEndpoint = sshEndpointField.getText().trim().replaceAll("/+$", "");
    sparkHistoryEndpoint = sparkHistoryEndpointField.getText().trim().replaceAll("/+$", "");
    ambariEndpoint = ambariEndpointField.getText().trim().replaceAll("/+$", "");
    try {
        host = new URI(sshEndpoint).getHost();
        sshPort = new URI(sshEndpoint).getPort();
        jsch = new JSch();
        config = new Properties();
        config.put("StrictHostKeyChecking", "no");
    } catch (Exception exception) {
        errorMessage = exception.getMessage();
        isCarryOnNextStep = false;
    }
    if (isCarryOnNextStep && StringHelper.isNullOrWhiteSpace(clusterName) || StringHelper.isNullOrWhiteSpace(sshEndpoint) || StringHelper.isNullOrWhiteSpace(livyEndpoint) || StringHelper.isNullOrWhiteSpace(userName) || StringHelper.isNullOrWhiteSpace(password) || StringHelper.isNullOrWhiteSpace(sparkHistoryEndpoint) || StringHelper.isNullOrWhiteSpace(ambariEndpoint)) {
        errorMessage = "Cluster Name, Endpoint fields, User Name, or Password shouldn't be empty";
        isCarryOnNextStep = false;
    } else if (isCarryOnNextStep && ClusterManagerEx.getInstance().isEmulatorClusterExist(clusterName)) {
        errorMessage = "Cluster Name already exist in current list";
        isCarryOnNextStep = false;
    } else if (isCarryOnNextStep && !checkSshEndpoint()) {
        errorMessage = "Could not ssh to emulator, please check username, password and ssh port.";
        isCarryOnNextStep = false;
    }
}
Also used : JSch(com.jcraft.jsch.JSch) Properties(java.util.Properties) URI(java.net.URI)

Example 28 with JSch

use of com.jcraft.jsch.JSch in project azure-tools-for-java by Microsoft.

the class AzureDockerCertVaultOps method generateSSHKeys.

public static AzureDockerCertVault generateSSHKeys(String passPhrase, String comment) throws AzureDockerException {
    try {
        AzureDockerCertVault result = new AzureDockerCertVault();
        JSch jsch = new JSch();
        KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA);
        ByteArrayOutputStream privateKeyBuff = new ByteArrayOutputStream(2048);
        ByteArrayOutputStream publicKeyBuff = new ByteArrayOutputStream(2048);
        keyPair.writePublicKey(publicKeyBuff, (comment != null) ? comment : "DockerSSHCerts");
        if (passPhrase == null || passPhrase.isEmpty()) {
            keyPair.writePrivateKey(privateKeyBuff);
        } else {
            keyPair.writePrivateKey(privateKeyBuff, passPhrase.getBytes());
        }
        result.sshKey = privateKeyBuff.toString();
        result.sshPubKey = publicKeyBuff.toString();
        return result;
    } catch (Exception e) {
        throw new AzureDockerException(e.getMessage());
    }
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) AzureDockerCertVault(com.microsoft.azure.docker.model.AzureDockerCertVault) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) CloudException(com.microsoft.azure.CloudException)

Example 29 with JSch

use of com.jcraft.jsch.JSch in project GNS by MobilityFirst.

the class UserAuthPubKey method main.

/**
   *
   * @param arg
   */
public static void main(String[] arg) {
    try {
        JSch jsch = new JSch();
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Choose your privatekey(ex. ~/.ssh/id_dsa)");
        chooser.setFileHidingEnabled(false);
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            System.out.println("You chose " + chooser.getSelectedFile().getAbsolutePath() + ".");
            //			 , "passphrase"
            jsch.addIdentity(//			 , "passphrase"
            chooser.getSelectedFile().getAbsolutePath());
        }
        String host = null;
        if (arg.length > 0) {
            host = arg[0];
        } else {
            host = JOptionPane.showInputDialog("Enter username@hostname", System.getProperty("user.name") + "@localhost");
        }
        String user = host.substring(0, host.indexOf('@'));
        host = host.substring(host.indexOf('@') + 1);
        Session session = jsch.getSession(user, host, 22);
        // username and passphrase will be given via UserInfo interface.
        UserInfo ui = new UserInfoPrompted();
        session.setUserInfo(ui);
        session.connect();
        Channel channel = session.openChannel("shell");
        channel.setInputStream(System.in);
        channel.setOutputStream(System.out);
        channel.connect();
    } catch (HeadlessException | JSchException e) {
        System.out.println(e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) JFileChooser(javax.swing.JFileChooser) UserInfoPrompted(edu.umass.cs.aws.networktools.UserInfoPrompted) HeadlessException(java.awt.HeadlessException) Channel(com.jcraft.jsch.Channel) UserInfo(com.jcraft.jsch.UserInfo) JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Example 30 with JSch

use of com.jcraft.jsch.JSch in project GNS by MobilityFirst.

the class SSHClient method scpTo.

/**
   *
   * @param user
   * @param host
   * @param keyFile
   * @param lfile
   * @param rfile
   */
public static void scpTo(String user, String host, File keyFile, String lfile, String rfile) {
    if (verbose) {
        System.out.println("Remote copy file from " + lfile + " to " + host + "@" + user + ":" + rfile);
    }
    FileInputStream fis = null;
    try {
        JSch jsch = new JSch();
        Session session = authenticateWithKey(jsch, user, host, keyFile);
        // username and password will be given via UserInfo interface.
        UserInfo ui = new UserInfoPrompted();
        session.setUserInfo(ui);
        session.connect();
        boolean ptimestamp = true;
        // exec 'scp -t rfile' remotely
        String command = "scp " + (ptimestamp ? "-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();
        int ack = checkAck(in);
        if (ack != 0) {
            System.out.println("ACK was " + ack);
            return;
        }
        File _lfile = new File(lfile);
        if (ptimestamp) {
            command = "T " + (_lfile.lastModified() / 1000) + " 0";
            // The access time should be sent here,
            // but it is not accessible with JavaAPI ;-<
            command += (" " + (_lfile.lastModified() / 1000) + " 0\n");
            out.write(command.getBytes());
            out.flush();
            ack = checkAck(in);
            if (ack != 0) {
                System.out.println("ACK was " + ack);
                return;
            }
        }
        // send "C0644 filesize filename", where filename should not include '/'
        long filesize = _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();
        ack = checkAck(in);
        if (ack != 0) {
            System.out.println("ACK was " + ack);
            return;
        }
        // send a content of lfile
        fis = new FileInputStream(lfile);
        byte[] buf = new byte[1024];
        while (true) {
            int len = fis.read(buf, 0, buf.length);
            if (len <= 0) {
                break;
            }
            //out.flush();
            out.write(buf, 0, len);
        }
        fis.close();
        fis = null;
        // send '\0'
        buf[0] = 0;
        out.write(buf, 0, 1);
        out.flush();
        ack = checkAck(in);
        if (ack != 0) {
            System.out.println("ACK was " + ack);
            return;
        }
        out.close();
        channel.disconnect();
        session.disconnect();
    } catch (JSchException | IOException e) {
        GNSConfig.getLogger().severe(e.toString());
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (Exception ee) {
        }
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) UserInfo(com.jcraft.jsch.UserInfo) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) FileInputStream(java.io.FileInputStream) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException) File(java.io.File) Session(com.jcraft.jsch.Session)

Aggregations

JSch (com.jcraft.jsch.JSch)35 Session (com.jcraft.jsch.Session)23 JSchException (com.jcraft.jsch.JSchException)16 IOException (java.io.IOException)14 Channel (com.jcraft.jsch.Channel)8 ChannelExec (com.jcraft.jsch.ChannelExec)7 File (java.io.File)7 InputStream (java.io.InputStream)7 ChannelSftp (com.jcraft.jsch.ChannelSftp)6 FileInputStream (java.io.FileInputStream)6 OutputStream (java.io.OutputStream)6 UserInfo (com.jcraft.jsch.UserInfo)5 Properties (java.util.Properties)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 KeyPair (com.jcraft.jsch.KeyPair)2 SftpException (com.jcraft.jsch.SftpException)2 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)2 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)2 UserInfoPrompted (edu.umass.cs.aws.networktools.UserInfoPrompted)2 HeadlessException (java.awt.HeadlessException)2