Example #1
0
        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            treeView1.SelectedNode = treeView1.Nodes[0];
            if (e.Result == null)
            {
                populateTabData();
            }
            else
            {
                SwitchInfo sw    = (SwitchInfo)e.Result;
                SwitchForm swFrm = null;
                foreach (Form frm in MdiChildren)
                {
                    if (frm.Name.Equals(sw.Name))
                    {
                        swFrm = (SwitchForm)frm;
                        break;
                    }
                }

                if (swFrm != null)
                {
                    swFrm.AddData();
                }

                treeView1.SelectedNode = treeView1.Nodes[sw.Name];
            }
            waitMode(false);
        }
Example #2
0
 public EditSwitchForm(SwitchInfo sw)
 {
     InitializeComponent();
     editingSwitch    = sw;
     txtName.Text     = sw.Name;
     txtAddress.Text  = sw.Address;
     txtUserName.Text = sw.UserName;
     txtPassword.Text = sw.Password;
 }
Example #3
0
        public static void deleteSwitch(SwitchInfo swtch)
        {
            string filename = DATA_PATH + swtch.Name + ".xml";

            MainForm.switches.Remove(swtch);

            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
        }
Example #4
0
        private void editSwitchNode()
        {
            SwitchInfo sw = getSwitch(treeView1.SelectedNode.Name);

            Form         frm = new EditSwitchForm(sw);
            DialogResult dr  = frm.ShowDialog(this);

            if (dr == DialogResult.OK)
            {
                populateTabData();
                switchUpdated = true;
            }
        }
Example #5
0
        public SwitchForm(SwitchInfo switchInfo)
        {
            InitializeComponent();

            if (switchInfo == null)
            {
                return;
            }

            sw        = switchInfo;
            this.Text = sw.Name;
            AddData();
        }
Example #6
0
        private void mACAddressesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dgvSearch.SelectedRows[0];
            SwitchInfo      sw  = MainForm.getSwitch(row.Cells[1].Value.ToString());

            if (sw == null)
            {
                return;
            }
            SwitchPort prt = sw.getPort(row.Cells[2].Value.ToString());

            if (prt == null)
            {
                return;
            }
            showMacs(prt);
        }
Example #7
0
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems == null || listView1.SelectedItems.Count < 1)
            {
                return;
            }

            SwitchInfo sw = MainForm.getSwitch(listView1.SelectedItems[0].SubItems[0].Text);

            Form         frm = new EditSwitchForm(sw);
            DialogResult dr  = frm.ShowDialog(this);

            if (dr == DialogResult.OK)
            {
                update = true;
                populateListData();
            }
        }
Example #8
0
        private void connectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SwitchInfo sw = getSwitch(treeView1.SelectedNode.Name);

            Process telnetProcess = new Process();
            string  cmdPath       = System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"), @"system32\telnet.exe");

            if (!File.Exists(cmdPath))
            {
                cmdPath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"), @"sysnative\telnet.exe");
            }
            if (!File.Exists(cmdPath))
            {
                MessageBox.Show("Unable to find Telnet exectuable.", "Error", MessageBoxButtons.OK);
                return;
            }

            telnetProcess.StartInfo.FileName  = cmdPath;
            telnetProcess.StartInfo.Arguments = sw.Address;
            telnetProcess.Start();
        }
