* @return session or null if not successful
*/
public Session getSession(String host, int port, String username, String userPassword,
File pemFile, String pemPassword, File passFile) throws IOException {
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 (pemFile != null) {
jsch.addIdentity(pemFile.getAbsolutePath(), pemPassword);
}
session.setUserInfo(new CfUserInfo(host, username, userPassword, pemFile,
pemPassword, passFile));
session.connect();
Message.verbose(":: SSH :: connected to " + host + "!");
setSession(username, host, port, session);
} catch (JSchException e) {
if (passFile.exists()) {
passFile.delete();