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(); } } }
private void SetValues() { chkExcludeTrunks.Checked = Properties.Settings.Default.ExcludeTrunksSearch; txtUserName.Text = Properties.Settings.Default.DefaultUserName; txtPassword.Text = Functions.DecryptPassword(Properties.Settings.Default.DefaultEncryptedPassword); }