示例#1
0
def install_okagent(remote_host,username,password=None, domain=None, install_method=None):
    ''' Installs an okagent to remote host using either winexe or ssh method

    Args:
     remote_host    -- Hostname/IPAddress of remote host
     username       -- Username to use
     password       -- Password to use. If None, try to use ssh keys
     install_method -- Use either "winexe" or "ssh". Leave empty for autodetect.
    Returns:
     exit_status,stdout,stderr
    '''
    if not install_method or install_method == '':
        if network_scan.check_tcp(remote_host, 22, timeout=5):
            install_method = 'ssh'
        elif network_scan.check_tcp(remote_host, 445, timeout=5):
            install_method = 'winexe'

    if install_method == 'ssh':
        return install_nrpe(remote_host=remote_host,username=username, password=password)
    elif install_method == 'winexe':
        return install_nsclient(remote_host=remote_host,username=username, password=password, domain=domain)
    raise OKConfigError("Cannot connect to %s on port 22 or 445. No install method available" % (remote_host))
示例#2
0
def discover_inet(host_name, service):
    # Check if we have already checked this
    if discovery_cache['inet'].has_key("%s,%s" % (host_name, service)):
        return discovery_cache['inet']["%s,%s" % (host_name, service)]

    # services come in 25/tcp or 53/udp
    port, proto = service.split("/", 1)

    # Do the actual scan
    scan_result = network_scan.check_tcp(host_name, int(port))

    # Let's cache it
    discovery_cache['inet']["%s,%s" % (host_name, service)] = scan_result

    return scan_result
示例#3
0
def install_nsclient(remote_host, domain, username, password):
    """ Logs into remote (windows) host and installs NSClient.

    Args:
     remote_host -- Hostname/IPAddress of remote host
     username -- Name of a user with administrative privileges on the remote host
     password -- Password to use
     domain -- Windows Domain

    Returns:
     True if operation was successful. Otherwise False
    """
    if not network_scan.check_tcp(remote_host, 445, timeout=5):
        raise OKConfigError('Cannot reach remote_host on port 445, aborting...')

    result = pynag.Utils.runCommand("%s/install_nsclient.sh '%s' --domain '%s' --user '%s' --password '%s'" % (config.nsclient_installfiles,remote_host,domain,username,password))
    return result
示例#4
0
def install_nrpe(remote_host, username, password=None):
    """ Logs into remote (unix) host and install nrpe-client.

    Args:
     remote_host -- Hostname/IPAddress of remote host
     username -- Username to use
     password -- Password to use. If None, try to use ssh keys

    Returns:
     True if operation was successful.
    """
    if not network_scan.check_tcp(remote_host, 22, timeout=5):
        raise OKConfigError('Cannot reach remote_host on port 22, aborting...')
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
    ssh.connect(remote_host, username=username, password=password)


    # Try a test command on the remote server to see if we are connected
    test_command = 'echo -e connection test'
    chan = ssh.get_transport().open_session()
    chan.exec_command( test_command )
    exit_status = chan.recv_exit_status()
    stdout = chan.recv(-1).strip()
    stderr = chan.recv_stderr(-1).strip()
    if exit_status != 0:
        ssh.close()
        raise OKConfigError('Exit code %s when trying to run test command\noutput: %s\nstderr: %s' % (chan.exit_status, stdout,stderr) )

    # Uploading install script to remote server
    sftp = ssh.open_sftp()
    sftp.put(config.install_nrpe_script, 'install_nrpe.sh')

    # Executing remote script
    # We need to do some hoola-hoops to get the exit code
    chan = ssh.get_transport().open_session()
    chan.exec_command('bash install_nrpe.sh')
    exit_status = chan.recv_exit_status()
    stdout = chan.recv(-1).strip()
    stderr = chan.recv_stderr(-1).strip()

    return exit_status,stdout,stderr
示例#5
0
def install_nsclient(remote_host, domain, username, password):
    """ Logs into remote (windows) host and installs NSClient.

    Args:
     remote_host -- Hostname/IPAddress of remote host
     username -- Name of a user with administrative privileges on the remote host
     password -- Password to use

    Returns:
     True if operation was successful. Otherwise False
    """
    if not network_scan.check_tcp(remote_host, 445, timeout=5):
        raise OKConfigError('Cannot reach remote_host on port 445, aborting...')

    # Try to authenticate with remote host and run a test command
    authcommand = 'winexe --reinstall -U "%s/%s%%%s" "//%s" "cmd /c echo test"' % (domain,username,password,remote_host)
    result = helper_functions.runCommand(authcommand)
    if result[0] != 0:
        raise OKConfigError('Cannot authenticate')
    result = helper_functions.runCommand("%s/install_nsclient.sh '%s' --domain '%s' --user '%s' --password '%s'" % (config.nsclient_installfiles,remote_host,domain,username,password))
    return result