Exemplo n.º 1
0
 def ubAddNetworkConfigurationStatic(self, device,
                                     ipaddress, netmask="255.255.255.0", gateway=None, nameservers=None):
     """Add an additional network device with static IP.
     
     As implemented only supports IPv4.
     
     device
         a string, e.g. "eth0".
         
         Should be increased past "eth0" if adding more than one additional configuration.
         
         As implemented there is no protection against a conflict in case there would be a
         pre-existing network configuration for the device.
     
     ipaddress
         IP address.
     
     netmask
         netmask.
         Defaults to 255.255.255.0.
     
     gateway
         gateway.
         If None then default to ip.1.
     
     nameservers
         one nameserver or a list of nameservers.
         If None then default to gateway.
         If empty list then remove option.
     
     return
         self, for daisychaining."""
     # no luck with preseed, hence write into /etc/network/interfaces
     # sanity check
     normalizedStaticIp = NetworkConfigurationStaticParameters.normalizeStaticIp(ipaddress, netmask, gateway, nameservers)
     postSection = self.sectionByName("%post")
     networkConfigurationToAdd = "".join(
         "\necho \"#\" >> /etc/network/interfaces"
         "\necho \"# Network interface " + device + "\" >> /etc/network/interfaces"
         "\necho \"auto " + device + "\" >> /etc/network/interfaces"
         "\necho \"iface " + device + " inet static\" >> /etc/network/interfaces"
         "\necho \"  address " + normalizedStaticIp.ipaddress + "\" >> /etc/network/interfaces"
         "\necho \"  netmask " + normalizedStaticIp.netmask + "\" >> /etc/network/interfaces"
         "\necho \"  gateway " + normalizedStaticIp.gateway + "\" >> /etc/network/interfaces"
         "\necho \"  dns-nameservers " + " ".join(normalizedStaticIp.nameservers) + "\" >> /etc/network/interfaces"
         )
     if re.search(self._endOfNetworkInterfacesRegex, postSection.string): # pre-existing network configuration
         # insert
         postSection.string = re.sub(self._endOfNetworkInterfacesRegex,
                                     r"\g<1>" + networkConfigurationToAdd + r"\2\3",
                                     postSection.string)
     else: # no pre-existing network configuration
         # append
         postSection.string = postSection.string + networkConfigurationToAdd + "\n"
Exemplo n.º 2
0
 def addNetworkConfigurationStatic(self, device,
                                   ipaddress, netmask="255.255.255.0", gateway=None, nameservers=None):
     """Add an additional network device with static IP.
     
     As implemented only supports IPv4.
     
     device
         a string, e.g. "eth0".
         
         Should be increased past "eth0" if adding more than one additional configuration.
         
         As implemented there is no protection against a conflict in case there would be a
         pre-existing network configuration for the device.
     
     ipaddress
         IP address.
     
     netmask
         netmask.
         Defaults to 255.255.255.0.
     
     gateway
         gateway.
         If None then default to ip.1.
     
     nameservers
         one nameserver or a list of nameservers.
         If None then default to gateway.
         If empty list then remove option.
     
     return
         self, for daisychaining."""
     # no luck with preseed, hence write into /etc/network/interfaces
     # sanity check
     normalizedStaticIp = NetworkConfigurationStaticParameters.normalizeStaticIp(ipaddress, netmask, gateway, nameservers)
     networkConfigurationToAdd = "\n".join([
         r"#",
         r"# Network interface " + device,
         r"auto " + device,
         r"iface " + device + r" inet static",
         r"  address " + normalizedStaticIp.ipaddress,
         r"  netmask " + normalizedStaticIp.netmask,
         r"  gateway " + normalizedStaticIp.gateway,
         r"  dns-nameservers " + " ".join(normalizedStaticIp.nameservers),
         ])
     # cannot use \n because ubiquity installer echo apparently doesn't take option -e
     for line in networkConfigurationToAdd.split("\n"):
         self.addPreseedCommandLine("ubiquity", "ubiquity/success_command",
                                    r'echo "' + line + r'" >> /target/etc/network/interfaces')
     return self
