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();
}
}
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);
}
}
}
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);
}
}
}
}
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();
}
}
}
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();
}
}
}
Aggregations