def _get_hosts_by_ids(self, ids_string): if ids_string == 'all': return self.hosts.copy() ids = ids_string.split(',') hosts = set() for id_ in ids: is_mac = netutils.validate_mac_address(id_) is_ip = netutils.validate_ip_address(id_) is_id_ = id_.isdigit() if not is_mac and not is_ip and not is_id_: IO.error('invalid identifier(s): \'{}\'.'.format(ids_string)) return if is_mac or is_ip: found = False for host in self.hosts: if host.mac == id_.lower() or host.ip == id_: found = True hosts.add(host) break if not found: IO.error('no host matching {}{}{}.'.format(IO.Fore.LIGHTYELLOW_EX, id_, IO.Style.RESET_ALL)) return else: id_ = int(id_) if len(self.hosts) == 0 or id_ not in range(len(self.hosts)): IO.error('no host with id {}{}{}.'.format(IO.Fore.LIGHTYELLOW_EX, id_, IO.Style.RESET_ALL)) return hosts.add(self.hosts[id_]) return hosts
def _add_handler(self, args): """ Handles 'add' command-line argument Adds custom host to host list """ ip = args.ip if not netutils.validate_ip_address(ip): IO.error('invalid ip address.') return if args.mac: mac = args.mac if not netutils.validate_mac_address(mac): IO.error('invalid mac address.') return else: mac = netutils.get_mac_by_ip(self.interface, ip) if mac is None: IO.error('unable to resolve mac address. specify manually (--mac).') return name = None try: host_info = socket.gethostbyaddr(ip) name = None if host_info is None else host_info[0] except socket.herror: pass host = Host(ip, mac, name) if host in self.hosts: IO.error('host does already exist.') return self.hosts.append(host) IO.ok('host added.')
def process_arguments(args): """ Processes the specified command-line arguments, adds them to a named tuple and returns. Executes actions specified in the command line, e.g. flush network settings """ if args.interface is None: interface = netutils.get_default_interface() if interface is None: IO.error('default interface could not be resolved. specify manually (-i).') return else: interface = args.interface if not netutils.exists_interface(interface): IO.error('interface {}{}{} does not exist.'.format(IO.Fore.LIGHTYELLOW_EX, interface, IO.Style.RESET_ALL)) return IO.ok('interface: {}{}{}'.format(IO.Fore.LIGHTYELLOW_EX, interface, IO.Style.RESET_ALL)) if args.gateway_ip is None: gateway_ip = netutils.get_default_gateway() if gateway_ip is None: IO.error('default gateway address could not be resolved. specify manually (-g).') return else: gateway_ip = args.gateway_ip IO.ok('gateway ip: {}{}{}'.format(IO.Fore.LIGHTYELLOW_EX, gateway_ip, IO.Style.RESET_ALL)) if args.gateway_mac is None: gateway_mac = netutils.get_mac_by_ip(interface, gateway_ip) if gateway_mac is None: IO.error('gateway mac address could not be resolved.') return else: if netutils.validate_mac_address(args.gateway_mac): gateway_mac = args.gateway_mac.lower() else: IO.error('gateway mac is invalid.') return IO.ok('gateway mac: {}{}{}'.format(IO.Fore.LIGHTYELLOW_EX, gateway_mac, IO.Style.RESET_ALL)) if args.netmask is None: netmask = netutils.get_default_netmask(interface) if netmask is None: IO.error('netmask could not be resolved. specify manually (-n).') return else: netmask = args.netmask IO.ok('netmask: {}{}{}'.format(IO.Fore.LIGHTYELLOW_EX, netmask, IO.Style.RESET_ALL)) if args.flush: netutils.flush_network_settings(interface) IO.spacer() IO.ok('flushed network settings') return InitialArguments(interface=interface, gateway_ip=gateway_ip, gateway_mac=gateway_mac, netmask=netmask)