예제 #1
0
def dhcp_query_ip(server, username, password, mac):
    '''
    Get IP address according to the MAC address, from target
    server, wich credential username:password
    :param server: server name
    :param username: server username
    :param password: server password
    :param mac: MAC address of the target node
    :return: IP address of the target node
    '''
    conn = CSSH(ip=server,
                username=username,
                password=password)
    if not conn.connect():
        raise Exception('Fail to connect to server {} to query IP'.format(server))

    rsp = conn.remote_shell('grep -A 2 -B 7 "{}" /var/lib/dhcp/dhcpd.leases | tail -n 10'.format(mac))
    if rsp['exitcode'] != 0:
        conn.disconnect()
        raise Exception('Fail to get response from server {} to query IP\n{}'.
                        format(server, json.dumps(rsp, indent=4)))
    if not rsp['stdout']:
        conn.disconnect()
        raise Exception('Find no DHCP lease information for MAC: {}'.format(mac))

    if "binding state active" not in rsp['stdout']:
        conn.disconnect()
        raise Exception('Find no active DHCP lease information for MAC: {}'.format(mac))

    p_ip = r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
    p = re.search(p_ip, rsp['stdout'])
    if p:
        if is_valid_ip(p.group(1)):
            conn.disconnect()
            return p.group(1)
예제 #2
0
파일: Apps.py 프로젝트: JuneZhou19/test
def arp_query_ip(server, username, password, mac):
    '''
    Get IP address according to the MAC address, from target
    server, wich credential username:password
    :param server: server name
    :param username: server username
    :param password: server password
    :param mac: MAC address of the target node
    :return: IP address of the target node
    '''
    conn = CSSH(ip=server,
                username=username,
                password=password)
    if not conn.connect():
        raise Exception('Fail to connect to server {} to query IP'.format(server))

    rsp = conn.remote_shell('arp -an | grep {}'.format(mac))
    if rsp['exitcode'] != 0:
        conn.disconnect()
        raise Exception('Fail to get response from server {} to query IP\n{}'.
                        format(server, json.dumps(rsp, indent=4)))

    p_ip = r'\((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\)'
    list_info = rsp['stdout'].split('\n')
    list_ip = []
    for each_info in list_info:
        p = re.search(p_ip, each_info)
        if p:
            if is_valid_ip(p.group(1)):
                list_ip.append(p.group(1))
    if len(list_ip) != 1:
        conn.disconnect()
        raise Exception('MAC conflict for IP: {}'.format(list_ip))
    else:
        conn.disconnect()
        return list_ip[0]
예제 #3
0
def arp_query_ip(server, username, password, mac):
    '''
    Get IP address according to the MAC address, from target
    server, wich credential username:password
    :param server: server name
    :param username: server username
    :param password: server password
    :param mac: MAC address of the target node
    :return: IP address of the target node
    '''
    conn = CSSH(ip=server,
                username=username,
                password=password)
    if not conn.connect():
        raise Exception('Fail to connect to server {} to query IP'.format(server))

    rsp = conn.remote_shell('arp -an | grep {}'.format(mac))
    if rsp['exitcode'] != 0:
        conn.disconnect()
        raise Exception('Fail to get response from server {} to query IP\n{}'.
                        format(server, json.dumps(rsp, indent=4)))

    p_ip = r'\((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\)'
    list_info = rsp['stdout'].split('\n')
    list_ip = []
    for each_info in list_info:
        p = re.search(p_ip, each_info)
        if p:
            if is_valid_ip(p.group(1)):
                list_ip.append(p.group(1))
    if len(list_ip) != 1:
        conn.disconnect()
        raise Exception('MAC conflict for IP: {}'.format(list_ip))
    else:
        conn.disconnect()
        return list_ip[0]