/// <summary> /// Get the List of VLAN's configured on Network Switch /// </summary> /// <returns>List of <see cref="VirtualLAN"/></returns> public Collection <VirtualLAN> GetAvailableVirtualLans() { Collection <VirtualLAN> vlans = new Collection <VirtualLAN>(); string ipData = string.Empty; string identifierData = string.Empty; try { using (TelnetIpc telnet = new TelnetIpc(IPAddress.ToString(), 23)) { telnet.Connect(); telnet.SendLine(" "); Thread.Sleep(1000); telnet.SendLine("show ip"); Thread.Sleep(3000); ipData = telnet.ReceiveUntilMatch("$"); telnet.SendLine(" "); telnet.SendLine("show vlan"); Thread.Sleep(3000); // Gets the identifier data which contains VLAN numbers. identifierData = telnet.ReceiveUntilMatch("$"); } // Once the connection is disposed, a wait is required to make sure that the connection is closed Thread.Sleep(TimeSpan.FromMilliseconds(1000)); vlans = GetVLans(ipData, identifierData); } catch (Exception ex) { Logger.LogInfo(ex.Message); } return(vlans); }
/// <summary> /// Gets all the radius servers configured on the switch. /// </summary> /// <returns></returns> public Collection <IPAddress> GetRadiusServers() { string configData = string.Empty; using (TelnetIpc telnet = new TelnetIpc(IPAddress.ToString(), 23)) { telnet.Connect(); telnet.SendLine("/n"); telnet.SendLine("show run"); Thread.Sleep(TimeSpan.FromSeconds(10)); configData = telnet.ReceiveUntilMatch("$"); string data = configData; while (data.Contains("more", StringComparison.CurrentCultureIgnoreCase)) { telnet.SendLine(" "); Thread.Sleep(TimeSpan.FromSeconds(10)); data = telnet.ReceiveUntilMatch("$"); configData += data; } configData = configData.Replace(controlCharacters, string.Empty).Replace("/r", string.Empty).Trim(); } // Wait time for the telnet connection to dispose Thread.Sleep(TimeSpan.FromSeconds(30)); string[] radiusServerData = Array.FindAll(configData.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries), x => x.Contains("radius-server", StringComparison.CurrentCultureIgnoreCase)); Regex ipAddressRegex = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"); return(new Collection <IPAddress>(radiusServerData.Select(x => IPAddress.Parse(ipAddressRegex.Match(x).Value)).ToArray())); }
/// <summary> /// Get log details of Network Switch /// </summary> /// <returns>Log Data of Network Switch</returns> public string GetLog() { string logData = string.Empty; using (TelnetIpc telnet = new TelnetIpc(IPAddress.ToString(), 23)) { try { telnet.Connect(); telnet.SendLine(" "); Thread.Sleep(1000); telnet.SendLine("show log -r"); Thread.Sleep(3000); string data = telnet.ReceiveUntilMatch("$"); while (data.Contains("-- MORE --")) { logData += data; telnet.SendLine(" "); Thread.Sleep(3000); data = telnet.ReceiveUntilMatch("$"); } logData += data; } catch (Exception ex) { Logger.LogInfo(ex.Message); } } return(logData); }
/// <summary> /// Gets the available virtual Lans /// </summary> private void GetRouterVirtualLans() { string ipData = string.Empty; string identifierData = string.Empty; using (TelnetIpc telnet = new TelnetIpc(_adress.ToString(), 23)) { telnet.Connect(); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine(_userName); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine(_password); Thread.Sleep(TimeSpan.FromSeconds(5)); // Get the VLAN IP details telnet.SendLine("show ip"); Thread.Sleep(TimeSpan.FromSeconds(10)); string data = string.Empty; data = telnet.ReceiveUntilMatch("$"); while (data.Contains("more", StringComparison.CurrentCultureIgnoreCase)) { data = data.Remove(data.IndexOf("-- more", StringComparison.CurrentCultureIgnoreCase)); telnet.SendLine(" "); Thread.Sleep(TimeSpan.FromSeconds(10)); data += telnet.ReceiveUntilMatch("$"); } ipData = data.Replace(controlCharacters, string.Empty);; data = string.Empty; // Gets the Virtual LAN Id details telnet.SendLine("show vlan"); Thread.Sleep(TimeSpan.FromSeconds(10)); data = telnet.ReceiveUntilMatch("$"); while (data.Contains("more", StringComparison.CurrentCultureIgnoreCase)) { data = data.Remove(data.IndexOf("-- more", StringComparison.CurrentCultureIgnoreCase)); telnet.SendLine(" "); Thread.Sleep(TimeSpan.FromSeconds(10)); data += telnet.ReceiveUntilMatch("$"); } identifierData = data.Replace(controlCharacters, string.Empty); } Thread.Sleep(TimeSpan.FromSeconds(30)); _routerVlans = GetVlanDetails(ipData, identifierData); }
/// <summary> /// Get the SwitchType using IP Address of the Network Switch /// </summary> /// <param name="ipAddress">IP Address of Network Switch</param> /// <returns><see cref="SwitchType"/></returns> private static SwitchType GetSwitchType(IPAddress ipAddress) { TelnetIpc telnet = new TelnetIpc(ipAddress.ToString(), 23); SwitchType switchType = SwitchType.None; try { telnet.Connect(); telnet.SendLine(" "); Thread.Sleep(1000); // This command is tested currently only for HP ProCurve switch, need to check for other switches how it behaves telnet.SendLine("show config"); Thread.Sleep(3000); string data = telnet.ReceiveUntilMatch("$"); // HP manufactured switch can contain any of the strings. // ProCurve, Hewlett-Packard, Hewlett Packard if (data.Contains("ProCurve", StringComparison.CurrentCultureIgnoreCase) || data.Contains("Hewlett-Packard", StringComparison.CurrentCultureIgnoreCase) || data.Contains("Hewlett Packard", StringComparison.CurrentCultureIgnoreCase)) { switchType = SwitchType.HPProCurve; } } finally { telnet.Dispose(); Thread.Sleep(1000); } return(switchType); }
/// <summary> /// Get the RouterType using IP Address of the Router. /// </summary> /// <param name="ipAddress">IP Address of the Router.</param> /// <param name="userName">User name credentials to the router.</param> /// <param name="password">Password credentials to the router.</param> /// <returns><see cref="RouterType"/></returns> private static RouterType GetRouterType(IPAddress ipAddress, string userName, string password) { TelnetIpc telnet = new TelnetIpc(ipAddress.ToString(), 23); RouterType routerType = RouterType.None; try { telnet.Connect(); telnet.SendLine(" "); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine(userName); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine(password); Thread.Sleep(TimeSpan.FromSeconds(20)); string data = telnet.ReceiveUntilMatch("$"); // HP manufactured router can contain any of the strings. // ProCurve, Hewlett-Packard, Hewlett Packard if (data.Contains("ProCurve", StringComparison.CurrentCultureIgnoreCase) || data.Contains("Hewlett-Packard", StringComparison.CurrentCultureIgnoreCase) || data.Contains("Hewlett Packard", StringComparison.CurrentCultureIgnoreCase)) { routerType = RouterType.HP; } } finally { telnet.Dispose(); Thread.Sleep(1000); } return(routerType); }
/// <summary> /// Gets the configured pots for the VLAN number. /// </summary> /// <param name="vlanIdentifier">The VLAN number.</param> /// <returns>The VLAN ports configured.</returns> public Collection <int> GetVlanPorts(int vlanIdentifier) { Collection <int> vlans = new Collection <int>(); string portData = string.Empty; try { using (TelnetIpc telnet = new TelnetIpc(IPAddress.ToString(), 23)) { telnet.Connect(); telnet.SendLine(" "); Thread.Sleep(1000); telnet.SendLine("show vlan {0}".FormatWith(vlanIdentifier)); Thread.Sleep(4000); portData = telnet.ReceiveUntilMatch("$"); } // Once the connection is disposed, a wait is required to make sure that the connection is closed Thread.Sleep(TimeSpan.FromMilliseconds(1000)); portData = portData.Replace("\r", string.Empty); string[] lines = portData.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); // Position at the VLAN Port Information table in the array int position = Array.FindIndex(lines, x => x.Contains("Port Information")); // If position is -1, no VLANs are configured on the switch. if (position == -1) { Logger.LogInfo("Configured port information is not available on the switch for VLAN : {0}.".FormatWith(vlanIdentifier)); return(vlans); } for (++position; position < lines.Count(); position++) { if (lines[position].Contains("Untagged")) { vlans.Add(Convert.ToInt32(lines[position].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[0], CultureInfo.CurrentCulture)); } } return(vlans); } catch (SocketException ex) { Logger.LogInfo(ex.Message); return(vlans); } }
/// <summary> /// Gets the details of a virtual LAN. /// </summary> /// <param name="routerVlanId">The virtual LAN id.</param> /// <returns><see cref="RouterVirtualLAN"/></returns> public RouterVirtualLAN GetVirtualLanDetails(int routerVlanId) { string vlanData = string.Empty; RouterVirtualLAN routerVlan = RouterVlans.Where(x => x.Identifier.Equals(routerVlanId)).FirstOrDefault(); using (TelnetIpc telnet = new TelnetIpc(_adress.ToString(), 23)) { telnet.Connect(); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine(_userName); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine(_password); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine("en"); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine("show run vlan {0}".FormatWith(routerVlanId)); Thread.Sleep(TimeSpan.FromSeconds(10)); vlanData = telnet.ReceiveUntilMatch("$"); while (vlanData.Contains("more", StringComparison.CurrentCultureIgnoreCase)) { vlanData = vlanData.Remove(vlanData.IndexOf("-- more", StringComparison.CurrentCultureIgnoreCase)); telnet.SendLine(" "); Thread.Sleep(TimeSpan.FromSeconds(10)); vlanData += telnet.ReceiveUntilMatch("$"); } vlanData = vlanData.Replace(controlCharacters, string.Empty).Replace("/r", string.Empty).Trim(); } GetVlanDetails(vlanData, ref routerVlan); return(routerVlan); }
/// <summary> /// Delete helper address for the specified virtual LAN. /// </summary> /// <param name="helperAddress">The helper address.</param> /// <returns>True if the helper address is deleted, else false.</returns> public bool DeleteHelperAddress(int routerVlanId, IPAddress helperAddress) { Logger.LogInfo("Deleting helper-address:{0} on router VLAN : {1}".FormatWith(helperAddress, routerVlanId)); string command = string.Empty; if (helperAddress.AddressFamily == AddressFamily.InterNetwork) { command = "no ip helper-address {0}".FormatWith(helperAddress); } else if (helperAddress.AddressFamily == AddressFamily.InterNetworkV6) { command = "no ipv6 helper-address unicast {0}".FormatWith(helperAddress); } string data = string.Empty; using (TelnetIpc telnet = new TelnetIpc(_adress.ToString(), 23)) { telnet.Connect(); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine(_userName); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine(_password); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine("en"); telnet.SendLine("conf t"); telnet.SendLine("vlan {0}".FormatWith(routerVlanId)); telnet.SendLine(command); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine("wr mem"); Thread.Sleep(TimeSpan.FromSeconds(5)); // Validation telnet.SendLine("sh run vlan {0}".FormatWith(routerVlanId)); Thread.Sleep(TimeSpan.FromSeconds(10)); data = telnet.ReceiveUntilMatch("$"); while (data.Contains("more", StringComparison.CurrentCultureIgnoreCase)) { data = data.Remove(data.IndexOf("-- more", StringComparison.CurrentCultureIgnoreCase)); telnet.SendLine(" "); Thread.Sleep(TimeSpan.FromSeconds(10)); data += telnet.ReceiveUntilMatch("$"); } data = data.Replace(controlCharacters, string.Empty); } if (data.Contains(command.Replace("no ", string.Empty).Trim())) { Logger.LogInfo("Failed to delete helper-address: {0} on VLAN: {1}".FormatWith(helperAddress, routerVlanId)); return(false); } else { Logger.LogInfo("Deleted helper-address: {0} on VLAN: {1}".FormatWith(helperAddress, routerVlanId)); return(true); } }
/// <summary> /// Set the flags for a particular virtual LAN. /// </summary> /// <param name="routerVlanId">The virtual LAN id.</param> /// <param name="routerFlag"><see cref="RouterFlags"/></param> /// <returns>True if the flag is set, else false.</returns> public bool SetRouterFlag(int routerVlanId, RouterFlags routerFlag) { bool result = false; string flagDescription = RouterFlags.None == routerFlag ? "Disabling M and O" : "Setting {0}".FormatWith(routerFlag); Logger.LogInfo("{0} flag on router VLAN : {1}".FormatWith(flagDescription, routerVlanId)); using (TelnetIpc telnet = new TelnetIpc(_adress.ToString(), 23)) { telnet.Connect(); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine(_userName); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine(_password); Thread.Sleep(TimeSpan.FromSeconds(5)); telnet.SendLine("en"); telnet.SendLine("conf t"); telnet.SendLine("vlan {0}".FormatWith(routerVlanId)); // TODO: Remove multiple if else if (routerFlag == RouterFlags.Managed) { telnet.SendLine(Enum <RouterFlags> .Value(RouterFlags.Managed)); telnet.SendLine(("no {0}".FormatWith(Enum <RouterFlags> .Value(RouterFlags.Other)))); } else if (routerFlag == RouterFlags.Other) { telnet.SendLine(Enum <RouterFlags> .Value(RouterFlags.Other)); telnet.SendLine(("no {0}".FormatWith(Enum <RouterFlags> .Value(RouterFlags.Managed)))); } else if (routerFlag == RouterFlags.Both) { telnet.SendLine(Enum <RouterFlags> .Value(RouterFlags.Other)); telnet.SendLine(Enum <RouterFlags> .Value(RouterFlags.Managed)); } else { telnet.SendLine(("no {0}".FormatWith(Enum <RouterFlags> .Value(RouterFlags.Managed)))); telnet.SendLine(("no {0}".FormatWith(Enum <RouterFlags> .Value(RouterFlags.Other)))); } telnet.SendLine("wr mem"); Thread.Sleep(TimeSpan.FromSeconds(10)); telnet.SendLine("sh run vlan {0}".FormatWith(routerVlanId)); Thread.Sleep(TimeSpan.FromSeconds(10)); string vlanData = telnet.ReceiveUntilMatch("$"); while (vlanData.Contains("more", StringComparison.CurrentCultureIgnoreCase)) { vlanData = vlanData.Remove(vlanData.IndexOf("-- more", StringComparison.CurrentCultureIgnoreCase)); telnet.SendLine(" "); Thread.Sleep(TimeSpan.FromSeconds(10)); vlanData += telnet.ReceiveUntilMatch("$"); } vlanData = vlanData.Replace(controlCharacters, string.Empty); // TODO: Remove multiple if else's if (routerFlag == RouterFlags.Managed) { result = vlanData.Contains(Enum <RouterFlags> .Value(RouterFlags.Managed)) && !vlanData.Contains(Enum <RouterFlags> .Value(RouterFlags.Other)); } else if (routerFlag == RouterFlags.Other) { result = vlanData.Contains(Enum <RouterFlags> .Value(RouterFlags.Other)) && !vlanData.Contains(Enum <RouterFlags> .Value(RouterFlags.Managed)); } else if (routerFlag == RouterFlags.Both) { result = vlanData.Contains(Enum <RouterFlags> .Value(RouterFlags.Managed)) && vlanData.Contains(Enum <RouterFlags> .Value(RouterFlags.Other)); } else { result = !vlanData.Contains(Enum <RouterFlags> .Value(RouterFlags.Managed)) && !vlanData.Contains(Enum <RouterFlags> .Value(RouterFlags.Other)); } flagDescription = RouterFlags.None == routerFlag ? "disabled M and O" : "set {0}".FormatWith(routerFlag); if (result) { Logger.LogInfo("Successfully {0} flag on router VLAN : {1}".FormatWith(flagDescription, routerVlanId)); } else { Logger.LogInfo("Failed to {0} flag on router VLAN : {1}".FormatWith(flagDescription, routerVlanId)); } return(result); } }
/// <summary> /// Get the link speed of the specified port. /// </summary> /// <param name="portNumber">The port number.</param> /// <returns>The link speed for the specified port.</returns> /// Note : Can not return the <see cref="LinkSpeed"/> since there are multiple enum values mapped to the port link speed. /// eg: 1000FDX corresponds to auto and auto-1000 /// 100FDX corresponds to 100-full and auto-100 private string GetLinkSpeed(int portNumber) { /* ????[2J[?7l[3;23r[?6l[1;1H[?25l[1;1HProCurve J4903A Switch 2824 * Copyright (C) 1991-2009 Hewlett-Packard Co. All Rights Reserved. * RESTRICTED RIGHTS LEGEND * Use, duplication, or disclosure by the Government is subject to restrictions * as set forth in subdivision (b) (3) (ii) of the Rights in Technical Data and Computer Software clause at 52.227-7013. * HEWLETT-PACKARD COMPANY, 3000 Hanover St., Palo Alto, CA 94303 * We'd like to keep you up to date about: * Software feature updates * New product announcements * Special events * Please register your products now at: www.ProCurve.com * [24;1HPress any key to continue[1;1H[?25h[24;27H[2J[?7l[3;23r[?6l[24;27H[?25h[24;27H[?6l[1;24r[?7l[2J[24;27H[1;24r[24;27H[2J[?7l[1;24r[?6l[24;1H[1;24r[24;1H[24;1H[2K[24;1H[?25h[24;1H[24;1HProCurve Switch 2824# [24;1H[24;23H[24;1H[?25h[24;23H[24;23Hconf t[24;23H[?25h[24;29H[24;0HE[24;1H[24;29H[24;1H[2K[24;1H[?25h[24;1H[1;24r[24;1H[1;24r[24;1H[24;1H[2K[24;1H[?25h[24;1H[24;1HProCurve Switch 2824(config)# [24;1H[24;31H[24;1H[?25h[24;31H[24;31Hshow inter[24;31H[?25h[24;41H[24;41Hface brief[24;41H[?25h[24;51H[24;51H ethernet [24;51H[?25h[24;61H[24;61H24[24;61H[?25h[24;63H[24;0HE[24;1H[24;63H[24;1H[2K[24;1H[?25h[24;1H[1;24r[24;1H * Status and Counters - Port Status | Intrusion MDI Flow Bcast | Port Type | Alert Enabled Status Mode Mode Ctrl Limit | ----- --------- + --------- ------- ------ ---------- ----- ----- ------ | 24 100/1000T | No Yes Down 100FDx MDI off 0 | [1;24r[24;1H[24;1H[2K[24;1H[?25h[24;1H[24;1HProCurve Switch 2824(config)# [24;1H[24;31H[24;1H[?25h[24;31H*/ try { string result = string.Empty; using (TelnetIpc telnet = new TelnetIpc(IPAddress.ToString(), 23)) { telnet.Connect(); telnet.SendLine("/n"); telnet.SendLine("conf t"); telnet.SendLine("show interface brief ethernet {0}".FormatWith(portNumber)); Thread.Sleep(TimeSpan.FromSeconds(20)); result = telnet.ReceiveUntilMatch("$"); } Thread.Sleep(TimeSpan.FromMinutes(1)); if (!string.IsNullOrEmpty(result)) { result = result.Replace("\r", "").Trim(); string[] data = result.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); int index = Array.FindIndex(data, x => x.TrimStart().StartsWith("Port", StringComparison.CurrentCultureIgnoreCase)); if (-1 == index) { Logger.LogInfo("Failed to fetch the value of link speed for port: {0}.".FormatWith(portNumber)); return(string.Empty); } // Skip the next line and fetch the speed information for the port. string[] portSpeedData = data[index + 2].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); return(portSpeedData[6].Trim()); } else { Logger.LogInfo("Failed to fetch the value of link speed for port: {0}.".FormatWith(portNumber)); return(string.Empty); } } catch (SocketException ex) { Logger.LogInfo("Failed to fetch the value of link speed for port: {0}.".FormatWith(portNumber)); Logger.LogInfo(ex.Message); return(string.Empty); } catch (Exception ex) { Logger.LogInfo("Failed to fetch the value of link speed for port: {0}.".FormatWith(portNumber)); Logger.LogInfo(ex.Message); return(string.Empty); } }
/// <summary> /// Check whether a Network Switch Port is Disabled /// </summary> /// <param name="portNumber">Port Number of Network Switch</param> /// <returns>true if Disabled, false otherwise</returns> public bool IsPortDisabled(int portNumber) { bool isDisabled = false; // eg: Sample data for one interface // Positive case | Negative case // ----------------------------------- // interface 5 | interface 5 // disable | no lacp // no lacp | exit // exit | // telnet works with port number 23 using (TelnetIpc telnet = new TelnetIpc(IPAddress.ToString(), 23)) { try { telnet.Connect(); telnet.SendLine(" "); // send space to continue to actual telnet prompt telnet.SendLine("show run"); // command to show the configuration details of the switch Thread.Sleep(3000); string configData = string.Empty; while (true) { string data = telnet.ReceiveUntilMatch("$"); configData += data; // if output contains -- MORE -- , few more configurations are available to display // send space to get more details. if (data.Contains("-- MORE --")) { telnet.SendLine(" "); Thread.Sleep(3000); } else { // break the loop if it doesn't contain -- MORE -- break; } } string[] lines = configData.Split('\n'); // walk through each line from the configData for (int i = 0; i < lines.Length; i++) { string line = lines[i]; // check to see if the line contains "interface <port number>" if (line.Contains("interface {0}".FormatWith(portNumber))) { for (int j = i + 1; j < lines.Length; j++) { line = lines[j]; // if the interface is disabled then turn on the flag and break the loop. if (line.Contains("disable")) { isDisabled = true; break; } // if exit is reached then it is end of interface details. if (line.Contains("exit")) { break; } } break; } } } catch (Exception ex) { Logger.LogInfo(ex.Message); } } // Once the connection is disposed, a wait is required to make sure that the connection is closed Thread.Sleep(TimeSpan.FromMilliseconds(1000)); return(isDisabled); }