def arp(self): for entry in self.api('/ip/arp/print'): if 'mac-address' not in entry: continue else: yield { 'interface': entry['interface'], 'mac': cast_mac(entry['mac-address']), 'ip': cast_ip(entry['address']), 'age': float(-1), }
def convert_arp_table(table): for entry in table: if 'mac-address' not in entry: continue yield { 'interface': entry['interface'], 'mac': cast_mac(entry['mac-address']), 'ip': cast_ip(entry['address']), 'age': float(-1), }
def ping(self, destination, source=C.PING_SOURCE, ttl=C.PING_TTL, timeout=C.PING_TIMEOUT, size=C.PING_SIZE, count=C.PING_COUNT, vrf=C.PING_VRF): params = { 'count': count, 'address': destination, 'ttl': ttl, 'size': size, 'count': count, } if source: params['src-address'] = source if vrf: params['routing-table'] = vrf results = self.api('/ping', **params) ping_results = { 'probes_sent': max(row['sent'] for row in results), 'packet_loss': max(row['packet-loss'] for row in results), 'rtt_min': min( float(row.get('min-rtt', '-1ms').replace('ms', '')) for row in results), 'rtt_max': max( float(row.get('max-rtt', '-1ms').replace('ms', '')) for row in results), # Last result has calculated avg 'rtt_avg': float(results[-1].get('avg-rtt', '-1ms').replace('ms', '')), 'rtt_stddev': float(-1), 'results': [] } for row in results: ping_results['results'].append({ 'ip_address': cast_ip(row['host']), 'rtt': float(row.get('time', '-1ms').replace('ms', '')), }) return dict(success=ping_results)
def get_ipv6_neighbors_table(self): ipv6_neighbors_table = [] for entry in self.api('/ipv6/neighbor/print'): if 'mac-address' not in entry: continue ipv6_neighbors_table.append({ 'interface': entry['interface'], 'mac': cast_mac(entry['mac-address']), 'ip': cast_ip(entry['address']), 'age': float(-1), 'state': entry['status'] }) return ipv6_neighbors_table
def iface_addresses(rows, ifname): ''' Return every found address and prefix length for given interface. example: { '192.168.1.1': {'prefix_length': 24} } ''' found = (row['address'].split('/', 1) for row in rows if row['interface'] == ifname) pairs = ((cast_ip(address), int(prefix_length)) for address, prefix_length in found) return dict((address, dict(prefix_length=length)) for address, length in pairs)
def get_arp_table(self): arp_table = [] for entry in self.api('/ip/arp/print'): if 'mac-address' not in entry: continue arp_table.append( { 'interface': entry['interface'], 'mac': cast_mac(entry['mac-address']), 'ip': cast_ip(entry['address']), 'age': float(-1), } ) return arp_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_ipv6_neighbors_table(self): ipv6_neighbors_table = [] for entry in self.api('/ipv6/neighbor/print'): if 'mac-address' not in entry: continue ipv6_neighbors_table.append( { 'interface': entry['interface'], 'mac': cast_mac(entry['mac-address']), 'ip': cast_ip(entry['address']), 'age': float(-1), 'state': entry['status'] } ) return ipv6_neighbors_table
def ping(self, destination, source=C.PING_SOURCE, ttl=C.PING_TTL, timeout=C.PING_TIMEOUT, size=C.PING_SIZE, count=C.PING_COUNT, vrf=C.PING_VRF): params = { 'count': count, 'address': destination, 'ttl': ttl, 'size': size, 'count': count, } if source: params['src-address'] = source if vrf: params['routing-table'] = vrf results = self.api('/ping', **params) ping_results = { 'probes_sent': max(row['sent'] for row in results), 'packet_loss': max(row['packet-loss'] for row in results), 'rtt_min': min(float(row.get('min-rtt', '-1ms').replace('ms', '')) for row in results), 'rtt_max': max(float(row.get('max-rtt', '-1ms').replace('ms', '')) for row in results), # Last result has calculated avg 'rtt_avg': float(results[-1].get('avg-rtt', '-1ms').replace('ms', '')), 'rtt_stddev': float(-1), 'results': [] } for row in results: ping_results['results'].append( { 'ip_address': cast_ip(row['host']), 'rtt': float(row.get('time', '-1ms').replace('ms', '')), } ) return dict(success=ping_results)