return true;
}
public static void attemptConnect(String host, String username, String password) throws Exception {
SSHTools ssh = new SSHTools(username, null);
SFTPSession sftp = null;
try {
HostAndPort hostAndPort = HostAndPort.fromString(host);
if (hostAndPort.hasPort()) {
sftp = ssh.getSftpSession(username, password, null, hostAndPort.getHostText(), hostAndPort.getPort(), null);
}
else {
sftp = ssh.getSftpSession(username, password, null, host, null);
}
} catch (SFTPException e) {
String errorMsg = e.getCause().getMessage();
/*
* e.getCause() is JSchException and the java exception class name only appears in message
* hide java class name and extract error message
*/
Pattern pattern = Pattern.compile("(java.*Exception: )(.*)");
Matcher matcher = pattern.matcher(errorMsg);
if (matcher.matches()) {
if (errorMsg.startsWith("java.net.UnknownHostException")) {
throw new Exception("Unknown host: " + matcher.group(2));
}
else {
throw new Exception(matcher.group(2));
}
}
else {
if (errorMsg.equals("Auth cancel") || errorMsg.equals("Auth fail")) {
// "Auth cancel" appears when username doesn't exist or password is wrong
throw new Exception("Authorization rejected");
}
else {
throw new Exception(errorMsg.substring(0, 1).toUpperCase() + errorMsg.substring(1));
}
}
} catch (Exception e) {
String errorMsg = e.getMessage();
throw new Exception(errorMsg.substring(0, 1).toUpperCase() + errorMsg.substring(1));
} finally {
if (sftp != null) {
sftp.terminate();
}
}
}