Example #9
0
        private bool addSwitch()
        {
            foreach (SwitchInfo sw in MainForm.switches)
            {
                if (sw.Name.ToLower().Equals(txtName.Text.ToLower().Trim()))
                {
                    MessageBox.Show("That switch name is already in use, please try something else.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return(false);
                }

                if (sw.Address.ToLower().Equals(txtAddress.Text.ToLower().Trim()))
                {
                    MessageBox.Show("That switch address is already in use, please try something else.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return(false);
                }
            }

            SwitchInfo swtch = new SwitchInfo(txtName.Text, txtAddress.Text, txtUserName.Text, Functions.EncryptPassword(txtPassword.Text));

            MainForm.switches.Add(swtch);
            return(true);
        }
Example #10
0
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("If you have a lot of switches selected, updating switch data can take a very long time.\n\nAre you sure you wish to continue?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            if (dr != DialogResult.Yes)
            {
                return;
            }

            if (listView1.SelectedItems == null)
            {
                MessageBox.Show("Please select the switch(es) you want to update");
                return;
            }

            Collection <SwitchInfo> switchList = new Collection <SwitchInfo>();

            foreach (ListViewItem itm in listView1.SelectedItems)
            {
                SwitchInfo sw = MainForm.getSwitch(itm.SubItems[0].Text);
                if (sw == null)
                {
                    MessageBox.Show("Error occurred finding switch data for switch " + itm.SubItems[0].Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    continue;
                }
                switchList.Add(sw);
            }

            _mainForm.progressBar1.Maximum = switchList.Count * 100;
            _mainForm.waitMode(true, "Gathering Data...", true);
            if (!_mainForm.switchUpdated)
            {
                _mainForm.switchUpdated = true;
            }
            _mainForm.worker.RunWorkerAsync(switchList);
            Close();
        }
Example #11
0
        public void gotoSwitch(string swtch, string port)
        {
            SwitchInfo sw = getSwitch(swtch);

            if (sw == null)
            {
                return;
            }
            SwitchPort prt = sw.getPort(port);

            if (prt == null)
            {
                return;
            }

            treeView1.SelectedNode = null;
            treeView1.SelectedNode = treeView1.Nodes[sw.Name];

            foreach (Form frm in MdiChildren)
            {
                if (frm.Text.Equals(sw.Name))
                {
                    SwitchForm swFrm = (SwitchForm)frm;
                    for (int j = 0; j < swFrm.dgvSwitch.RowCount; j++)
                    {
                        if (swFrm.dgvSwitch.Rows[j].Cells[0].Value.ToString() == prt.Name)
                        {
                            swFrm.dgvSwitch.ClearSelection();
                            swFrm.dgvSwitch.Rows[j].Selected = true;
                            swFrm.dgvSwitch.FirstDisplayedScrollingRowIndex = j;
                            break;
                        }
                    }
                    break;
                }
            }
        }
Example #12
0
        private void updateCurrentSwitchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null || treeView1.SelectedNode == treeView1.Nodes[0])
            {
                MessageBox.Show("Please select the switch you want to update");
                return;
            }

            SwitchInfo sw = getSwitch(treeView1.SelectedNode.Name);

            if (sw == null)
            {
                MessageBox.Show("Error occurred finding switch data!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            this.progressBar1.Maximum = 100;
            waitMode(true, "Gathering Data...", true);
            if (!switchUpdated)
            {
                switchUpdated = true;
            }
            worker.RunWorkerAsync(sw);
        }
Example #13
0
        private void btnImport_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            try
            {
                using (CsvFileReader reader = new CsvFileReader(openFileDialog1.OpenFile()))
                {
                    CsvRow row = new CsvRow();
                    reader.ReadRow(row);

                    int nameIndex    = row.FindIndex(delegate(string str) { return(str == "Name"); });
                    int addressIndex = row.FindIndex(delegate(string str) { return(str == "Network Address"); });
                    int userIndex    = row.FindIndex(delegate(string str) { return(str == "Username"); });
                    int passIndex    = row.FindIndex(delegate(string str) { return(str == "Password"); });

                    if (nameIndex == -1 || addressIndex == -1)
                    {
                        MessageBox.Show("Please ensure your CSV file contains the following column names in the header row:\r\n\r\nName\r\nNetwork Address\r\nUsername (Optional)\r\nPassword (Optional)",
                                        "Invalid Header Row", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    DialogResult dr = MessageBox.Show("Any existing switches with the same name or address in the imported data will be removed and is irreversible.\r\n\r\nAre you sure you wish to continue?",
                                                      "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                    if (dr != DialogResult.Yes)
                    {
                        return;
                    }

                    bool added = false;

                    while (reader.ReadRow(row))
                    {
                        string nameVal    = "";
                        string addressVal = "";
                        string userVal    = "";
                        string passVal    = "";

                        nameVal    = row.ElementAt(nameIndex).Trim();
                        addressVal = row.ElementAt(addressIndex).Trim();
                        if (userIndex > -1)
                        {
                            userVal = row.ElementAt(userIndex).Trim();
                        }
                        if (passIndex > -1)
                        {
                            passVal = row.ElementAt(passIndex);
                        }

                        if (string.IsNullOrEmpty(nameVal) || string.IsNullOrEmpty(addressVal))
                        {
                            continue;
                        }

                        // delete any existing switches
                        List <SwitchInfo> dellist = new List <SwitchInfo>();
                        foreach (SwitchInfo sw in MainForm.switches)
                        {
                            if (sw.Name.ToLower().Trim().Equals(nameVal.ToLower()))
                            {
                                dellist.Add(sw);
                            }

                            if (sw.Address.ToLower().Trim().Equals(addressVal.ToLower()))
                            {
                                dellist.Add(sw);
                            }
                        }
                        foreach (SwitchInfo sw in dellist)
                        {
                            MainForm.deleteSwitch(sw);
                        }

                        // Add new switch
                        SwitchInfo swtch = new SwitchInfo(nameVal, addressVal, userVal, Functions.EncryptPassword(passVal));
                        MainForm.switches.Add(swtch);
                        added = true;
                    }

                    if (added)
                    {
                        update = true;
                        populateListData();
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
Example #14
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();
                }
            }
        }
Example #15
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);
        }