示例#1
0
        private static void DisableDHCP_CentOS(ExecutionContext context, String adapter, ExecutionResult ret, string[] ipAddresses, string[] subnetMasksPrefix, string ipGateway)
        {
            string adapterPath = ifcfgBasePath + adapter;

            TxtHelper.ReplaceStr(adapterPath, "BOOTPROTO=none", TxtHelper.GetStrPos(adapterPath, "BOOTPROTO", 0, -1));
            TxtHelper.ReplaceStr(adapterPath, "ONBOOT=yes", TxtHelper.GetStrPos(adapterPath, "ONBOOT", 0, -1));
            TxtHelper.ReplaceStr(adapterPath, "IPADDR=" + ipAddresses[0], TxtHelper.GetStrPos(adapterPath, "IPADDR", 0, -1));
            TxtHelper.ReplaceStr(adapterPath, "PREFIX=" + subnetMasksPrefix[0], TxtHelper.GetStrPos(adapterPath, "PREFIX", 0, -1));
            TxtHelper.ReplaceStr(adapterPath, "GATEWAY=" + ipGateway, TxtHelper.GetStrPos(adapterPath, "GATEWAY", 0, -1));
            if (CheckParameter(context, "PreferredDNSServer"))
            {
                string[] dnsServers = ParseArray(context.Parameters["PreferredDNSServer"]);
                for (int i = 0; i < dnsServers.Length; i++)
                {
                    TxtHelper.ReplaceStr(adapterPath, "DNS" + (i + 1).ToString() + "=" + dnsServers[i], TxtHelper.GetStrPos(adapterPath, "DNS" + (i + 1).ToString(), 0, -1));
                }
            }
            if (ipAddresses.Length > 1)
            {
                for (int i = 1; i < ipAddresses.Length; i++)
                {
                    TxtHelper.ReplaceStr(adapterPath, "IPADDR" + i.ToString() + "=" + ipAddresses[i], TxtHelper.GetStrPos(adapterPath, "IPADDR" + i.ToString(), 0, -1));
                    TxtHelper.ReplaceStr(adapterPath, "PREFIX" + i.ToString() + "=" + subnetMasksPrefix[i], TxtHelper.GetStrPos(adapterPath, "PREFIX" + i.ToString(), 0, -1));
                }
            }
            int pos;
            int interfaceNum = ipAddresses.Length;

            do
            {
                pos = TxtHelper.GetStrPos(adapterPath, "IPADDR" + interfaceNum.ToString(), 0, -1);
                if (pos != -1)
                {
                    TxtHelper.DelStr(adapterPath, pos);
                }
                pos = TxtHelper.GetStrPos(adapterPath, "PREFIX" + interfaceNum.ToString(), 0, -1);
                if (pos != -1)
                {
                    TxtHelper.DelStr(adapterPath, pos);
                }
                interfaceNum++;
            } while (pos != -1);
            ExecutionResult res = ShellHelper.RunCmd("systemctl restart network");

            if (res.ResultCode == 1)
            {
                ret.ResultCode   = 1;
                ret.ErrorMessage = res.ErrorMessage;
            }
        }
示例#2
0
        private static void DisableDHCP_FreeBSD(ExecutionContext context, String adapter, ExecutionResult ret, string[] ipAddresses, string[] subnetMasks, string ipGateway)
        {
            const string rcConfPath     = compatLinux + rcConf;
            const string resolvConfPath = compatLinux + resolvConf;

            ShellHelper.RunCmd("cp -p " + rcConf + " " + rcConfPath);
            int pos = TxtHelper.GetStrPos(rcConfPath, "ifconfig_" + adapter + "=", 0, -1);

            if (pos == -1)
            {
                pos = TxtHelper.GetStrPos(rcConfPath, "ifconfig_" + adapter + " =", 0, -1);
            }
            TxtHelper.ReplaceStr(rcConfPath, "ifconfig_" + adapter + "=\"inet " + ipAddresses[0] + " netmask " + subnetMasks[0] + "\"", pos);
            TxtHelper.ReplaceStr(rcConfPath, "defaultrouter=\"" + ipGateway + "\"", TxtHelper.GetStrPos(rcConfPath, "defaultrouter", 0, -1));

            if (CheckParameter(context, "PreferredDNSServer"))
            {
                ShellHelper.RunCmd("cp -p " + resolvConf + " " + resolvConfPath);
                string[] dnsServers = ParseArray(context.Parameters["PreferredDNSServer"]);
                do
                {
                    pos = TxtHelper.GetStrPos(resolvConfPath, "nameserver", 0, -1);
                    if (pos != -1)
                    {
                        TxtHelper.DelStr(resolvConfPath, pos);
                    }
                } while (pos != -1);
                foreach (var dnsServer in dnsServers)
                {
                    TxtHelper.ReplaceStr(resolvConfPath, "nameserver " + dnsServer, -1);
                }
                ShellHelper.RunCmd("cp -p " + resolvConfPath + " " + resolvConf);
            }

            if (ipAddresses.Length > 1)
            {
                for (int i = 1; i < ipAddresses.Length; i++)
                {
                    pos = TxtHelper.GetStrPos(rcConfPath, "ifconfig_" + adapter + "_alias" + (i - 1).ToString(), 0, -1);
                    TxtHelper.ReplaceStr(rcConfPath, "ifconfig_" + adapter + "_alias" + (i - 1).ToString() + "=\"inet " + ipAddresses[i] + " netmask " + subnetMasks[i] + "\"", pos);
                }
            }

            int interfaceNum = ipAddresses.Length;

            do
            {
                pos = TxtHelper.GetStrPos(rcConfPath, "ifconfig_" + adapter + "_alias" + (interfaceNum - 1).ToString(), 0, -1);
                if (pos != -1)
                {
                    TxtHelper.DelStr(rcConfPath, pos);
                }
                interfaceNum++;
            } while (pos != -1);

            ShellHelper.RunCmd("cp -p " + rcConfPath + " " + rcConf);

            ExecutionResult res = ShellHelper.RunCmd("service netif restart");

            if (res.ResultCode == 1)
            {
                ret.ResultCode   = 1;
                ret.ErrorMessage = res.ErrorMessage;
            }
            res = ShellHelper.RunCmd("service routing restart");
            if (res.ResultCode == 1)
            {
                ret.ResultCode   = 1;
                ret.ErrorMessage = res.ErrorMessage;
            }
        }