Exemplo n.º 3
0
 def elReplaceStaticIP(self, ipaddress, netmask="255.255.255.0", gateway=None, nameservers=None):
     """Replace static IP options in network option.
     
     As implemented only supports IPv4.
     
     ipaddress
         IP address.
     
     netmask
         netmask.
         Defaults to 255.255.255.0.
     
     gateway
         gateway.
         If None then default to ip.1.
     
     nameservers
         one nameserver or a list of nameservers.
         If None then default to gateway.
         If empty list then remove option.
     
     return
         self, for daisychaining."""
     # see http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/6/html/Installation_Guide/s1-kickstart2-options.html
     # sanity check
     normalizedStaticIp = NetworkConfigurationStaticParameters.normalizeStaticIp(ipaddress, netmask, gateway, nameservers)
     commandSection = self.sectionByName("command")
     # several set
     commandSection.string = re.sub(r"(?m)^([ \t]*network[ \t]+.*--ip[ \t]*(?:=|[ \t])[ \t]*)[^\s]+(.*)$",
                                    r"\g<1>" + normalizedStaticIp.ipaddress + r"\g<2>",
                                    commandSection.string)
     commandSection.string = re.sub(r"(?m)^([ \t]*network[ \t]+.*--netmask[ \t]*(?:=|[ \t])[ \t]*)[^\s]+(.*)$",
                                    r"\g<1>" + normalizedStaticIp.netmask + r"\g<2>",
                                    commandSection.string)
     commandSection.string = re.sub(r"(?m)^([ \t]*network[ \t]+.*--gateway[ \t]*(?:=|[ \t])[ \t]*)[^\s]+(.*)$",
                                    r"\g<1>" + normalizedStaticIp.gateway + r"\g<2>",
                                    commandSection.string)
     if normalizedStaticIp.nameservers:
         commandSection.string = re.sub(r"(?m)^([ \t]*network[ \t]+.*--nameserver[ \t]*(?:=|[ \t])[ \t]*)[^\s]+(.*)$",
                                        r"\g<1>" + ",".join(normalizedStaticIp.nameservers) + r"\g<2>",
                                        commandSection.string)
     else:
         # remove option --nameserver
         commandSection.string = re.sub(r"(?m)^([ \t]*network[ \t]+.*)--nameserver[ \t]*(?:=|[ \t])[ \t]*[^\s]+(.*)$",
                                        r"\g<1>" + r"\g<2>",
                                        commandSection.string)
     return self
Exemplo n.º 4
0
    def addNetworkConfigurationStatic(self, mac,
                                      ipaddress, netmask="255.255.255.0", gateway=None, nameservers=None,
                                      limitRoutingToLocalByNetmask=False):
        """Add an additional network device with static IP.
        
        As implemented only supports IPv4.
        
        mac
            the MAC, e.g. "01:23:45:67:89:ab" or "01-23-45-67-89-AB".
        
        ipaddress
            IP address.
        
        netmask
            netmask.
            Defaults to 255.255.255.0.
        
        gateway
            gateway.
            If None then default to ip.1.
        
        nameservers
            one nameserver or a list of nameservers.
            If None then default to gateway.
            If empty list then do not add any.
        
        return
            self, for daisychaining."""
        # sanity check
        normalizedStaticIp = NetworkConfigurationStaticParameters.normalizeStaticIp(ipaddress, netmask, gateway, nameservers)
        # see http://technet.microsoft.com/en-us/library/ff716288.aspx
        mac = mac.replace(":","-").upper()
        ipaddressSlashRoutingPrefixLength = normalizedStaticIp.ipaddress + "/" + str(normalizedStaticIp.routingprefixlength)
        gatewaySlashRoutingPrefixLength = normalizedStaticIp.gateway + "/" + str(normalizedStaticIp.routingprefixlength)
        if not limitRoutingToLocalByNetmask:
            routePrefix = "0.0.0.0/0"
        else:
            routePrefix = IPAddress.asString(normalizedStaticIp.localprefix) + "/" + str(normalizedStaticIp.routingprefixlength)
        nameservers = normalizedStaticIp.nameservers
        additionalContent = r"""
<component name="Microsoft-Windows-TCPIP" processorArchitecture="x86" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Interfaces>
    <Interface wcm:action="add">
      <Identifier>""" + mac + r"""</Identifier>
      <Ipv4Settings>
        <DhcpEnabled>false</DhcpEnabled>
        <RouterDiscoveryEnabled>false</RouterDiscoveryEnabled>
      </Ipv4Settings>
      <UnicastIpAddresses>
        <IpAddress wcm:action="add" wcm:keyValue="1">""" + ipaddressSlashRoutingPrefixLength + r"""</IpAddress>
      </UnicastIpAddresses>
      <Routes>
        <Route wcm:action="add">
          <Identifier>1</Identifier>
          <NextHopAddress>""" + gatewaySlashRoutingPrefixLength + r"""</NextHopAddress>
          <Prefix>""" + routePrefix + r"""</Prefix>
        </Route>
      </Routes>
    </Interface>
  </Interfaces>
</component>"""
        if nameservers:
            additionalContent += r"""
<component name="Microsoft-Windows-DNS-Client" processorArchitecture="x86" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Interfaces>
    <Interface wcm:action="add">
      <Identifier>""" + mac + r"""</Identifier>
      <DNSServerSearchOrder>
""" + "\n".join(map(lambda nameserver, i:
                    r"""<IpAddress wcm:action="add" wcm:keyValue=""" r'"' + str(i+1) + r'"' r""">""" + nameserver + r"""</IpAddress>""",
                    nameservers, range(0,len(nameservers)))) + r"""
      </DNSServerSearchOrder>
      <EnableAdapterDomainNameRegistration>false</EnableAdapterDomainNameRegistration>
      <DisableDynamicUpdate>true</DisableDynamicUpdate>
    </Interface>
  </Interfaces>
<DNSDomain>example.com</DNSDomain>
</component>"""
        self._appendToChildren("settings", "pass", "specialize", additionalContent, prepend=True)
        return self
