File pemFile, String pemPassword, File passFile, boolean allowedAgentUse)
throws IOException {
Checks.checkNotNull(host, "host");
Checks.checkNotNull(username, "user");
Entry entry = getCacheEntry(username, host, port);
Session session = null;
if (entry != null) {
session = entry.getSession();
}
if (session == null || !session.isConnected()) {
Message.verbose(":: SSH :: connecting to " + host + "...");
try {
JSch jsch = new JSch();
if (port != -1) {
session = jsch.getSession(username, host, port);
} else {
session = jsch.getSession(username, host);
}
if (allowedAgentUse) {
attemptAgentUse(jsch);
}
if (pemFile != null) {
jsch.addIdentity(pemFile.getAbsolutePath(), pemPassword);
}
session.setUserInfo(new CfUserInfo(host, username, userPassword, pemFile,
pemPassword, passFile));
session.setDaemonThread(true);
Properties config = new Properties();
config.setProperty("PreferredAuthentications",
"publickey,keyboard-interactive,password");
session.setConfig(config);
session.connect();
Message.verbose(":: SSH :: connected to " + host + "!");
setSession(username, host, port, session);
} catch (JSchException e) {
if (passFile != null && passFile.exists()) {
passFile.delete();