Search in sources :

Example 26 with JSchException

use of com.jcraft.jsch.JSchException in project hadoop by apache.

the class SshFenceByTcpPort method tryFence.

@Override
public boolean tryFence(HAServiceTarget target, String argsStr) throws BadFencingConfigurationException {
    Args args = new Args(argsStr);
    InetSocketAddress serviceAddr = target.getAddress();
    String host = serviceAddr.getHostName();
    Session session;
    try {
        session = createSession(serviceAddr.getHostName(), args);
    } catch (JSchException e) {
        LOG.warn("Unable to create SSH session", e);
        return false;
    }
    LOG.info("Connecting to " + host + "...");
    try {
        session.connect(getSshConnectTimeout());
    } catch (JSchException e) {
        LOG.warn("Unable to connect to " + host + " as user " + args.user, e);
        return false;
    }
    LOG.info("Connected to " + host);
    try {
        return doFence(session, serviceAddr);
    } catch (JSchException e) {
        LOG.warn("Unable to achieve fencing on remote host", e);
        return false;
    } finally {
        session.disconnect();
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) InetSocketAddress(java.net.InetSocketAddress) Session(com.jcraft.jsch.Session)

Example 27 with JSchException

use of com.jcraft.jsch.JSchException in project hadoop by apache.

the class SFTPConnectionPool method disconnect.

void disconnect(ChannelSftp channel) throws IOException {
    if (channel != null) {
        // close connection if too many active connections
        boolean closeConnection = false;
        synchronized (this) {
            if (liveConnectionCount > maxConnection) {
                --liveConnectionCount;
                con2infoMap.remove(channel);
                closeConnection = true;
            }
        }
        if (closeConnection) {
            if (channel.isConnected()) {
                try {
                    Session session = channel.getSession();
                    channel.disconnect();
                    session.disconnect();
                } catch (JSchException e) {
                    throw new IOException(StringUtils.stringifyException(e));
                }
            }
        } else {
            returnToPool(channel);
        }
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) IOException(java.io.IOException) Session(com.jcraft.jsch.Session)

Example 28 with JSchException

use of com.jcraft.jsch.JSchException in project DataX by alibaba.

the class SftpHelper method loginFtpServer.

@Override
public void loginFtpServer(String host, String username, String password, int port, int timeout, String connectMode) {
    // 创建JSch对象
    JSch jsch = new JSch();
    try {
        session = jsch.getSession(username, host, port);
        // 如果服务器连接不上,则抛出异常
        if (session == null) {
            throw DataXException.asDataXException(FtpReaderErrorCode.FAIL_LOGIN, "session is null,无法通过sftp与服务器建立链接,请检查主机名和用户名是否正确.");
        }
        // 设置密码
        session.setPassword(password);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        // 为Session对象设置properties
        session.setConfig(config);
        // 设置timeout时间
        session.setTimeout(timeout);
        // 通过Session建立链接
        session.connect();
        // 打开SFTP通道
        channelSftp = (ChannelSftp) session.openChannel("sftp");
        // 建立SFTP通道的连接
        channelSftp.connect();
    //设置命令传输编码
    //String fileEncoding = System.getProperty("file.encoding");
    //channelSftp.setFilenameEncoding(fileEncoding);		
    } catch (JSchException e) {
        if (null != e.getCause()) {
            String cause = e.getCause().toString();
            String unknownHostException = "java.net.UnknownHostException: " + host;
            String illegalArgumentException = "java.lang.IllegalArgumentException: port out of range:" + port;
            String wrongPort = "java.net.ConnectException: Connection refused";
            if (unknownHostException.equals(cause)) {
                String message = String.format("请确认ftp服务器地址是否正确,无法连接到地址为: [%s] 的ftp服务器", host);
                LOG.error(message);
                throw DataXException.asDataXException(FtpReaderErrorCode.FAIL_LOGIN, message, e);
            } else if (illegalArgumentException.equals(cause) || wrongPort.equals(cause)) {
                String message = String.format("请确认连接ftp服务器端口是否正确,错误的端口: [%s] ", port);
                LOG.error(message);
                throw DataXException.asDataXException(FtpReaderErrorCode.FAIL_LOGIN, message, e);
            }
        } else {
            if ("Auth fail".equals(e.getMessage())) {
                String message = String.format("与ftp服务器建立连接失败,请检查用户名和密码是否正确: [%s]", "message:host =" + host + ",username = " + username + ",port =" + port);
                LOG.error(message);
                throw DataXException.asDataXException(FtpReaderErrorCode.FAIL_LOGIN, message);
            } else {
                String message = String.format("与ftp服务器建立连接失败 : [%s]", "message:host =" + host + ",username = " + username + ",port =" + port);
                LOG.error(message);
                throw DataXException.asDataXException(FtpReaderErrorCode.FAIL_LOGIN, message, e);
            }
        }
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) JSch(com.jcraft.jsch.JSch) Properties(java.util.Properties)

Example 29 with JSchException

use of com.jcraft.jsch.JSchException in project azure-sdk-for-java by Azure.

the class CreateVirtualMachineUsingCustomImageFromVM method deprovisionAgentInLinuxVM.

/**
     * De-provision an Azure linux virtual machine.
     *
     * @param host the public host name
     * @param port the ssh port
     * @param userName the ssh user name
     * @param password the ssh user password
     */
protected static void deprovisionAgentInLinuxVM(String host, int port, String userName, String password) {
    SSHShell shell = null;
    try {
        System.out.println("Trying to de-provision: " + host);
        shell = SSHShell.open(host, port, userName, password);
        List<String> deprovisionCommand = new ArrayList<>();
        deprovisionCommand.add("sudo waagent -deprovision+user --force");
        String output = shell.runCommands(deprovisionCommand);
        System.out.println(output);
    } catch (JSchException jSchException) {
        System.out.println(jSchException.getMessage());
    } catch (IOException ioException) {
        System.out.println(ioException.getMessage());
    } catch (Exception exception) {
        System.out.println(exception.getMessage());
    } finally {
        if (shell != null) {
            shell.close();
        }
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SSHShell(com.microsoft.azure.management.samples.SSHShell) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

Example 30 with JSchException

use of com.jcraft.jsch.JSchException in project azure-sdk-for-java by Azure.

the class ManageManagedDisks method deprovisionAgentInLinuxVM.

private static void deprovisionAgentInLinuxVM(String host, int port, String userName, String password) {
    SSHShell shell = null;
    try {
        System.out.println("Trying to de-provision: " + host);
        shell = SSHShell.open(host, port, userName, password);
        List<String> deprovisionCommand = new ArrayList<>();
        deprovisionCommand.add("sudo waagent -deprovision+user --force");
        String output = shell.runCommands(deprovisionCommand);
        System.out.println(output);
    } catch (JSchException jSchException) {
        System.out.println(jSchException.getMessage());
    } catch (IOException ioException) {
        System.out.println(ioException.getMessage());
    } catch (Exception exception) {
        System.out.println(exception.getMessage());
    } finally {
        if (shell != null) {
            shell.close();
        }
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SSHShell(com.microsoft.azure.management.samples.SSHShell) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

Aggregations

JSchException (com.jcraft.jsch.JSchException)40 IOException (java.io.IOException)20 Session (com.jcraft.jsch.Session)19 JSch (com.jcraft.jsch.JSch)14 ChannelSftp (com.jcraft.jsch.ChannelSftp)11 Channel (com.jcraft.jsch.Channel)9 SftpException (com.jcraft.jsch.SftpException)9 ChannelExec (com.jcraft.jsch.ChannelExec)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 UserInfo (com.jcraft.jsch.UserInfo)5 File (java.io.File)5 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)5 SSHShell (com.microsoft.azure.management.samples.SSHShell)4 Properties (java.util.Properties)4 BufferedReader (java.io.BufferedReader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileInputStream (java.io.FileInputStream)3 InputStreamReader (java.io.InputStreamReader)3 OutputStream (java.io.OutputStream)3