| Method from com.sshtools.ant.Ssh Detail: |
protected void connectAndAuthenticate() throws BuildException {
if (sshtoolsHome != null) {
System.setProperty("sshtools.home", sshtoolsHome);
}
log("Initializing J2SSH");
try {
ConfigurationLoader.initialize(false);
log("Creating connection to " + host + ":" + String.valueOf(port));
if (ssh == null) {
ssh = new SshClient();
SshConnectionProperties properties = new SshConnectionProperties();
properties.setHost(host);
properties.setPort(port);
properties.setUsername(username);
if (cipher != null) {
if (SshCipherFactory.getSupportedCiphers().contains(cipher)) {
properties.setPrefSCEncryption(cipher);
properties.setPrefCSEncryption(cipher);
} else {
this.log(cipher +
" is not a supported cipher, using default " +
SshCipherFactory.getDefaultCipher());
}
}
if (mac != null) {
if (SshHmacFactory.getSupportedMacs().contains(mac)) {
properties.setPrefCSMac(mac);
properties.setPrefSCMac(mac);
} else {
this.log(mac +
" is not a supported mac, using default " +
SshHmacFactory.getDefaultHmac());
}
}
log("Connecting....");
ssh.connect(properties,
new AbstractKnownHostsKeyVerification(
new File(System.getProperty("user.home"),
".ssh" + File.separator + "known_hosts").getAbsolutePath()) {
public void onUnknownHost(String hostname,
SshPublicKey key) throws InvalidHostFileException {
if (Ssh.this.verifyhost) {
if (key.getFingerprint().equalsIgnoreCase(Ssh.this.fingerprint)) {
allowHost(hostname, key, always);
}
} else {
allowHost(hostname, key, always);
}
}
public void onHostKeyMismatch(String hostname,
SshPublicKey allowed, SshPublicKey supplied)
throws InvalidHostFileException {
if (Ssh.this.verifyhost) {
if (supplied.getFingerprint().equalsIgnoreCase(Ssh.this.fingerprint)) {
allowHost(hostname, supplied, always);
}
} else {
allowHost(hostname, supplied, always);
}
}
public void onDeniedHost(String host) {
log("The server host key is denied!");
}
});
int result;
boolean authenticated = false;
log("Authenticating " + username);
if (keyfile != null) {
log("Performing public key authentication");
PublicKeyAuthenticationClient pk = new PublicKeyAuthenticationClient();
// Open up the private key file
SshPrivateKeyFile file = SshPrivateKeyFile.parse(new File(
keyfile));
// If the private key is passphrase protected then ask for the passphrase
if (file.isPassphraseProtected() && (passphrase == null)) {
throw new BuildException(
"Private key file is passphrase protected, please supply a valid passphrase!");
}
// Get the key
SshPrivateKey key = file.toPrivateKey(passphrase);
pk.setUsername(username);
pk.setKey(key);
// Try the authentication
result = ssh.authenticate(pk);
if (result == AuthenticationProtocolState.COMPLETE) {
authenticated = true;
} else if (result == AuthenticationProtocolState.PARTIAL) {
log(
"Public key authentication completed, attempting password authentication");
} else {
throw new BuildException(
"Public Key Authentication failed!");
}
}
if ((password != null) && (authenticated == false)) {
log("Performing password authentication");
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername(username);
pwd.setPassword(password);
result = ssh.authenticate(pwd);
if (result == AuthenticationProtocolState.COMPLETE) {
log("Authentication complete");
} else if (result == AuthenticationProtocolState.PARTIAL) {
throw new BuildException(
"Password Authentication succeeded but further authentication required!");
} else {
throw new BuildException(
"Password Authentication failed!");
}
}
}
} catch (IOException ex) {
throw new BuildException(ex);
}
}
|
public SshSubTask createExec() {
SshSubTask task = new Exec();
tasks.addElement(task);
return task;
}
|
public SshSubTask createSftp() {
SshSubTask task = new Sftp();
tasks.addElement(task);
return task;
}
|
public SshSubTask createShell() {
SshSubTask task = new Shell();
tasks.addElement(task);
return task;
}
|
protected void disconnect() throws BuildException {
try {
log("Disconnecting from " + host);
ssh.disconnect();
} catch (Exception ex) {
throw new BuildException(ex);
}
}
|
public void execute() throws BuildException {
validate();
connectAndAuthenticate();
executeSubTasks();
disconnect();
}
|
protected void executeSubTasks() throws BuildException {
Iterator it = tasks.iterator();
SshSubTask task;
while (it.hasNext()) {
task = (SshSubTask) it.next();
task.setParent(this);
task.execute(ssh);
}
}
|
protected boolean hasMoreSftpTasks() {
Iterator it = tasks.iterator();
while (it.hasNext()) {
if (it.next().getClass().equals(Sftp.class)) {
return true;
}
}
return false;
}
|
public void setAlways(boolean always) {
this.always = always;
}
|
public void setCipher(String cipher) {
this.cipher = cipher;
}
|
public void setFingerprint(String fingerprint) {
this.fingerprint = fingerprint;
}
|
public void setHost(String host) {
this.host = host;
}
|
public void setKeyfile(String keyfile) {
this.keyfile = keyfile;
}
|
public void setLogfile(String logfile) {
this.logfile = logfile;
}
|
public void setMac(String mac) {
this.mac = mac;
}
|
public void setNewline(String newline) {
this.newline = newline;
}
|
public void setPassphrase(String passphrase) {
this.passphrase = passphrase;
}
|
public void setPassword(String password) {
this.password = password;
}
|
public void setPort(int port) {
this.port = port;
}
|
public void setSshtoolshome(String sshtoolsHome) {
this.sshtoolsHome = sshtoolsHome;
}
|
public void setUsername(String username) {
this.username = username;
}
|
public void setVerifyhost(boolean verifyhost) {
this.verifyhost = verifyhost;
}
|
protected void validate() throws BuildException {
if (host == null) {
throw new BuildException("You must provide a host to connect to!");
}
if (username == null) {
throw new BuildException(
"You must supply a username for authentication!");
}
if ((password == null) && (keyfile == null)) {
throw new BuildException(
"You must supply either a password or keyfile/passphrase to authenticate!");
}
if (verifyhost && (fingerprint == null)) {
throw new BuildException(
"Public key fingerprint required to verify the host");
}
}
|