Esempio n. 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 | grep "lease" | tail -n 1'.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))

    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)
Esempio n. 2
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)
Esempio n. 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]
Esempio n. 4
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]
Esempio n. 5
0
    def get_host_ssh(self, obj_node, username, password, **kwargs):
        '''
        Build a SSH connection to host in virtual node
        '''
        if 'qemu_ip' in kwargs:
            self.log('INFO', 'Build host SSH to {}@{}'.format(username, kwargs['qemu_ip']))
            conn = CSSH(ip=kwargs['qemu_ip'], username=username, password=password, port=kwargs.get('port', 22))
            conn.connect()
            return conn

        # To get host's IP address:
        #     - Get MAC
        #     - Get IP via DHCP lease on DHCP server
        #     - Check if IP is available
        #     - SSH to host
        if 'dhcp_server' in kwargs and 'dhcp_user' in kwargs and 'dhcp_pass' in kwargs:
            self.log('INFO', 'Query host IP from DHCP server {} ...'.format(kwargs['dhcp_server']))

            # Get IP
            rsp_qemu = obj_node.get_bmc().ssh.remote_shell('ps aux | grep qemu')
            if rsp_qemu['exitcode'] != 0:
                raise Exception('Fail to get node {} qemu MAC address'.format(obj_node.get_name()))
            qemu_mac = rsp_qemu['stdout'].split('mac=')[1].split(' ')[0].lower()
            self.log('INFO', 'Node {} host MAC address is: {}'.format(obj_node.get_name(), qemu_mac))

            self.log('INFO', 'Wait node {} host IP address in up to 300s ...'.format(obj_node.get_name()))
            time_start = time.time()
            qemu_ip = ''
            while time.time() - time_start < 300:
                try:
                    qemu_ip = dhcp_query_ip(server=kwargs['dhcp_server'],
                                            username=kwargs['dhcp_user'],
                                            password=kwargs['dhcp_pass'],
                                            mac=qemu_mac)
                    if qemu_ip == obj_node.get_bmc().get_ip():
                        self.log('WARNING', 'IP lease for mac {} is {} to node {}\'s BMC, '
                                            'waiting for host IP lease, retry after 30s ...'.
                                 format(qemu_mac, qemu_ip, obj_node.get_name()))
                        time.sleep(30)
                        continue
                    rsp = os.system('ping -c 1 {}'.format(qemu_ip))
                    if rsp != 0:
                        self.log('WARNING', 'Find an IP {} lease for mac {} on node {}, '
                                            'but this IP address is not available, retry after 30s ...'.
                                 format(qemu_ip, qemu_mac, obj_node.get_name()))
                        time.sleep(30)
                        continue
                    else:
                        self.log('INFO', 'Node {} host get IP {}'.format(obj_node.get_name(), qemu_ip))
                        break
                except Exception, e:
                    self.log('WARNING', 'Fail to get node {}\'s host IP: {}'.format(obj_node.get_name(), e))
                    time.sleep(30)

            if not qemu_ip:
                raise Exception('Fail to get node {}\'s host IP in 300s, '
                                'check if vSwith\'s promiscuous mode is "Accept"'.
                                format(obj_node.get_name()))

            conn = CSSH(ip=qemu_ip,
                        username=kwargs.get('host_username', 'june'),
                        password=kwargs.get('host_password', '111111'),
                        port=kwargs.get('port', 22))
            conn.connect()
            return conn