Search in sources :

Example 1 with Ssh

use of org.opennms.netmgt.provision.support.ssh.Ssh in project opennms by OpenNMS.

the class SshClient method connect.

/** {@inheritDoc} */
@Override
public void connect(final InetAddress address, final int port, final int timeout) throws Exception {
    Map<String, ?> emptyMap = Collections.emptyMap();
    TimeoutTracker tracker = new TimeoutTracker(emptyMap, SshClient.DEFAULT_RETRY, timeout);
    String banner = m_banner;
    String match = m_match;
    String clientBanner = m_clientBanner;
    PollStatus ps = PollStatus.unavailable();
    Ssh ssh = new Ssh(address, port, tracker.getConnectionTimeout());
    ssh.setClientBanner(clientBanner);
    Pattern regex = null;
    if (match == null && (banner == null || banner.equals("*"))) {
        regex = null;
    } else if (match != null) {
        regex = Pattern.compile(match);
    } else if (banner != null) {
        regex = Pattern.compile(banner);
    }
    for (tracker.reset(); tracker.shouldRetry() && !ps.isAvailable(); tracker.nextAttempt()) {
        try {
            ps = ssh.poll(tracker);
        } catch (InsufficientParametersException e) {
            LOG.error("Caught InsufficientParametersException: {}", e.getMessage(), e);
            break;
        }
    }
    if (regex != null && ps.isAvailable()) {
        String response = ssh.getServerBanner();
        if (response == null) {
            ps = PollStatus.unavailable("server closed connection before banner was recieved.");
        }
        if (!regex.matcher(response).find()) {
            // Got a response but it didn't match... no need to attempt
            // retries
            LOG.debug("isServer: NON-matching response='{}'", response);
            ps = PollStatus.unavailable("server responded, but banner did not match '" + banner + "'");
        } else {
            LOG.debug("isServer: matching response='{}'", response);
        }
    }
    PollStatus result = ps;
    m_isAvailable = result.isAvailable();
}
Also used : Pattern(java.util.regex.Pattern) PollStatus(org.opennms.netmgt.poller.PollStatus) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) Ssh(org.opennms.netmgt.provision.support.ssh.Ssh) InsufficientParametersException(org.opennms.netmgt.provision.support.ssh.InsufficientParametersException)

Aggregations

Pattern (java.util.regex.Pattern)1 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)1 PollStatus (org.opennms.netmgt.poller.PollStatus)1 InsufficientParametersException (org.opennms.netmgt.provision.support.ssh.InsufficientParametersException)1 Ssh (org.opennms.netmgt.provision.support.ssh.Ssh)1