def _ifconf_staticip(self): def _validate(addr, netmask, gateway, nameservers): """Validate Static IP form parameters. Returns an empty array on success, an array of strings describing errors otherwise""" errors = [] if not addr: errors.append("No IP address provided") elif not ipaddr.is_legal_ip(addr): errors.append("Invalid IP address: %s" % addr) if not netmask: errors.append("No netmask provided") elif not ipaddr.is_legal_ip(netmask): errors.append("Invalid netmask: %s" % netmask) for nameserver in nameservers: if nameserver and not ipaddr.is_legal_ip(nameserver): errors.append("Invalid nameserver: %s" % nameserver) if len(nameservers) != len(set(nameservers)): errors.append("Duplicate nameservers specified") if errors: return errors if gateway: if not ipaddr.is_legal_ip(gateway): return [ "Invalid gateway: %s" % gateway ] else: iprange = ipaddr.IPRange(addr, netmask) if gateway not in iprange: return [ "Gateway (%s) not in IP range (%s)" % (gateway, iprange) ] return [] addr, netmask, gateway, nameservers = ifutil.get_ipconf(self.ifname) input = [addr, netmask, gateway] input.extend(nameservers) # include minimum 2 nameserver fields and 1 blank one if len(input) < 4: input.append('') if input[-1]: input.append('') field_width = 30 field_limit = 15 while 1: fields = [ ("IP Address", input[0], field_width, field_limit), ("Netmask", input[1], field_width, field_limit), ("Default Gateway", input[2], field_width, field_limit), ] for i in range(len(input[3:])): fields.append(("Name Server", input[3+i], field_width, field_limit)) text = "Static IP configuration (%s)" % self.ifname retcode, input = self.console.form("Network settings", text, fields) if retcode is not self.OK: break # remove any whitespaces the user might of included for i in range(len(input)): input[i] = input[i].strip() # unconfigure the nic if all entries are empty if not input[0] and not input[1] and not input[2] and not input[3]: ifutil.unconfigure_if(self.ifname) break addr, netmask, gateway = input[:3] nameservers = input[3:] for i in range(nameservers.count('')): nameservers.remove('') err = _validate(addr, netmask, gateway, nameservers) if err: err = "\n".join(err) else: err = ifutil.set_static(self.ifname, addr, netmask, gateway, nameservers) if not err: break self.console.msgbox("Error", err) return "ifconf"
def _ifconf_staticip(self): def _validate(addr, netmask, gateway, nameservers): """Validate Static IP form parameters. Returns an empty array on success, an array of strings describing errors otherwise""" errors = [] if not addr: errors.append("No IP address provided") elif not ipaddr.is_legal_ip(addr): errors.append("Invalid IP address: %s" % addr) if not netmask: errors.append("No netmask provided") elif not ipaddr.is_legal_ip(netmask): errors.append("Invalid netmask: %s" % netmask) for nameserver in nameservers: if nameserver and not ipaddr.is_legal_ip(nameserver): errors.append("Invalid nameserver: %s" % nameserver) if len(nameservers) != len(set(nameservers)): errors.append("Duplicate nameservers specified") if errors: return errors if gateway: if not ipaddr.is_legal_ip(gateway): return ["Invalid gateway: %s" % gateway] else: iprange = ipaddr.IPRange(addr, netmask) if gateway not in iprange: return [ "Gateway (%s) not in IP range (%s)" "" % (gateway, iprange) ] return [] warnings = [] try: addr, netmask, gateway, nameservers = ifutil.get_ipconf( self.ifname, True) except CalledProcessError: warnings.append( '`route -n` returned non-0 exit code! (unable to get gateway)') addr, netmask, gateway, nameservers = None, None, '', [] except netinfo.NetInfoError: warnings.append('failed to find default gateway!') addr, netmask, gateway, nameservers = None, None, '', [] if addr is None: warnings.append('failed to assertain current address!') addr = '' if netmask is None: warnings.append('failed to assertain current netmask!') netmask = '' if warnings: warnings.append('\nWill leave relevant fields blank') if warnings: self.console.msgbox("Warning", '\n'.join(warnings)) input = [addr, netmask, gateway] input.extend(nameservers) # include minimum 2 nameserver fields and 1 blank one if len(input) < 4: input.append('') if input[-1]: input.append('') field_width = 30 field_limit = 15 while 1: fields = [ ("IP Address", input[0], field_width, field_limit), ("Netmask", input[1], field_width, field_limit), ("Default Gateway", input[2], field_width, field_limit), ] for i in range(len(input[3:])): fields.append( ("Name Server", input[3 + i], field_width, field_limit)) fields = format_fields(fields) text = "Static IP configuration (%s)" % self.ifname retcode, input = self.console.form("Network settings", text, fields) if retcode is not self.OK: break # remove any whitespaces the user might of included for i in range(len(input)): input[i] = input[i].strip() # unconfigure the nic if all entries are empty if not input[0] and not input[1] and not input[2] and not input[3]: ifutil.unconfigure_if(self.ifname) break addr, netmask, gateway = input[:3] nameservers = input[3:] for i in range(nameservers.count('')): nameservers.remove('') err = _validate(addr, netmask, gateway, nameservers) if err: err = "\n".join(err) else: in_ssh = 'SSH_CONNECTION' in os.environ if not in_ssh or (in_ssh and self.console.yesno( "Warning: Changing ip while an ssh session is active will" " drop said ssh session!", autosize=True) == self.OK): err = ifutil.set_static(self.ifname, addr, netmask, gateway, nameservers) if not err: break else: break self.console.msgbox("Error", err) return "ifconf"
def _apply(interface, address, netmask, gateway, nameservers): set_static(interface, address, netmask, gateway, nameservers) inithooks_cache.write('APP_STATIC_IP', '')