def get_snmp_information(self): """ Returns a dict of dicts containing SNMP configuration. Each inner dictionary contains these fields * chassis_id (string) * community (dictionary) * contact (string) * location (string) 'community' is a dictionary with community string specific information, as follows: * acl (string) # acl number or name * mode (string) # read-write (rw), read-only (ro) Example:: { 'chassis_id': u'Asset Tag 54670', 'community': { u'private': { 'acl': u'12', 'mode': u'rw' }, u'public': { 'acl': u'11', 'mode': u'ro' }, u'public_named_acl': { 'acl': u'ALLOW-SNMP-ACL', 'mode': u'ro' }, u'public_no_acl': { 'acl': u'N/A', 'mode': u'ro' } }, 'contact' : u'Joe Smith', 'location': u'123 Anytown USA Rack 404' } """ snmp_output = self._send_command('/snmp print') snmp_community_output = self._send_command( '/snmp community print terse') snmp = parse_output(snmp_output) community_list = parse_terse_output(snmp_community_output) community = {} for item in community_list: community.setdefault( item.get('name'), { 'acl': item.get('addresses'), 'mode': u'rw' if item.get('write-access') == 'yes' else u'ro' }) return { 'contact': snmp.get('contact'), 'location': snmp.get('location'), 'community': community, 'chassis_id': '' }
def get_lldp_neighbors_detail(self, interface=""): pass """ Return a detailed view of the LLDP neighbors as a dictionary. Sample output: { 'TenGigE0/0/0/8': [ { 'parent_interface': u'Bundle-Ether8', 'remote_chassis_id': u'8c60.4f69.e96c', 'remote_system_name': u'switch', 'remote_port': u'Eth2/2/1', 'remote_port_description': u'Ethernet2/2/1', 'remote_system_description': u'''huawei os''', 'remote_system_capab': u'B, R', 'remote_system_enable_capab': u'B' } ] } """ lldp_neighbors = {} output = self._send_command('/ip neighbor print terse') if not output: return {} neighbors = parse_terse_output(output) for neighbor in neighbors: ifname = neighbor.get('interface') if not lldp_neighbors.get(ifname): lldp_neighbors[ifname] = list() lldp_neighbors[ifname].append({ 'parent_interface': ifname, 'remote_system_name': neighbor.get('identity'), 'remote_port': neighbor.get('interface-name'), 'remote_chassis_id': neighbor.get('mac-address'), 'remote_system_description': neighbor.get('platform'), 'remote_system_capab': neighbor.get('system-caps'), 'remote_system_enable_capab': neighbor.get('system-caps-enabled'), }) return lldp_neighbors
def get_mac_address_table(self): """ Return the MAC address table. Sample output: [ { "active": true, "interface": "10GE1/0/1", "last_move": -1.0, "mac": "00:00:00:00:00:33", "moves": -1, "static": false, "vlan": 100 }, { "active": false, "interface": "10GE1/0/2", "last_move": -1.0, "mac": "00:00:00:00:00:01", "moves": -1, "static": true, "vlan": 200 } ] """ mac_address_table = [] command = '/interface bridge host print terse' output = self._send_command(command) for host in parse_terse_output(output): mac_address_table.append({ 'mac': cast_mac(host.get('mac-address')), 'interface': host.get('interface'), 'vlan': -1, 'static': True if 'D' not in host.get('_flags') else False, 'active': True if 'X' not in host.get('_flags') else False, 'moves': -1, 'last_move': -1.0 }) return mac_address_table
def get_interfaces_ip(self): """ Get interface IP details. Returns a dictionary of dictionaries. Sample output: { "LoopBack0": { "ipv4": { "192.168.0.9": { "prefix_length": 32 } } }, "Vlanif2000": { "ipv4": { "192.168.200.3": { "prefix_length": 24 }, "192.168.200.6": { "prefix_length": 24 }, "192.168.200.8": { "prefix_length": 24 } }, "ipv6": { "FC00::1": { "prefix_length": 64 } } } } """ interfaces_ip = dict() command = '/ip address print terse' ip_address_output_v4 = self._send_command(command) ip_addresses = parse_terse_output(ip_address_output_v4) for ip_address in ip_addresses: interface = ip_address.get('interface') address, mask = ip_address.get('address').split('/') interfaces_ip.setdefault(interface, {}) \ .setdefault('ipv4', {}) \ .setdefault(cast_ip(address), {}) \ .setdefault('prefix_length', int(mask)) return interfaces_ip
def get_interfaces(self, dynamic=False): """ Get interface details (last_flapped is not implemented). """ interfaces = {} command = '/interface print terse' if not dynamic: command += ' where !dynamic' output = self._send_command(command) if not output: return {} new_interfaces = parse_terse_output(output) for interface in new_interfaces: ifname = interface.get('name') if interface.get('mac-address'): mac_address = cast_mac(interface.get('mac-address')) else: mac_address = '' interfaces.update({ ifname: { 'description': interface.get('comment'), 'is_enabled': True if interface.get('_flags') and 'X' not in interface.get('_flags') else False, 'is_up': True if interface.get('_flags') and 'R' not in interface.get('_flags') else False, 'last_flapped': -1.0, 'mac_address': mac_address, 'speed': -1.0 } }) return interfaces
def get_arp_table(self, vrf=""): """ Get arp table information. Return a list of dictionaries having the following set of keys: * interface (string) * mac (string) * ip (string) * age (float) (not support) Sample output: [ { 'interface' : 'ether1', 'mac' : '5c:5e:ab:da:3c:f0', 'ip' : '172.17.17.1', 'age' : -1 }, { 'interface': 'ether1', 'mac' : '66:0e:94:96:e0:ff', 'ip' : '172.17.17.2', 'age' : -1 } ] """ arp_table = [] output = self._send_command('/ip arp print terse') arps = parse_terse_output(output) for arp in arps: if arp.get('mac-address'): arp_table.append({ 'interface': arp.get('interface'), 'mac': cast_mac(arp.get('mac-address')), 'ip': arp.get('address'), 'age': -1.0, }) return arp_table
def get_users(self): """ Returns a dictionary with the configured users. The keys of the main dictionary represents the username. The values represent the details of the user, represented by the following keys: * level (int) * password (str) * sshkeys (list) *Note: sshkeys on ios is the ssh key fingerprint The level is an integer between 0 and 15, where 0 is the lowest access and 15 represents full access to the device. """ users = {} command = "/user print terse" output = self._send_command(command) for user in parse_terse_output(output): users[user.get('name')] = {"group": user.get('group')} return users
def get_environment(self): """ Return environment details. Sample output: { "cpu": { "0": { "%usage": 18.0 } }, "fans": { "FAN1": { "status": true } }, "memory": { "available_ram": 3884224, "used_ram": 784552 }, "power": { "PWR1": { "capacity": 600.0, "output": 92.0, "status": true } }, "temperature": { "CPU": { "is_alert": false, "is_critical": false, "temperature": 45.0 } } } """ environment = {} system_health = self._send_command('/system health print') system_resources = self._send_command('/system resource print') system_resources_cpu = self._send_command( '/system resource cpu print terse') for key, value in parse_output(system_health).items(): if 'fan' in key: environment.setdefault('fans', {}).setdefault( re.sub(r'(fan\d+).*', r'\1', key), {'status': True}) if 'temperature' in key: environment.setdefault('temperature', {}).setdefault( re.sub(r'(\w+)-temperature(\d+)?.*', r'\1\2', key), { 'temperature': float(value.rstrip('C')), 'is_alert': False, 'is_critical': False, }) if 'psu' in key and 'voltage' in key: environment.setdefault('power', {}).setdefault( re.sub(r'psu(\d+)?.*', r'psu\1', key), { 'status': True, }) cpus = parse_terse_output(system_resources_cpu) for cpu in cpus: environment.setdefault('cpu', {}).setdefault( str(cpu.get('_index')), {'%usage': cpu.get('load').rstrip('%')}) # if resources.get('cpu-load'): # environment.setdefault('cpu', {}).setdefault( # '0', {'%usage': resources.get('cpu-load').rstrip('%') }) resources = parse_output(system_resources) available_ram = human_to_bytes(resources.get('total-memory')) free_ram = human_to_bytes(resources.get('free-memory')) environment.setdefault( 'memory', { 'available_ram': bytes_to_human(available_ram), 'used_ram': bytes_to_human(available_ram - free_ram) }) return environment