private NetworkInterface CreateNetworkInterfaceInstanceFromInterfaceState(InterfaceState interfaceState)
        {
            string ipAddress = string.Empty;
            try { ipAddress = interfaceState.InetAddr.First().IPAddr.ToString(); } catch (Exception) { }

            return new NetworkInterface() { Name = interfaceState.Name, MacAddress = interfaceState.HWAddr, IpAddress = ipAddress };
        }
        private static void getMACAIX(SshCommandLineRunner commandRunner, InterfaceState curif)
        {
            char[] fieldseps          = { ' ', '\t' };
            char[] onlydot            = { '.' };
            var    commandOutputLines = commandRunner.ExecuteCommand("netstat -I " + curif.Name).SplitStringByDefaultNewLine();

            if (commandOutputLines == null)
            {
                return;
            }
            foreach (var line in commandOutputLines)
            {
                string[] pfields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
                if (pfields.GetUpperBound(0) >= 3)
                {
                    string   maybeMAC = pfields[3];
                    string[] bytesMAC = maybeMAC.Split(onlydot, StringSplitOptions.None);
                    if (bytesMAC.GetUpperBound(0) == 5)
                    {
                        try
                        {
                            curif.HWAddr = String.Format("{0}:{1}:{2}:{3}:{4}:{5}",
                                                         Convert.ToUInt32(bytesMAC[0], 16).ToString("X2"),
                                                         Convert.ToUInt32(bytesMAC[1], 16).ToString("X2"),
                                                         Convert.ToUInt32(bytesMAC[2], 16).ToString("X2"),
                                                         Convert.ToUInt32(bytesMAC[3], 16).ToString("X2"),
                                                         Convert.ToUInt32(bytesMAC[4], 16).ToString("X2"),
                                                         Convert.ToUInt32(bytesMAC[5], 16).ToString("X2"));
                            return;
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }
        private static void getMACAIX(SshCommandLineRunner commandRunner, InterfaceState curif)
        {
            char[] fieldseps = { ' ', '\t' };
            char[] onlydot = { '.' };
            var commandOutputLines = commandRunner.ExecuteCommand("netstat -I " + curif.Name).SplitStringByDefaultNewLine();

            foreach (var line in commandOutputLines)
            {
                string[] pfields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
                if (pfields.GetUpperBound(0) >= 3)
                {
                    string maybeMAC = pfields[3];
                    string[] bytesMAC = maybeMAC.Split(onlydot, StringSplitOptions.None);
                    if (bytesMAC.GetUpperBound(0) == 5)
                    {
                        try
                        {
                            curif.HWAddr = String.Format("{0}:{1}:{2}:{3}:{4}:{5}",
                                Convert.ToUInt32(bytesMAC[0], 16).ToString("X2"),
                                Convert.ToUInt32(bytesMAC[1], 16).ToString("X2"),
                                Convert.ToUInt32(bytesMAC[2], 16).ToString("X2"),
                                Convert.ToUInt32(bytesMAC[3], 16).ToString("X2"),
                                Convert.ToUInt32(bytesMAC[4], 16).ToString("X2"),
                                Convert.ToUInt32(bytesMAC[5], 16).ToString("X2"));
                            return;
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }
        private static void getInterfacesSolaris(SshCommandLineRunner commandRunner, SysInfo mySysInfo)
        {
            uint ifIndex = 0;
            InterfaceState curif = null;
            char[] fieldseps = { ' ', '\t' };
            var commandOutputLines = commandRunner.ExecuteCommand("/sbin/ifconfig -a").SplitStringByDefaultNewLine();

            foreach (var line in commandOutputLines)
            {
                if (!char.IsWhiteSpace(line[0]))
                {
                    if (curif != null)
                    {
                        getMACAIX(commandRunner, curif);
                        mySysInfo.Interfaces.Add(curif);
                    }
                    curif = new InterfaceState();
                    curif.InetAddr = new List<InterfaceState.IPInfo>();
                    curif.Index = ifIndex++;
                    string[] ifields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
                    curif.Name = ifields[0].Replace(":", "");
                    string iflags = ifields[1].Replace("<", ",").Replace(">", ",");
                    if (iflags.Contains(",LOOPBACK,"))
                    {
                        curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_LOOPBACK;
                        curif.IsPhysical = false;
                    }
                    else if (iflags.Contains(",VIRTUAL,"))
                    {
                        curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_OTHER;
                        curif.IsPhysical = false;
                    }
                    else
                    {
                        curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_OTHER;
                        curif.IsPhysical = true;
                    }
                }
                else
                {
                    string[] pfields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
                    if (pfields[0] == "ether")
                    {
                        curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_ETHERNET;
                        string[] macParts = pfields[1].Split(':');
                        if (macParts.Length == 6)
                        {
                            for (int i = 0; i < macParts.Length; i++)
                            {
                                if (macParts[i].Length == 1)
                                    macParts[i] = "0" + macParts[i];
                            }
                            curif.HWAddr = String.Join(":", macParts);
                        }
                    }
                    else if (pfields[0] == "inet")
                    {
                        InterfaceState.IPInfo curip = new InterfaceState.IPInfo();
                        curip.IPAddr = pfields[1];
                        if ((pfields.Length > 5) && (pfields[4] == "broadcast"))
                            curip.IPBcast = pfields[5];
                        else
                            curip.IPBcast = "";
                        if ((pfields.Length > 3) && (pfields[2] == "netmask"))
                        {
                            if (pfields[3].Contains("."))
                                curip.IPMask = pfields[3];
                            else
                            {
                                if (pfields[3].StartsWith("0x"))
                                    pfields[3] = pfields[3].Substring(2);
                                UInt32 masknum = Convert.ToUInt32(pfields[3], 16);
                                curip.IPMask = String.Format("{0}.{1}.{2}.{3}",
                                    masknum >> 24, (masknum >> 16) & 0x00FF, (masknum >> 8) & 0x00FF, masknum & 0x00FF);
                            }
                        }
                        else
                            curip.IPMask = "";
                        curip.AddrType = InterfaceState.stateAddrType.MIB_IPADDR_PRIMARY;
                        curif.InetAddr.Add(curip);
                    }
                }
            }
            if (curif != null)
                mySysInfo.Interfaces.Add(curif);
        }
        private static void getInterfacesLinux(SshCommandLineRunner commandRunner, SysInfo mySysInfo)
        {
            uint ifIndex = 0;
            InterfaceState curif = null;
            char[] fieldseps = { ' ', '\t' };

            var output = commandRunner.ExecuteCommand("/sbin/ip addr show").SplitStringByDefaultNewLine();
            foreach (var line in output)
            {
                if ((line[0] >= '0') && (line[0] <= '9'))
                {
                    if (curif != null)
                        mySysInfo.Interfaces.Add(curif);
                    curif = new InterfaceState();
                    curif.InetAddr = new List<InterfaceState.IPInfo>();
                    curif.Index = ifIndex++;
                    var ifields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
                    curif.Name = ifields[1].Replace(":", "");
                }
                else
                {
                    string[] pfields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
                    if (pfields[0].StartsWith("link/"))
                    {
                        string itype = pfields[0].Remove(0, 5);
                        switch (itype)
                        {
                            case "ether":
                                curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_ETHERNET;
                                curif.IsPhysical = true;
                                break;
                            case "loopback":
                                curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_LOOPBACK;
                                curif.IsPhysical = false;
                                break;
                            case "ppp":
                                curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_PPP;
                                curif.IsPhysical = false;
                                break;
                            case "fddi":
                                curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_FDDI;
                                curif.IsPhysical = true;
                                break;
                            case "tr":
                                curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_TOKENRING;
                                curif.IsPhysical = true;
                                break;
                            case "slip":
                            case "cslip":
                            case "slip6":
                            case "cslip6":
                                curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_SLIP;
                                curif.IsPhysical = false;
                                break;
                            default:
                                curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_OTHER;
                                break;
                        }
                        curif.HWAddr = pfields[1];
                    }
                    else if (pfields[0] == "inet")
                    {
                        char[] sepslash = { '/' };
                        string[] addrAndMask = pfields[1].Split(sepslash);
                        InterfaceState.IPInfo curip;
                        curip.IPAddr = addrAndMask[0];
                        if (addrAndMask[1].Contains("."))
                            curip.IPMask = addrAndMask[1];
                        else
                        {
                            uint hostbits = uint.Parse(addrAndMask[1]);
                            if (hostbits <= 8)
                                curip.IPMask = String.Format("{0}.0.0.0", (0xFF00 >> (int)hostbits) & 0x00FF);
                            else if (hostbits <= 16)
                                curip.IPMask = String.Format("255.{0}.0.0", (0xFF00 >> (int)(hostbits - 8)) & 0x00FF);
                            else if (hostbits <= 24)
                                curip.IPMask = String.Format("255.255.{0}.0", (0xFF00 >> (int)(hostbits - 16)) & 0x00FF);
                            else if (hostbits <= 32)
                                curip.IPMask = String.Format("255.255.{0}.0", (0xFF00 >> (int)(hostbits - 24)) & 0x00FF);
                            else
                                throw new InvalidOperationException(String.Format("Netmask bits in '{0}' ({1}) outside acceptable range", curif.Name, hostbits));
                        }
                        curip.IPBcast = TokenFollowing(pfields, "brd");
                        curip.AddrType = InterfaceState.stateAddrType.MIB_IPADDR_PRIMARY;
                        curif.InetAddr.Add(curip);
                    }
                }
            }
            if (curif != null)
                mySysInfo.Interfaces.Add(curif);
        }
 private static void getMACAIX(SshExec exec, InterfaceState curif)
 {
     char[] lineseps = { '\r', '\n' };
     char[] fieldseps = { ' ', '\t' };
     char[] onlydot = { '.' };
     string output = exec.RunCommand("netstat -I " + curif.Name);
     string[] lines = output.Split(lineseps, StringSplitOptions.RemoveEmptyEntries);
     foreach (string line in lines)
     {
         string[] pfields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
         if (pfields.GetUpperBound(0) >= 3)
         {
             string maybeMAC = pfields[3];
             string[] bytesMAC = maybeMAC.Split(onlydot, StringSplitOptions.None);
             if (bytesMAC.GetUpperBound(0) == 5)
             {
                 try
                 {
                     curif.HWAddr = String.Format("{0}:{1}:{2}:{3}:{4}:{5}",
                         Convert.ToUInt32(bytesMAC[0], 16).ToString("X2"),
                         Convert.ToUInt32(bytesMAC[1], 16).ToString("X2"),
                         Convert.ToUInt32(bytesMAC[2], 16).ToString("X2"),
                         Convert.ToUInt32(bytesMAC[3], 16).ToString("X2"),
                         Convert.ToUInt32(bytesMAC[4], 16).ToString("X2"),
                         Convert.ToUInt32(bytesMAC[5], 16).ToString("X2"));
                     return;
                 }
                 catch
                 {
                 }
             }
         }
     }
 }
 private static void getInterfacesAIX(SshExec exec, SysInfo mySysInfo)
 {
     uint ifIndex = 0;
     InterfaceState curif = null;
     char[] lineseps = { '\r', '\n' };
     char[] fieldseps = { ' ', '\t' };
     string output = exec.RunCommand("ifconfig -a");
     string[] lines = output.Split(lineseps, StringSplitOptions.RemoveEmptyEntries);
     foreach (string line in lines)
     {
         if (!char.IsWhiteSpace(line[0]))
         {
             if (curif != null)
             {
                 getMACAIX(exec, curif);
                 mySysInfo.Interfaces.Add(curif);
             }
             curif = new InterfaceState();
             curif.InetAddr = new List<InterfaceState.IPInfo>();
             curif.Index = ifIndex++;
             string[] ifields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
             curif.Name = ifields[0].Replace(":", "");
             string iflags = ifields[1].Replace("<", ",").Replace(">", ",");
             if (iflags.Contains(",LOOPBACK,"))
             {
                 curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_LOOPBACK;
                 curif.IsPhysical = false;
             }
             else if (curif.Name.StartsWith("en") || curif.Name.StartsWith("et"))
             {
                 curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_ETHERNET;
                 curif.IsPhysical = true;
             }
             else
             {
                 curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_OTHER;
             }
         }
         else
         {
             string[] pfields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
             if (pfields[0] == "inet")
             {
                 InterfaceState.IPInfo curip = new InterfaceState.IPInfo();
                 curip.IPAddr = pfields[1];
                 if ((pfields.Length > 5) && (pfields[4] == "broadcast"))
                     curip.IPBcast = pfields[5];
                 else
                     curip.IPBcast = "";
                 if ((pfields.Length > 3) && (pfields[2] == "netmask"))
                 {
                     if (pfields[3].Contains("."))
                         curip.IPMask = pfields[3];
                     else if (pfields[3].StartsWith("0x"))
                     {
                         UInt32 masknum = Convert.ToUInt32(pfields[3].Substring(2), 16);
                         curip.IPMask = String.Format("{0}.{1}.{2}.{3}",
                             masknum >> 24, (masknum >> 16) & 0x00FF, (masknum >> 8) & 0x00FF, masknum & 0x00FF);
                     }
                 }
                 else
                     curip.IPMask = "";
                 curip.AddrType = InterfaceState.stateAddrType.MIB_IPADDR_PRIMARY;
                 curif.InetAddr.Add(curip);
             }
         }
     }
     if (curif != null)
         mySysInfo.Interfaces.Add(curif);
 }
        private static void getInterfacesLinux(SshCommandLineRunner commandRunner, SysInfo mySysInfo)
        {
            uint           ifIndex = 0;
            InterfaceState curif   = null;

            char[] fieldseps = { ' ', '\t' };

            var output = commandRunner.ExecuteCommand("/sbin/ip addr show").SplitStringByDefaultNewLine();

            foreach (var line in output)
            {
                if ((line[0] >= '0') && (line[0] <= '9'))
                {
                    if (curif != null)
                    {
                        mySysInfo.Interfaces.Add(curif);
                    }
                    curif          = new InterfaceState();
                    curif.InetAddr = new List <InterfaceState.IPInfo>();
                    curif.Index    = ifIndex++;
                    var ifields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
                    curif.Name = ifields[1].Replace(":", "");
                }
                else
                {
                    string[] pfields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
                    if (pfields[0].StartsWith("link/"))
                    {
                        string itype = pfields[0].Remove(0, 5);
                        switch (itype)
                        {
                        case "ether":
                            curif.Type       = InterfaceState.stateInterfaceType.MIB_IF_TYPE_ETHERNET;
                            curif.IsPhysical = true;
                            break;

                        case "loopback":
                            curif.Type       = InterfaceState.stateInterfaceType.MIB_IF_TYPE_LOOPBACK;
                            curif.IsPhysical = false;
                            break;

                        case "ppp":
                            curif.Type       = InterfaceState.stateInterfaceType.MIB_IF_TYPE_PPP;
                            curif.IsPhysical = false;
                            break;

                        case "fddi":
                            curif.Type       = InterfaceState.stateInterfaceType.MIB_IF_TYPE_FDDI;
                            curif.IsPhysical = true;
                            break;

                        case "tr":
                            curif.Type       = InterfaceState.stateInterfaceType.MIB_IF_TYPE_TOKENRING;
                            curif.IsPhysical = true;
                            break;

                        case "slip":
                        case "cslip":
                        case "slip6":
                        case "cslip6":
                            curif.Type       = InterfaceState.stateInterfaceType.MIB_IF_TYPE_SLIP;
                            curif.IsPhysical = false;
                            break;

                        default:
                            curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_OTHER;
                            break;
                        }
                        curif.HWAddr = pfields[1];
                    }
                    else if (pfields[0] == "inet")
                    {
                        char[]   sepslash    = { '/' };
                        string[] addrAndMask = pfields[1].Split(sepslash);
                        InterfaceState.IPInfo curip;
                        curip.IPAddr = addrAndMask[0];
                        if (addrAndMask[1].Contains("."))
                        {
                            curip.IPMask = addrAndMask[1];
                        }
                        else
                        {
                            uint hostbits = uint.Parse(addrAndMask[1]);
                            if (hostbits <= 8)
                            {
                                curip.IPMask = String.Format("{0}.0.0.0", (0xFF00 >> (int)hostbits) & 0x00FF);
                            }
                            else if (hostbits <= 16)
                            {
                                curip.IPMask = String.Format("255.{0}.0.0", (0xFF00 >> (int)(hostbits - 8)) & 0x00FF);
                            }
                            else if (hostbits <= 24)
                            {
                                curip.IPMask = String.Format("255.255.{0}.0", (0xFF00 >> (int)(hostbits - 16)) & 0x00FF);
                            }
                            else if (hostbits <= 32)
                            {
                                curip.IPMask = String.Format("255.255.{0}.0", (0xFF00 >> (int)(hostbits - 24)) & 0x00FF);
                            }
                            else
                            {
                                throw new InvalidOperationException(String.Format("Netmask bits in '{0}' ({1}) outside acceptable range", curif.Name, hostbits));
                            }
                        }
                        curip.IPBcast  = TokenFollowing(pfields, "brd");
                        curip.AddrType = InterfaceState.stateAddrType.MIB_IPADDR_PRIMARY;
                        curif.InetAddr.Add(curip);
                    }
                }
            }
            if (curif != null)
            {
                mySysInfo.Interfaces.Add(curif);
            }
        }
        private static void getInterfacesAIX(SshCommandLineRunner commandRunner, SysInfo mySysInfo)
        {
            uint           ifIndex = 0;
            InterfaceState curif   = null;

            char[] fieldseps = { ' ', '\t' };
            var    output    = commandRunner.ExecuteCommand("ifconfig -a").SplitStringByDefaultNewLine();

            foreach (var line in output)
            {
                if (!char.IsWhiteSpace(line[0]))
                {
                    if (curif != null)
                    {
                        getMACAIX(commandRunner, curif);
                        mySysInfo.Interfaces.Add(curif);
                    }
                    curif          = new InterfaceState();
                    curif.InetAddr = new List <InterfaceState.IPInfo>();
                    curif.Index    = ifIndex++;
                    string[] ifields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
                    curif.Name = ifields[0].Replace(":", "");
                    string iflags = ifields[1].Replace("<", ",").Replace(">", ",");
                    if (iflags.Contains(",LOOPBACK,"))
                    {
                        curif.Type       = InterfaceState.stateInterfaceType.MIB_IF_TYPE_LOOPBACK;
                        curif.IsPhysical = false;
                    }
                    else if (curif.Name.StartsWith("en") || curif.Name.StartsWith("et"))
                    {
                        curif.Type       = InterfaceState.stateInterfaceType.MIB_IF_TYPE_ETHERNET;
                        curif.IsPhysical = true;
                    }
                    else
                    {
                        curif.Type = InterfaceState.stateInterfaceType.MIB_IF_TYPE_OTHER;
                    }
                }
                else
                {
                    string[] pfields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
                    if (pfields[0] == "inet")
                    {
                        InterfaceState.IPInfo curip = new InterfaceState.IPInfo();
                        curip.IPAddr = pfields[1];
                        if ((pfields.Length > 5) && (pfields[4] == "broadcast"))
                        {
                            curip.IPBcast = pfields[5];
                        }
                        else
                        {
                            curip.IPBcast = "";
                        }
                        if ((pfields.Length > 3) && (pfields[2] == "netmask"))
                        {
                            if (pfields[3].Contains("."))
                            {
                                curip.IPMask = pfields[3];
                            }
                            else if (pfields[3].StartsWith("0x"))
                            {
                                UInt32 masknum = Convert.ToUInt32(pfields[3].Substring(2), 16);
                                curip.IPMask = String.Format("{0}.{1}.{2}.{3}",
                                                             masknum >> 24, (masknum >> 16) & 0x00FF, (masknum >> 8) & 0x00FF, masknum & 0x00FF);
                            }
                        }
                        else
                        {
                            curip.IPMask = "";
                        }
                        curip.AddrType = InterfaceState.stateAddrType.MIB_IPADDR_PRIMARY;
                        curif.InetAddr.Add(curip);
                    }
                }
            }
            if (curif != null)
            {
                mySysInfo.Interfaces.Add(curif);
            }
        }