Example #1
0
        private static void EnableDHCP_Ubuntu(String adapter, ExecutionResult ret)
        {
            //netplan configuration (for Ubuntu 18.04+)
            string netplanPath = GetNetplanPath();
            int    startPos    = TxtHelper.GetStrPos(netplanPath, adapter + ":", 0, -1);

            if (startPos != -1)
            {
                string firstStr    = TxtHelper.GetStr(netplanPath, adapter + ":", startPos, startPos);
                int    spacesCount = GetNetplanSpacesCount(firstStr);

                startPos++;
                int           endPos = GetNetplanEndPos(netplanPath, startPos, spacesCount);
                List <string> config = new List <string>();
                config.Add(new String(' ', spacesCount + 2) + "dhcp4: yes");
                TxtHelper.ReplaceAllStr(netplanPath, config, startPos, endPos);
                ShellHelper.RunCmd("netplan apply");
            }

            //iterfaces configuration (for Ubuntu 16.04-)
            startPos = TxtHelper.GetStrPos(interfacesPath, "iface " + adapter, 0, -1);
            if (startPos != -1)
            {
                int endPos = TxtHelper.GetStrPos(interfacesPath, "auto ", startPos, -1);
                if (endPos != -1)
                {
                    endPos--;
                }
                List <string> config = new List <string>();
                config.Add("iface " + adapter + " inet dhcp");
                TxtHelper.ReplaceAllStr(interfacesPath, config, startPos, endPos);
                ret.RebootRequired = true;
            }
        }
Example #2
0
        private static void DisableDHCP_Ubuntu(ExecutionContext context, String adapter, ExecutionResult ret, string[] ipAddresses, string[] subnetMasksPrefix, string[] subnetMasks, string ipGateway)
        {
            //netplan configuration (for Ubuntu 18.04+)
            string netplanPath = GetNetplanPath();
            int    startPos    = TxtHelper.GetStrPos(netplanPath, adapter + ":", 0, -1);

            if (startPos != -1)
            {
                string firstStr    = TxtHelper.GetStr(netplanPath, adapter + ":", startPos, startPos);
                int    spacesCount = GetNetplanSpacesCount(firstStr);

                startPos++;
                int           endPos = GetNetplanEndPos(netplanPath, startPos, spacesCount);
                List <string> config = new List <string>();
                string        str    = new String(' ', spacesCount + 2) + "addresses: [";
                for (int i = 0; i < ipAddresses.Length; i++)
                {
                    str += ipAddresses[i] + "/" + subnetMasksPrefix[i];
                    if (i + 1 < ipAddresses.Length)
                    {
                        str += ",";
                    }
                }
                str += "]";
                config.Add(str);
                config.Add(new String(' ', spacesCount + 2) + "gateway4: " + ipGateway);
                config.Add(new String(' ', spacesCount + 2) + "nameservers:");
                if (CheckParameter(context, "PreferredDNSServer"))
                {
                    string[] dnsServers = ParseArray(context.Parameters["PreferredDNSServer"]);
                    str = new String(' ', spacesCount + 4) + "addresses: [";
                    for (int i = 0; i < dnsServers.Length; i++)
                    {
                        str += dnsServers[i];
                        if (i + 1 < dnsServers.Length)
                        {
                            str += ",";
                        }
                    }
                    str += "]";
                    config.Add(str);
                }
                config.Add(new String(' ', spacesCount + 2) + "dhcp4: no");
                TxtHelper.ReplaceAllStr(netplanPath, config, startPos, endPos);
                ShellHelper.RunCmd("netplan apply");
            }
            //iterfaces configuration (for Ubuntu 16.04-)
            startPos = TxtHelper.GetStrPos(interfacesPath, "iface " + adapter, 0, -1);
            if (startPos != -1)
            {
                int endPos = TxtHelper.GetStrPos(interfacesPath, "auto ", startPos, -1);
                if (endPos != -1)
                {
                    endPos--;
                }
                List <string> config = new List <string>();
                string        str;
                for (int i = 0; i < ipAddresses.Length; i++)
                {
                    config.Add("iface " + adapter + " inet static");
                    config.Add("address " + ipAddresses[i]);
                    config.Add("netmask " + subnetMasks[i]);
                    config.Add("");
                }
                config.Add("gateway " + ipGateway);
                if (CheckParameter(context, "PreferredDNSServer"))
                {
                    string[] dnsServers = ParseArray(context.Parameters["PreferredDNSServer"]);
                    str = "dns-nameservers ";
                    for (int t = 0; t < dnsServers.Length; t++)
                    {
                        str += dnsServers[t];
                        if (t + 1 < dnsServers.Length)
                        {
                            str += " ";
                        }
                    }
                    config.Add(str);
                    config.Add("");
                }
                TxtHelper.ReplaceAllStr(interfacesPath, config, startPos, endPos);
                ret.RebootRequired = true;
            }
        }