def get_interface_running_config(self, interface): self._check_if_connected() if interface not in self.get_interfaces(): raise ebay_exceptions.EntityDoesNotExistsException( 'interface %s does not exists' % interface) command = 'show running-config interface %s' % interface current_runing_conf = self._exec_command(command) return current_runing_conf
def get_interface_running_config(self, interface): interfaces = junos_views.junos_config_iface_table(self.device) interfaces.get() if interface not in interfaces: raise exceptions.EntityDoesNotExistsException( 'interface %s does not exists' % interface) command = "show configuration interfaces %s" % interface current_running_conf = self.device.cli(command) return current_running_conf
def _validate_vlan_tags(self, vlan_tags): self._check_if_connected() for v in vlan_tags: vlan_wiri = self.get_vlan(int(v)) if not vlan_wiri: raise ebay_exceptions.EntityDoesNotExistsException( 'vlan %s does not exist' '' % str(v)) if vlan_wiri['status'] == 'suspend': raise ebay_exceptions.\ EntityInSuspendedModeException('vlan_tag %s' % v)
def get_interface_running_config(self, interface): self._check_if_connected() found = False for i in self.get_interfaces(): if interface in i: found = True break if not found: raise ebay_exceptions.EntityDoesNotExistsException( 'interface %s does not exists' % interface) command = 'show running-config interface %s' % interface current_running_config = self._exec_command(command) current_running_config = current_running_config.split('\n') current_running_config = \ [conf.strip() for conf in current_running_config] return current_running_config
def update_interface_label(self, interface, label): self._check_if_connected() interface_name = interface[len("Ethernet"):] cmd = "configure terminal ; interface %s ; description %s ;" \ " commands.append('copy running-config startup-config')" %\ (interface_name, label) self._exec_command(cmd) # post validate label changes interfaces_data = self.get_interfaces_by_name([interface]) if interface in interfaces_data: if interfaces_data[interface]['description'] != label: raise ebay_exceptions.\ PostChangeValidationException("interface label not " "updated. Current value [%s]" % (interfaces_data[interface]['description'])) else: raise ebay_exceptions.EntityDoesNotExistsException( 'interface does not exist ' 'on device.')
def update_interface_label(self, interface, label): interface_number = interface[len("Ethernet"):] commands = ''' configure terminal interface ethernet %s description %s ''' % (interface_number, label) self._exec_command(commands) # post validate label changes interfaces_data = self.get_interfaces_by_name([interface]) if interface in interfaces_data: if interfaces_data[interface]['description'] != label: raise exceptions.PostChangeValidationException( "interface label not updated. Current value [%s]" % (interfaces_data[interface]['description'])) else: raise exceptions.EntityDoesNotExistsException( 'interface does not exist on device.')
def get_vlan_interface_name(self, vlan_tag): vlan_data = self.get_vlan(vlan_tag) if not vlan_data: raise exceptions.EntityDoesNotExistsException( 'vlan %s does not exists' % vlan_tag) # Always pass vlan name on device to get the irb unit cmd = "show configuration vlans %s" % vlan_data['name'] data = self.device.cli(cmd) data = data.split('\n') irb_unit = None for d in data: if 'l3-interface' in d: irb_unit = d.replace('l3-interface', '') irb_unit = irb_unit.replace(';', '') irb_unit = irb_unit.strip() break if not irb_unit: raise exceptions. \ EntityDoesNotExistsException('no l3-interface configured' ' for vlan %s:' % (vlan_tag)) return irb_unit
def get_traffic_on_interface(self, interface_name): self._check_if_connected() found = False for i in self.get_interfaces(): if interface_name in i: found = True break if not found: raise exceptions.EntityDoesNotExistsException( 'interface %s does not exists' % interface_name) cmd = "show interfaces %s | include input rate" % interface_name output = self._exec_command(cmd) output = output.split('\n')[-2] input_bits = int(output.split()[4]) trans_unit = output.split()[5] input_bits = self.convert_to_bits_per_sec(trans_unit, input_bits) cmd = "show interfaces %s | include output rate" % interface_name output = self._exec_command(cmd) output = output.split('\n')[-2] output_bits = int(output.split()[4]) trans_unit = output.split()[5] output_bits = self.convert_to_bits_per_sec(trans_unit, output_bits) return input_bits, output_bits
def get_vlans_on_interface(self, interface): self._check_if_connected() command = "show interface switchport " response_dict = self._exec_command_xml(command) vlans_interface_list = ( response_dict['show']['interface']['switchport']) if '__XML__OPT_Cmd_show_interface_switchport___readonly__' \ in vlans_interface_list: vlans_interface_list = (vlans_interface_list[ '__XML__OPT_Cmd_show_interface_switchport___readonly__'] ['__readonly__']['TABLE_interface']) else: vlans_interface_list = ( vlans_interface_list['__readonly__']['TABLE_interface']) vlan_data = {} if 'ROW_interface' in vlans_interface_list: for vlans_if_list in vlans_interface_list['ROW_interface']: if vlans_if_list['interface'] != interface: continue vlan_list = dict() vlan_list['access_vlan'] = vlans_if_list['access_vlan'] vlan_list['native_vlan'] = vlans_if_list['native_vlan'] vlan_list['trunk_vlans'] = vlans_if_list['trunk_vlans'].\ split(',') vlan_list['switch_port_mode'] = vlans_if_list['oper_mode'] vlan_data[vlans_if_list['interface']] = vlan_list if interface in vlan_data: vlan_data = vlan_data[interface] if vlan_data['switch_port_mode'] == 'access': vlan_data.pop('trunk_vlans') else: vlan_data.pop('access_vlan') return vlan_data raise ebay_exceptions.EntityDoesNotExistsException( 'interface %s does not exists' % interface)
def get_vlans_on_interface(self, interface): result = {} interfaces = junos_views.junos_config_iface_table(self.device) interfaces.get() # interfaces if interface in interfaces: vlan_members = interfaces[interface]['members'] if isinstance(vlan_members, unicode): vlan_members = [str(vlan_members)] else: vlan_members = [str(mem) for mem in vlan_members] result['native_vlan'] = str(interfaces[interface] ['native-vlan-id']) \ if interfaces[interface]['native-vlan-id'] else None if interfaces[interface]['interface-mode'] == "access": result['access_vlan'] = [vlan_members[0]] else: result['trunk_vlans'] = vlan_members result['switch_port_mode'] =\ interfaces[interface]['interface-mode'] return result raise exceptions.EntityDoesNotExistsException( 'interface %s does not exists' % interface)
def get_mac_addresses_on_interface(self, interface_name, vlan=None): """ Returns a lists of dictionaries. Each dictionary represents an entry in the MAC Address Table, having the following keys * mac (string) * interface (string) * vlan (int) * active (boolean) * static (boolean) * moves (int) * last_move (float) Format1: Mac Address Table ------------------------------------------------------------------ Vlan Mac Address Type Ports Moves Last Move ---- ----------- ---- ----- ----- --------- 1 001c.7315.b96c STATIC Router 1 1cc1.de18.9a42 DYNAMIC Et38 1 410 days, 8:1 Total Mac Addresses for this criterion: 1 """ self._check_if_connected() found = False for i in self.get_interfaces(): if interface_name in i: found = True break if not found: raise exceptions.EntityDoesNotExistsException( 'interface %s does not exists' % interface_name) RE_MACTABLE_DEFAULT = r"^" + MAC_REGEX RE_MACTABLE_6500_1 = r"^\*\s+{}\s+{}\s+".\ format(VLAN_REGEX, MAC_REGEX) # 7 fields RE_MACTABLE_6500_2 = r"^{}\s+{}\s+".\ format(VLAN_REGEX, MAC_REGEX) # 6 fields RE_MACTABLE_6500_3 = r"^\s{51}\S+" # Fill down from prior RE_MACTABLE_4500_1 = r"^{}\s+{}\s+".\ format(VLAN_REGEX, MAC_REGEX) # 5 fields RE_MACTABLE_4500_2 = r"^\s{32}\S+" # Fill down from prior RE_MACTABLE_2960_1 = r"^All\s+{}".format(MAC_REGEX) RE_MACTABLE_GEN_1 = r"^{}\s+{}\s+".\ format(VLAN_REGEX, MAC_REGEX) # 4 fields (2960/4500) def _process_mac_fields(vlan, mac, mac_type, interface): """Return proper data for mac address fields.""" if mac_type.lower() in ['self', 'static', 'system']: static = True if vlan.lower() == 'all': vlan = 0 if interface.lower() == 'cpu' or re.search(r'router', interface.lower()) or \ re.search(r'switch', interface.lower()): interface = '' else: static = False if mac_type.lower() in ['dynamic']: active = True else: active = False return { 'mac': napalm_base.helpers.mac(mac), 'interface': interface, 'vlan': int(vlan), 'static': static, 'active': active, 'moves': -1, 'last_move': -1.0 } mac_address_table = [] if vlan: cmd = "show mac address-table vlan %s " % vlan else: cmd = 'show mac address-table interface %s ' % interface_name output = self._exec_command(cmd) # Skip the header lines output = re.split(r'^----.*', output, flags=re.M)[1:] output = "\n".join(output).strip() # Strip any leading astericks output = re.sub(r"^\*", "", output, flags=re.M) fill_down_vlan = fill_down_mac = fill_down_mac_type = '' for line in output.splitlines(): # Cat6500 one off anf 4500 multicast format if (re.search(RE_MACTABLE_6500_3, line) or re.search(RE_MACTABLE_4500_2, line)): interface = line.strip() if ',' in interface: interfaces = interface.split(',') else: interfaces = [] interfaces.append(interface) for single_interface in interfaces: mac_address_table.append( _process_mac_fields(fill_down_vlan, fill_down_mac, fill_down_mac_type, single_interface)) continue line = line.strip() if line == '': continue if re.search(r"^---", line): # Convert any '---' to VLAN 0 line = re.sub(r"^---", "0", line, flags=re.M) # Format1 if re.search(RE_MACTABLE_DEFAULT, line): if len(line.split()) == 4: mac, mac_type, vlan, interface = line.split() mac_address_table.append( _process_mac_fields(vlan, mac, mac_type, interface)) else: raise ValueError("Unexpected output from: {}".format( line.split())) elif (re.search(RE_MACTABLE_6500_1, line) or re.search( RE_MACTABLE_6500_2, line)) \ and len(line.split()) >= 6: if len(line.split()) == 7: _, vlan, mac, mac_type, _, _, interface = line.split() elif len(line.split()) == 6: vlan, mac, mac_type, _, _, interface = line.split() elif len(line.split()) == 9: vlan, mac, mac_type, interface, _, _, _, _, _ = \ line.split() if ',' in interface: interfaces = interface.split(',') fill_down_vlan = vlan fill_down_mac = mac fill_down_mac_type = mac_type for single_interface in interfaces: mac_address_table.append( _process_mac_fields(vlan, mac, mac_type, single_interface)) else: mac_address_table.append( _process_mac_fields(vlan, mac, mac_type, interface)) elif re.search(RE_MACTABLE_4500_1, line) \ and len(line.split()) == 5: vlan, mac, mac_type, _, interface = line.split() mac_address_table.append( _process_mac_fields(vlan, mac, mac_type, interface)) elif re.search(r"^Vlan\s+Mac Address\s+", line): continue elif (re.search(RE_MACTABLE_2960_1, line) or re.search(RE_MACTABLE_GEN_1, line)) \ and len(line.split()) == 4: vlan, mac, mac_type, interface = line.split() if ',' in interface: interfaces = interface.split(',') fill_down_vlan = vlan fill_down_mac = mac fill_down_mac_type = mac_type for single_interface in interfaces: mac_address_table.append( _process_mac_fields(vlan, mac, mac_type, single_interface)) else: mac_address_table.append( _process_mac_fields(vlan, mac, mac_type, interface)) elif re.search(r"Total Mac Addresses", line): continue elif re.search(r"Multicast Entries", line): continue elif re.search(r"vlan.*mac.*address.*type.*", line): continue else: raise ValueError("Unexpected output from: {}".format( repr(line))) result = [] interface_name = interface_name.replace('Ethernet', 'Et') if mac_address_table: for mac_address in mac_address_table: if interface_name == mac_address['interface']: result.append({ 'mac_address': mac_address['mac'], 'vlan': int(mac_address['vlan']) }) if vlan and vlan == mac_address['vlan']: result.append({ 'mac_address': mac_address['mac'], 'vlan': int(mac_address['vlan']) }) return result
def get_vlans_on_interface(self, interface): """ Get vlans configured on an interface :param interface_number: :return: """ # a dict to map the device property names to driver names. device_prop_to_driver_dict = { 'Trunking VLANs Enabled': 'trunk_vlans', 'Trunking Native Mode VLAN': 'native_vlan', 'Access Mode VLAN': 'access_vlan', 'Operational Mode': 'switch_port_mode' } commands = list() commands.append('show interfaces switchport') output = self.device.run_commands(commands, encoding='text', send_enable=self.send_enable)\ .pop(0)['output'] interface_dict = dict() interfaces_output = output.strip().split('\n\n') for each_if in interfaces_output: if each_if.startswith('Name'): interface_props = each_if.split('\n') interface_name = None for each_prop in interface_props: prop_key_and_value = each_prop.split(': ') if len(prop_key_and_value) == 2: prop_key = prop_key_and_value[0].strip() prop_value = prop_key_and_value[1].strip() if prop_key.lower() == 'name': interface_name = prop_value interface_dict[interface_name] = dict() else: if prop_key in device_prop_to_driver_dict: interface_dict[interface_name][ device_prop_to_driver_dict[prop_key]] = \ prop_value interface = interface.replace("hernet", "") if interface not in interface_dict: raise exceptions.EntityDoesNotExistsException( 'interface %s does not exists' % interface) interface_data = interface_dict[interface] access_vlan = interface_data['access_vlan'] native_vlan = interface_data['native_vlan'] if '(' in access_vlan: access_vlan = access_vlan.split('(')[0].strip() interface_data['access_vlan'] = access_vlan if '(' in native_vlan: native_vlan = native_vlan.split('(')[0].strip() interface_data['native_vlan'] = native_vlan if interface_data['switch_port_mode'] == 'static access': interface_data.pop('trunk_vlans') else: interface_data.pop('access_vlan') return interface_data