@@ -20,12 +20,15 @@ package org.apache.tools.ant.taskdefs.optional.ssh;
import java.io.File;
import java.io.File;
import java.io.IOException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.jcraft.jsch.ConfigRepository;
import com.jcraft.jsch.ConfigRepository;
import com.jcraft.jsch.OpenSSHConfig;
import com.jcraft.jsch.OpenSSHConfig;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Environment.Variable;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.JSchException;
@@ -50,6 +53,7 @@ public abstract class SSHBase extends Task implements LogListener {
private String sshConfig;
private String sshConfig;
private int serverAliveCountMax = 3;
private int serverAliveCountMax = 3;
private int serverAliveIntervalSeconds = 0;
private int serverAliveIntervalSeconds = 0;
private final Map<String, String> additionalConfig = new HashMap<>();
/**
/**
* Constructor for SSHBase.
* Constructor for SSHBase.
@@ -247,6 +251,10 @@ public abstract class SSHBase extends Task implements LogListener {
return port;
return port;
}
}
public void addConfiguredAdditionalConfig(final Variable v) {
additionalConfig.put(v.getKey(), v.getValue());
}
/**
/**
* Initialize the task.
* Initialize the task.
* This initializes the known hosts and sets the default port.
* This initializes the known hosts and sets the default port.
@@ -268,7 +276,7 @@ public abstract class SSHBase extends Task implements LogListener {
if (!new File(sshConfig).exists()) {
if (!new File(sshConfig).exists()) {
throw new BuildException("The SSH configuration file specified doesn't exist: " + sshConfig);
throw new BuildException("The SSH configuration file specified doesn't exist: " + sshConfig);
}
}
log("Loading SSH configuration file " + sshConfig, Project.MSG_DEBUG);
log("Loading SSH configuration file " + sshConfig, Project.MSG_DEBUG);
ConfigRepository.Config config = null;
ConfigRepository.Config config = null;
try {
try {
@@ -276,15 +284,15 @@ public abstract class SSHBase extends Task implements LogListener {
} catch (IOException e) {
} catch (IOException e) {
throw new BuildException("Failed to load the SSH configuration file " + sshConfig, e);
throw new BuildException("Failed to load the SSH configuration file " + sshConfig, e);
}
}
if (config.getHostname() != null) {
if (config.getHostname() != null) {
host = config.getHostname();
host = config.getHostname();
}
}
if (userInfo.getName() == null) {
if (userInfo.getName() == null) {
userInfo.setName(config.getUser());
userInfo.setName(config.getUser());
}
}
if (userInfo.getKeyfile() == null) {
if (userInfo.getKeyfile() == null) {
log("Using SSH key file " + config.getValue("IdentityFile") + " for host " + host, Project.MSG_INFO);
log("Using SSH key file " + config.getValue("IdentityFile") + " for host " + host, Project.MSG_INFO);
userInfo.setKeyfile(config.getValue("IdentityFile"));
userInfo.setKeyfile(config.getValue("IdentityFile"));
@@ -332,6 +340,11 @@ public abstract class SSHBase extends Task implements LogListener {
session.setServerAliveInterval(getServerAliveIntervalSeconds() * 1000);
session.setServerAliveInterval(getServerAliveIntervalSeconds() * 1000);
}
}
additionalConfig.forEach((k,v) -> {
log("Setting additional config value " + k, Project.MSG_DEBUG);
session.setConfig(k, v);
});
log("Connecting to " + host + ":" + port);
log("Connecting to " + host + ":" + port);
session.connect();
session.connect();
return session;
return session;