Ejemplo n.º 1
0
        private void updateSwitch(SwitchInfo sw)
        {
            //If no username specified use defaults for username and password
            string username = (string.IsNullOrEmpty(sw.UserName) ? Properties.Settings.Default.DefaultUserName : sw.UserName);
            string password = (string.IsNullOrEmpty(sw.UserName) ? Functions.DecryptPassword(Properties.Settings.Default.DefaultEncryptedPassword) : sw.Password);

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                if (InvokeRequired)
                {
                    Invoke(new UpdateStatus(updateStatus), "Credentials invalid for " + sw.Name + ".");
                }
                if (failedSwitches != null)
                {
                    failedSwitches.Add(sw.Name);
                }
                else
                {
                    if (InvokeRequired)
                    {
                        Invoke(new ShowWarning(showWarning), "Credentials are not set correctly to access " + sw.Name + ".", "Error", false);
                    }
                }
                return;
            }

            if (InvokeRequired)
            {
                Invoke(new UpdateStatus(updateStatus), "Connecting to " + sw.Name + "...");
            }
            using (var sshclient = new SshClient(sw.Address, username, password))
            {
                try
                {
                    if (InvokeRequired)
                    {
                        Invoke(new UpdateStatus(updateStatus), "Gathering data for " + sw.Name + "...");
                    }

                    sw.Ports = new Collection <SwitchPort>();
                    sshclient.Connect();
                    var reply = sshclient.RunCommand("show interface description").Result;
                    sshclient.Disconnect();
                    string[] lines = getResultData(reply, "Interface");
                    if (lines != null && lines.Length > 1)
                    {
                        for (int i = 1; i < lines.Length; i++)
                        {
                            char[] lineChars = lines[i].ToCharArray();
                            string strInt    = "";
                            string strDesc   = "";
                            for (int j = 0; j < lineChars.Length; j++)
                            {
                                if (j < 31)
                                {
                                    strInt += lineChars[j];
                                }
                                if (j > 54)
                                {
                                    strDesc += lineChars[j];
                                }
                            }
                            strInt  = strInt.Trim();
                            strDesc = strDesc.Trim();
                            if (strInt.Length > 0)
                            {
                                sw.addPort(new SwitchPort(strInt, strDesc));
                            }
                        }
                    }

                    if (sw.Ports.Count < 1)
                    {
                        if (failedSwitches != null)
                        {
                            failedSwitches.Add(sw.Name);
                        }
                        else
                        {
                            if (InvokeRequired)
                            {
                                Invoke(new ShowWarning(showWarning), "Failed to collect data for switch " + sw.Name, "Error", false);
                            }
                        }

                        return;
                    }

                    sshclient.Connect();
                    reply = sshclient.RunCommand("show interface status").Result;
                    sshclient.Disconnect();
                    lines = getResultData(reply, "Port");
                    if (lines != null && lines.Length > 1)
                    {
                        for (int i = 1; i < lines.Length; i++)
                        {
                            char[] lineChars = lines[i].ToCharArray();
                            string strInt    = "";
                            string strStatus = "";
                            string strVlan   = "";
                            string strDuplex = "";
                            string strSpeed  = "";
                            string strType   = "";
                            for (int j = 0; j < lineChars.Length; j++)
                            {
                                if (j < 10)
                                {
                                    strInt += lineChars[j];
                                }
                                if (j >= 30 && j < 43)
                                {
                                    strStatus += lineChars[j];
                                }
                                if (j >= 43 && j < 54)
                                {
                                    strVlan += lineChars[j];
                                }
                                if (j >= 54 && j < 61)
                                {
                                    strDuplex += lineChars[j];
                                }
                                if (j >= 61 && j < 68)
                                {
                                    strSpeed += lineChars[j];
                                }
                                if (j >= 68)
                                {
                                    strType += lineChars[j];
                                }
                            }
                            strInt    = strInt.Trim();
                            strStatus = strStatus.Trim();
                            strVlan   = strVlan.Trim();
                            strDuplex = strDuplex.Trim();
                            strSpeed  = strSpeed.Trim();
                            strType   = strType.Trim();
                            if (strInt.Length > 0)
                            {
                                SwitchPort prt = sw.getPort(strInt);
                                if (prt != null)
                                {
                                    prt.Status = strStatus;
                                    prt.Vlan   = strVlan;
                                    prt.Duplex = strDuplex;
                                    prt.Speed  = strSpeed;
                                    prt.Type   = strType;
                                }
                            }
                        }
                    }

                    sshclient.Connect();
                    reply = sshclient.RunCommand("show ip arp").Result;
                    sshclient.Disconnect();
                    lines = getResultData(reply, "Protocol");
                    if (lines != null && lines.Length > 1)
                    {
                        for (int i = 1; i < lines.Length; i++)
                        {
                            char[] lineChars = lines[i].ToCharArray();
                            string strIp     = "";
                            string strMac    = "";
                            for (int j = 0; j < lineChars.Length; j++)
                            {
                                if (j >= 10 && j < 27)
                                {
                                    strIp += lineChars[j];
                                }
                                if (j >= 38 && j < 54)
                                {
                                    strMac += lineChars[j];
                                }
                            }
                            strIp  = strIp.Trim();
                            strMac = strMac.Trim();
                            if (strIp.Length > 6)
                            {
                                bool found = false;
                                foreach (string ip in ipList)
                                {
                                    if (ip.Equals(strIp))
                                    {
                                        foreach (string mac in ipList.GetValues(ip))
                                        {
                                            if (mac.Equals(strMac))
                                            {
                                                found = true;
                                            }
                                        }
                                    }
                                }
                                if (!found)
                                {
                                    ipList.Add(strIp, strMac);
                                }
                            }
                        }
                    }

                    sshclient.Connect();
                    reply = sshclient.RunCommand("show mac address").Result;
                    sshclient.Disconnect();
                    lines = getResultData(reply, "mac address");
                    if (lines != null && lines.Length > 1)
                    {
                        List <string> lineList = lines.ToList();
                        // Remove irrelevant multicast entries
                        if (lineList.FindIndex(r => r.Contains("Multicast Entries")) >= 0)
                        {
                            lineList.RemoveRange(lineList.FindIndex(r => r.Contains("Multicast Entries")), lineList.Count - lineList.FindIndex(r => r.Contains("Multicast Entries")));
                        }

                        foreach (SwitchPort prt in sw.Ports)
                        {
                            foreach (string line in lineList)
                            {
                                var portname = Regex.Match(line.TrimEnd(), @" [^ ]+$");
                                if (portname.Success && prt.Name.Equals(Regex.Replace(portname.Value, @" ([a-zA-Z][a-zA-Z])[a-zA-Z]*([0-9].*)", "$1$2"), StringComparison.OrdinalIgnoreCase))
                                {
                                    var macAdd = Regex.Match(line, @"([0-9a-f]{4}\.){2}[0-9a-f]{4}");
                                    if (macAdd.Success && !prt.getMacs().Contains(macAdd.Value))
                                    {
                                        prt.addMac(macAdd.Value);
                                    }
                                }
                            }
                            if (InvokeRequired)
                            {
                                Invoke(new UpdateProgress(updateProgress), Math.Max(100 / sw.Ports.Count, 1), true);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (failedSwitches != null)
                    {
                        failedSwitches.Add(sw.Name);
                    }
                    else
                    {
                        if (InvokeRequired)
                        {
                            Invoke(new ShowWarning(showWarning), "Failed to update switch " + sw.Name + "\r\n\r\n" + ex.Message, "Error", false);
                        }
                    }
                }
                finally
                {
                    sshclient.Disconnect();
                    sshclient.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        private void loadData()
        {
            if (!Directory.Exists(DATA_PATH))
            {
                return;
            }
            if (!File.Exists(DATA_PATH + "IP_List.txt"))
            {
                return;
            }

            waitMode(true, "Gathering Data...");

            using (StreamReader sr = new StreamReader(DATA_PATH + "IP_List.txt"))
            {
                string line = "";
                while ((line = sr.ReadLine()) != null)
                {
                    string[] parts = line.Split("\t".ToCharArray());

                    bool found = false;
                    foreach (string ip in ipList)
                    {
                        if (ip.Equals(parts[0]))
                        {
                            foreach (string mac in ipList.GetValues(ip))
                            {
                                if (mac.Equals(parts[1]))
                                {
                                    found = true;
                                }
                            }
                        }
                    }

                    if (!found)
                    {
                        ipList.Add(parts[0], parts[1]);
                    }
                }
            }

            XmlDocument xd = new XmlDocument();

            foreach (string filename in Directory.GetFiles(DATA_PATH, "*.xml"))
            {
                xd.Load(filename);
                XmlNode    swtch = xd.DocumentElement;
                SwitchInfo sw    = new SwitchInfo(swtch.Attributes["Name"].Value, swtch.Attributes["Address"].Value, swtch.Attributes["UserName"].Value, swtch.Attributes["Password"].Value);
                for (XmlNode port = swtch.FirstChild; port != null; port = port.NextSibling)
                {
                    SwitchPort prt = new SwitchPort(port.Attributes["Name"].Value, port.Attributes["Description"].Value);
                    prt.Status = port.Attributes["Status"].Value;
                    prt.Vlan   = port.Attributes["Vlan"].Value;
                    prt.Speed  = port.Attributes["Speed"].Value;
                    prt.Duplex = port.Attributes["Duplex"].Value;
                    prt.Type   = port.Attributes["Type"].Value;
                    for (XmlNode macAdd = port.FirstChild; macAdd != null; macAdd = macAdd.NextSibling)
                    {
                        prt.addMac(macAdd.Attributes["Address"].Value);
                    }
                    sw.addPort(prt);
                }
                switches.Add(sw);
            }

            populateTabData();

            waitMode(false);
        }