Ejemplo n.º 1
0
        private static string GetAdapterMacAddress(string adapterName)
        {
            ExecutionResult res;

            if (OsVersion.GetOsVersion() == OsVersionEnum.FreeBSD)
            {
                res = ShellHelper.RunCmd("ifconfig " + adapterName + " ether | grep ether | awk '{print $2}'");
            }
            else
            {
                res = ShellHelper.RunCmd("cat /sys/class/net/" + adapterName + "/address");
            }
            if (res.ResultCode == 1 || res.Value == null)
            {
                return(null);
            }
            string mac = res.Value.Trim();

            if (mac != null)
            {
                mac = mac.Replace("\n", "");
            }
            else
            {
                return(null);
            }
            if (IsValidMACAddress(mac))
            {
                return(mac);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
 private static void InstallService()
 {
     if (OsVersion.GetOsVersion() == OsVersionEnum.FreeBSD)
     {
         InstallService_FreeBSD();
     }
     else
     {
         InstallService_Linux();
     }
 }
Ejemplo n.º 3
0
        private static void DisableDHCP(ExecutionContext context, String adapter, ExecutionResult ret)
        {
            List <string> oldIpList = GetAdapterIp(adapter);

            string[] ipGateways  = ParseArray(context.Parameters["DefaultIPGateway"]);
            string[] ipAddresses = ParseArray(context.Parameters["IPAddress"]);
            string[] subnetMasks = ParseArray(context.Parameters["SubnetMask"]);
            if (subnetMasks.Length != ipAddresses.Length)
            {
                throw new ArgumentException("Number of Subnet Masks should be equal to IP Addresses");
            }
            string[] subnetMasksPrefix = new string[subnetMasks.Length];
            for (int i = 0; i < subnetMasks.Length; i++)
            {
                subnetMasksPrefix[i] = GetSubnetMaskPrefix(subnetMasks[i]);
            }

            switch (OsVersion.GetOsVersion())
            {
            case OsVersionEnum.Ubuntu:
                DisableDHCP_Ubuntu(context, adapter, ret, ipAddresses, subnetMasksPrefix, subnetMasks, ipGateways[0]);
                break;

            case OsVersionEnum.CentOS:
                DisableDHCP_CentOS(context, adapter, ret, ipAddresses, subnetMasksPrefix, ipGateways[0]);
                break;

            case OsVersionEnum.FreeBSD:
                DisableDHCP_FreeBSD(context, adapter, ret, ipAddresses, subnetMasks, ipGateways[0]);
                break;
            }

            if (oldIpList != null)//update ip in hosts
            {
                foreach (string ip in oldIpList)
                {
                    if (ip.Length > 0)
                    {
                        if (OsVersion.GetOsVersion() == OsVersionEnum.FreeBSD)
                        {
                            ShellHelper.RunCmd("cp -p " + hosts + " " + compatLinux + hosts);
                            TxtHelper.ReplaceStr(compatLinux + hosts, ip, ipAddresses[0]);
                            ShellHelper.RunCmd("cp -p " + compatLinux + hosts + " " + hosts);
                        }
                        else
                        {
                            TxtHelper.ReplaceStr(hosts, ip, ipAddresses[0]);
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public static ExecutionResult ChangeUserPassword(string userName, string password)
        {
            ExecutionResult ret = new ExecutionResult();

            ret.ResultCode   = 0;
            ret.ErrorMessage = null;

            try
            {
                string shellPath = null;
                string arguments = null;
                if (OsVersion.GetOsVersion() == OsVersionEnum.FreeBSD)
                {
                    shellPath = Shell_FreeBSD;
                    arguments = $"-c \"echo \"{password}\" | pw usermod {userName} -h 0\"";
                }
                else
                {
                    shellPath = Shell_Linux;
                    arguments = $"-c \"passwd {userName}\"";
                }
                using (Process process = new Process())
                {
                    process.StartInfo = new ProcessStartInfo
                    {
                        FileName  = shellPath,
                        Arguments = arguments,
                        RedirectStandardOutput = true,
                        RedirectStandardError  = true,
                        RedirectStandardInput  = true,
                        UseShellExecute        = false,
                        CreateNoWindow         = true,
                    };
                    process.Start();
                    if (OsVersion.GetOsVersion() != OsVersionEnum.FreeBSD)//Input redirect dont work on FreeBSD
                    {
                        System.Threading.Thread.Sleep(1000);
                        process.StandardInput.WriteLine(password);
                        System.Threading.Thread.Sleep(1000);
                        process.StandardInput.WriteLine(password);
                    }
                    process.WaitForExit();
                }
                return(ret);
            }
            catch (Exception ex)
            {
                ret.ResultCode   = 1;
                ret.ErrorMessage = "ShellHelper error: " + ex.ToString();
                return(ret);
            }
        }
Ejemplo n.º 5
0
 static void Main(string[] args)
 {
     if (OsVersion.GetOsVersion() == OsVersionEnum.FreeBSD)
     {
         InputKVP  = DEFAULT_KVP_DIRECTORY_FREEBSD + KVP_BASEFILENAME + "0";
         OutputKVP = DEFAULT_KVP_DIRECTORY_FREEBSD + KVP_BASEFILENAME + "1";
     }
     foreach (var arg in args)
     {
         if (arg.Trim().Equals("install"))
         {
             InstallService();
             return;
         }
     }
     mainThread = new System.Threading.Thread(new System.Threading.ThreadStart(Start));
     mainThread.Start();
 }
Ejemplo n.º 6
0
        private static readonly string Shell_FreeBSD = AppDomain.CurrentDomain.BaseDirectory + "sh";//use external shell to out from Linux-emulator

        public static ExecutionResult RunCmd(string cmd)
        {
            ExecutionResult ret = new ExecutionResult();

            ret.ResultCode   = 0;
            ret.ErrorMessage = null;
            string shellPath = null;

            try
            {
                if (OsVersion.GetOsVersion() == OsVersionEnum.FreeBSD)
                {
                    shellPath = Shell_FreeBSD;
                }
                else
                {
                    shellPath = Shell_Linux;
                }
                string escapedArgs = cmd.Replace("\"", "\\\"");
                using (Process process = new Process())
                {
                    process.StartInfo = new ProcessStartInfo
                    {
                        FileName  = shellPath,
                        Arguments = $"-c \"{escapedArgs}\"",
                        RedirectStandardOutput = true,
                        RedirectStandardError  = true,
                        UseShellExecute        = false,
                        CreateNoWindow         = true,
                    };
                    process.Start();
                    ret.Value        = process.StandardOutput.ReadToEnd();
                    ret.ErrorMessage = process.StandardError.ReadToEnd();
                    process.WaitForExit();
                }
                return(ret);
            }
            catch (Exception ex)
            {
                ret.ResultCode   = 1;
                ret.ErrorMessage = "ShellHelper error: " + ex.ToString();
                return(ret);
            }
        }
Ejemplo n.º 7
0
        private static void EnableDHCP(String adapter, ExecutionResult ret)
        {
            Log.WriteStart("Enabling DHCP...");
            switch (OsVersion.GetOsVersion())
            {
            case OsVersionEnum.Ubuntu:
                EnableDHCP_Ubuntu(adapter, ret);
                break;

            case OsVersionEnum.CentOS:
                EnableDHCP_CentOS(adapter, ret);
                break;

            case OsVersionEnum.FreeBSD:
                EnableDHCP_FreeBSD(adapter, ret);
                break;
            }
            Log.WriteEnd("DHCP enabled");
        }
Ejemplo n.º 8
0
        private static List <Adapter> getAdapters()
        {
            List <Adapter>  adapters = new List <Adapter>();
            ExecutionResult res      = null;

            switch (OsVersion.GetOsVersion())
            {
            case OsVersionEnum.Ubuntu:
                res = ShellHelper.RunCmd("ifconfig -s | awk '{print $1}'");
                break;

            case OsVersionEnum.CentOS:
                res = ShellHelper.RunCmd("nmcli -p dev | grep \"ethernet\" | awk '{print $1}'");
                break;

            case OsVersionEnum.FreeBSD:
                res = ShellHelper.RunCmd("ifconfig -l");
                if (res.Value != null)
                {
                    res.Value = res.Value.Replace(" ", "\n");
                }
                break;
            }
            if (res.Value == null || res.ResultCode == 1)
            {
                return(null);
            }
            List <string> adaptersStr = StrToList(res.Value.Trim());

            foreach (string adapterName in adaptersStr)
            {
                if (!adapterName.Equals("Iface") && !adapterName.Equals("lo"))
                {
                    string mac = GetAdapterMacAddress(adapterName);
                    if (mac != null)
                    {
                        Adapter adapter = new Adapter(adapterName, mac);
                        adapters.Add(adapter);
                    }
                }
            }
            return(adapters);
        }
Ejemplo n.º 9
0
        private static List <string> GetAdapterIp(string adapterName)
        {
            ExecutionResult res;

            if (OsVersion.GetOsVersion() == OsVersionEnum.FreeBSD)
            {
                res = ShellHelper.RunCmd("ifconfig " + adapterName + " inet | grep inet | awk '{print $2}'");
            }
            else
            {
                res = ShellHelper.RunCmd("ip addr show " + adapterName + " | grep \"inet \" | awk '{print $2}' | cut -d/ -f1");
            }
            if (res.ResultCode == 1)
            {
                return(null);
            }
            List <string> IpList = StrToList(res.Value.Trim());

            return(IpList);
        }
Ejemplo n.º 10
0
        public static ExecutionResult Run(ref ExecutionContext context)
        {
            ExecutionResult ret = new ExecutionResult();

            ret.ResultCode     = 0;
            ret.ErrorMessage   = null;
            ret.RebootRequired = true;
            try
            {
                context.ActivityDescription = "Changing computer name...";
                context.Progress            = 0;
                if (!context.Parameters.ContainsKey("FullComputerName"))
                {
                    ret.ResultCode   = 2;
                    ret.ErrorMessage = "Parameter 'FullComputerName' not found";
                    Log.WriteError(ret.ErrorMessage);
                    context.Progress = 100;
                    return(ret);
                }
                string computerName = context.Parameters["FullComputerName"].ToLower();
                string netBiosName  = computerName;
                int    idx          = netBiosName.IndexOf(".");
                if (idx != -1)
                {
                    netBiosName = computerName.Substring(0, idx);
                }
                ExecutionResult res;
                res = ShellHelper.RunCmd("hostname -s");
                string hostNameOld = null;
                if (res.Value != null)
                {
                    hostNameOld = res.Value.Trim();
                }

                res = ShellHelper.RunCmd("hostname");
                string fullNameOld = null;
                if (res.Value != null)
                {
                    fullNameOld = res.Value.Trim();
                }

                string domain    = computerName.Replace(netBiosName + ".", "");
                string domainOld = null;
                if (hostNameOld != null && hostNameOld.Length > 0 && fullNameOld != null && fullNameOld.Length > 0)
                {
                    domainOld = fullNameOld.Replace(hostNameOld + ".", "");
                }

                switch (OsVersion.GetOsVersion())
                {
                case OsVersionEnum.Ubuntu:
                    TxtHelper.ReplaceStr(hostname, computerName, 0);
                    if (domainOld != null && domainOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(hosts, domainOld, domain);
                    }
                    if (hostNameOld != null && hostNameOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(hosts, hostNameOld, netBiosName);
                    }
                    TxtHelper.ReplaceStr(cloudCfg, "preserve_hostname: false", "preserve_hostname: true");
                    TxtHelper.ReplaceStr(cloudCfg, "# preserve_hostname: true", "preserve_hostname: true");
                    TxtHelper.ReplaceStr(cloudCfg, "#preserve_hostname: true", "preserve_hostname: true");
                    break;

                case OsVersionEnum.CentOS:
                    TxtHelper.ReplaceStr(hostname, computerName, 0);
                    if (domainOld != null && domainOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(hosts, domainOld, domain);
                    }
                    if (hostNameOld != null && hostNameOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(hosts, hostNameOld, netBiosName);
                    }
                    break;

                case OsVersionEnum.FreeBSD:
                    ShellHelper.RunCmd("cp -p " + rcConf + " " + compatLinux + rcConf);
                    ShellHelper.RunCmd("cp -p " + hosts + " " + compatLinux + hosts);
                    TxtHelper.ReplaceStr(compatLinux + rcConf, "hostname=\"" + computerName + "\"", TxtHelper.GetStrPos(compatLinux + rcConf, "hostname", 0, -1));
                    if (domainOld != null && domainOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(compatLinux + hosts, domainOld, domain);
                    }
                    if (hostNameOld != null && hostNameOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(compatLinux + hosts, hostNameOld, netBiosName);
                    }
                    ShellHelper.RunCmd("cp -p " + compatLinux + rcConf + " " + rcConf);
                    ShellHelper.RunCmd("cp -p " + compatLinux + hosts + " " + hosts);
                    break;

                default:
                    TxtHelper.ReplaceStr(hostname, computerName, 0);
                    if (domainOld != null && domainOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(hosts, domainOld, domain);
                    }
                    if (hostNameOld != null && hostNameOld.Length > 0)
                    {
                        TxtHelper.ReplaceStr(hosts, hostNameOld, netBiosName);
                    }
                    break;
                }
            }catch (Exception ex)
            {
                ret.ResultCode   = 1;
                ret.ErrorMessage = "ChangeComputerName error: " + ex.ToString();
                Log.WriteError(ret.ErrorMessage);
            }
            if (ret.ResultCode == 0)
            {
                Log.WriteInfo("Computer name has been changed successfully");
            }
            context.Progress = 100;
            return(ret);
        }