Exemplo n.º 5
0
    def addNetworkConfigurationStatic(self,
                                      mac,
                                      ipaddress,
                                      netmask="255.255.255.0",
                                      gateway=None,
                                      nameservers=None,
                                      limitRoutingToLocalByNetmask=False):
        """Add an additional network device with static IP.
        
        As implemented only supports IPv4.
        
        mac
            the MAC, e.g. "01:23:45:67:89:ab" or "01-23-45-67-89-AB".
        
        ipaddress
            IP address.
        
        netmask
            netmask.
            Defaults to 255.255.255.0.
        
        gateway
            gateway.
            If None then default to ip.1.
        
        nameservers
            one nameserver or a list of nameservers.
            If None then default to gateway.
            If empty list then do not add any.
        
        return
            self, for daisychaining."""
        # sanity check
        normalizedStaticIp = NetworkConfigurationStaticParameters.normalizeStaticIp(
            ipaddress, netmask, gateway, nameservers)
        # see http://technet.microsoft.com/en-us/library/ff716288.aspx
        mac = mac.replace(":", "-").upper()
        ipaddressSlashRoutingPrefixLength = normalizedStaticIp.ipaddress + "/" + str(
            normalizedStaticIp.routingprefixlength)
        gatewaySlashRoutingPrefixLength = normalizedStaticIp.gateway + "/" + str(
            normalizedStaticIp.routingprefixlength)
        if not limitRoutingToLocalByNetmask:
            routePrefix = "0.0.0.0/0"
        else:
            routePrefix = IPAddress.asString(
                normalizedStaticIp.localprefix) + "/" + str(
                    normalizedStaticIp.routingprefixlength)
        nameservers = normalizedStaticIp.nameservers
        additionalContent = r"""
<component name="Microsoft-Windows-TCPIP" processorArchitecture="x86" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Interfaces>
    <Interface wcm:action="add">
      <Identifier>""" + mac + r"""</Identifier>
      <Ipv4Settings>
        <DhcpEnabled>false</DhcpEnabled>
        <RouterDiscoveryEnabled>false</RouterDiscoveryEnabled>
      </Ipv4Settings>
      <UnicastIpAddresses>
        <IpAddress wcm:action="add" wcm:keyValue="1">""" + ipaddressSlashRoutingPrefixLength + r"""</IpAddress>
      </UnicastIpAddresses>
      <Routes>
        <Route wcm:action="add">
          <Identifier>1</Identifier>
          <NextHopAddress>""" + gatewaySlashRoutingPrefixLength + r"""</NextHopAddress>
          <Prefix>""" + routePrefix + r"""</Prefix>
        </Route>
      </Routes>
    </Interface>
  </Interfaces>
</component>"""
        if nameservers:
            additionalContent += r"""
<component name="Microsoft-Windows-DNS-Client" processorArchitecture="x86" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Interfaces>
    <Interface wcm:action="add">
      <Identifier>""" + mac + r"""</Identifier>
      <DNSServerSearchOrder>
""" + "\n".join(
                map(
                    lambda nameserver, i:
                    r"""<IpAddress wcm:action="add" wcm:keyValue="""
                    r'"' + str(i + 1) + r'"'
                    r""">""" + nameserver + r"""</IpAddress>""", nameservers,
                    range(0, len(nameservers)))) + r"""
      </DNSServerSearchOrder>
      <EnableAdapterDomainNameRegistration>false</EnableAdapterDomainNameRegistration>
      <DisableDynamicUpdate>true</DisableDynamicUpdate>
    </Interface>
  </Interfaces>
<DNSDomain>example.com</DNSDomain>
</component>"""
        self._appendToChildren("settings",
                               "pass",
                               "specialize",
                               additionalContent,
                               prepend=True)
        return self