Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <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);
        }
Пример #3
0
        /// <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);
        }
Пример #4
0
        /// <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);
        }
Пример #5
0
        /// <summary>
        /// Set the link speed of the port to the specified value.
        /// </summary>
        /// <param name="portNumber">The port number to be configured.</param>
        /// <param name="linkSpeed"><see cref="LinkSpeed"/></param>
        /// <returns>True if the link speed is set, else false.</returns>
        public bool SetLinkSpeed(int portNumber, LinkSpeed linkSpeed)
        {
            try
            {
                Logger.LogInfo("Setting link speed: {0} on port number: {1}".FormatWith(Enum <LinkSpeed> .Value(linkSpeed), portNumber));

                using (TelnetIpc telnet = new TelnetIpc(IPAddress.ToString(), 23))
                {
                    telnet.Connect();
                    telnet.SendLine("/n");
                    telnet.SendLine("conf t");
                    telnet.SendLine("interface ethernet {0} speed-duplex {1}".FormatWith(portNumber, Enum <LinkSpeed> .Value(linkSpeed)));
                    Thread.Sleep(TimeSpan.FromSeconds(20));
                }

                Thread.Sleep(TimeSpan.FromMinutes(1));

                // Link Speed validation is removed as the switch link speed changes w.r.t printer connection on the port.
                Logger.LogInfo("Successfully set link speed: {0} on port number: {1}".FormatWith(Enum <LinkSpeed> .Value(linkSpeed), portNumber));
                return(true);
            }
            catch (SocketException ex)
            {
                Logger.LogInfo("Failed to set link speed: {0} on port number: {1}".FormatWith(Enum <LinkSpeed> .Value(linkSpeed), portNumber));
                Logger.LogInfo(ex.Message);
                return(false);
            }
            catch (Exception ex)
            {
                Logger.LogInfo("Failed to set link speed: {0} on port number: {1}".FormatWith(Enum <LinkSpeed> .Value(linkSpeed), portNumber));
                Logger.LogInfo(ex.Message);
                return(false);
            }
        }
Пример #6
0
        /// <summary>
        /// Enables 802.1X Authentication for the switch port
        /// </summary>
        /// <param name="portNumber">Port to be enabled</param>
        /// <param name="timeOut">time to wait after the telnet connection is disposed</param>
        public void EnableAuthenticatorPort(int portNumber, int timeOut = 1000)
        {
            Logger.LogInfo("Enabling authentication on port: {0}.".FormatWith(portNumber));

            using (TelnetIpc telnet = new TelnetIpc(IPAddress.ToString(), 23))
            {
                try
                {
                    telnet.Connect();
                    telnet.SendLine("/n");
                    telnet.SendLine("conf t");
                    telnet.SendLine(string.Format(CultureInfo.CurrentCulture, "aaa port-access authenticator {0}", portNumber));
                    Thread.Sleep(1000);
                    telnet.SendLine("wr mem");
                }
                catch (Exception ex)
                {
                    Logger.LogInfo(ex.Message);
                    throw;
                }
            }

            // Once the connection is disposed, a wait is required to make sure that the connection is closed
            Thread.Sleep(TimeSpan.FromMilliseconds(1000));
        }
Пример #7
0
        /// <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()));
        }
Пример #8
0
        /// <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);
            }
        }
Пример #9
0
        /// <summary>
        /// Configure radius server on the switch.
        /// </summary>
        /// <param name="radiusServer">IP address of the radius server.</param>
        /// <param name="sharedSecret">Shared secret for the radius server configuration.</param>
        /// <returns>True if the configuration is successful, else false.</returns>
        public bool ConfigureRadiusServer(IPAddress radiusServer, string sharedSecret)
        {
            try
            {
                using (TelnetIpc telnet = new TelnetIpc(IPAddress.ToString(), 23))
                {
                    telnet.Connect();
                    telnet.SendLine("/n");
                    telnet.SendLine("conf t");
                    telnet.SendLine("aaa authentication port-access eap-radius");
                    telnet.SendLine("radius-server host {0} key {1}".FormatWith(radiusServer.ToString(), sharedSecret));
                    Thread.Sleep(TimeSpan.FromSeconds(10));
                    telnet.SendLine("aaa port-access authenticator active");
                    Thread.Sleep(TimeSpan.FromSeconds(10));
                }

                Thread.Sleep(TimeSpan.FromSeconds(20));

                if (GetRadiusServers().Contains(radiusServer))
                {
                    Logger.LogInfo("Successfully configured the radius server: {0} on the switch.".FormatWith(radiusServer));
                    return(true);
                }
                else
                {
                    Logger.LogInfo("Failed to configure the radius server: {0} on the switch.".FormatWith(radiusServer));
                    return(false);
                }
            }
            catch (SocketException ex)
            {
                Logger.LogInfo("Failed to configure the radius server: {0} on the switch.".FormatWith(radiusServer));
                Logger.LogInfo(ex.Message);
                return(false);
            }
            catch (Exception ex)
            {
                Logger.LogInfo("Failed to configure the radius server: {0} on the switch.".FormatWith(radiusServer));
                Logger.LogInfo(ex.Message);
                return(false);
            }
        }
Пример #10
0
        /// <summary>
        /// DeConfigure/Remove radius server details from the switch.
        /// </summary>
        /// <returns>True if the configuration is successful, else false.</returns>
        public bool DeconfigureRadisuServer(IPAddress radiusServer)
        {
            try
            {
                using (TelnetIpc telnet = new TelnetIpc(IPAddress.ToString(), 23))
                {
                    telnet.Connect();
                    telnet.SendLine("/n");
                    telnet.SendLine("conf t");
                    telnet.SendLine("no radius-server host {0}".FormatWith(radiusServer.ToString()));
                    Thread.Sleep(TimeSpan.FromSeconds(10));
                }

                Thread.Sleep(TimeSpan.FromSeconds(20));

                if (!GetRadiusServers().Contains(radiusServer))
                {
                    Logger.LogInfo("Successfully removed the radius server: {0} on the switch.".FormatWith(radiusServer));
                    return(true);
                }
                else
                {
                    Logger.LogInfo("Failed to remove the radius server: {0} on the switch.".FormatWith(radiusServer));
                    return(false);
                }
            }
            catch (SocketException ex)
            {
                Logger.LogInfo("Failed to remove the radius server: {0} on the switch.".FormatWith(radiusServer));
                Logger.LogInfo(ex.Message);
                return(false);
            }
            catch (Exception ex)
            {
                Logger.LogInfo("Failed to remove the radius server: {0} on the switch.".FormatWith(radiusServer));
                Logger.LogInfo(ex.Message);
                return(false);
            }
        }
Пример #11
0
        /// <summary>
        /// Changes the network of a port by keeping the port in the destination VLAN.
        /// </summary>
        /// <param name="portNumber">Port number to be changed to destination VLAN.</param>
        /// <param name="destinationVlanIdentifer">The destination VLAN identifier.</param>
        /// <returns>true if successfully changed the port to the specified VLAN, else false</returns>
        public bool ChangeVirtualLan(int portNumber, int destinationVlanIdentifer)
        {
            try
            {
                using (TelnetIpc telnet = new TelnetIpc(IPAddress.ToString(), 23))
                {
                    telnet.Connect();
                    telnet.SendLine("/n");
                    telnet.SendLine("conf t");
                    telnet.SendLine("vlan {0}".FormatWith(destinationVlanIdentifer));
                    Thread.Sleep(TimeSpan.FromMilliseconds(1000));
                    telnet.SendLine("untagged {0}".FormatWith(portNumber));
                    Thread.Sleep(TimeSpan.FromMilliseconds(1000));
                    telnet.SendLine("wr mem");
                    Thread.Sleep(TimeSpan.FromMilliseconds(1000));
                }

                // Once the connection is disposed, a wait is required to make sure that the connection is closed
                Thread.Sleep(TimeSpan.FromMilliseconds(1000));

                // Validate the port number in destination VLAN
                if (GetVlanPorts(destinationVlanIdentifer).Contains(portNumber))
                {
                    Logger.LogInfo("Successfully changed the network of port number {0} to vlan {1}.".FormatWith(portNumber, destinationVlanIdentifer));
                    return(true);
                }
                else
                {
                    Logger.LogInfo("Failed to change the network of port number {0} to vlan {1}.".FormatWith(portNumber, destinationVlanIdentifer));
                    return(false);
                }
            }
            catch (SocketException ex)
            {
                Logger.LogInfo("Socket Exception occurred: {0}.".FormatWith(ex.Message));
                return(false);
            }
        }
Пример #12
0
        /// <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);
        }
Пример #13
0
        /// <summary>
        /// Enable/ Disable a Network Switch Port
        /// </summary>
        /// <param name="portNo">Port Number of Network Switch</param>
        /// <param name="enablePort">true to Enable, false to Disable</param>
        /// <returns>true if successful, false otherwise</returns>
        private bool EnablePort(int portNo, bool enablePort)
        {
            bool   result = false;
            string enable = (enablePort.Equals(true) ? " enable" : " disable");

            Logger.LogInfo("{0} port: {1}.".FormatWith(enable, portNo));

            string[] commands = { " ", "configure",
                                  "interface ethernet " + portNo + enable };
            using (TelnetIpc telnet = new TelnetIpc(IPAddress.ToString(), 23))
            {
                try
                {
                    telnet.Connect();

                    for (int i = 0; i < commands.Length; i++)
                    {
                        telnet.SendLine(commands[i]);
                        Thread.Sleep(1000);
                    }

                    result = IsPortDisabled(portNo);
                }
                catch (Exception ex)
                {
                    Logger.LogInfo(ex.Message);
                    return(false);
                }
            }

            // Once the connection is disposed, a wait is required to make sure that the connection is closed
            Thread.Sleep(TimeSpan.FromMilliseconds(1000));

            result = enablePort.Equals(false) ? result : !result;

            if (result)
            {
                Logger.LogInfo("Successfully {0}d port: {1}.".FormatWith(enable, portNo));
            }
            else
            {
                Logger.LogInfo("Failed to {0} port: {1}.".FormatWith(enable, portNo));
            }

            return(result);
        }
Пример #14
0
        /// <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);
        }
Пример #15
0
        /// <summary>
        /// Set the Lease time.
        /// </summary>
        /// <param name="routerVlanId">The virtual LAN id.</param>
        /// <param name="validLifeTime"><see cref="LeaseTime"/></param>
        /// <param name="preferredLifeTime">The lease time.</param>
        /// <returns>True if the lease time is set, else false.</returns>
        public bool SetLeaseTime(int routerVlanId, TimeSpan validLifeTime, TimeSpan preferredLifeTime)
        {
            RouterVirtualLAN routerVlan = GetVirtualLanDetails(routerVlanId);

            Collection <RouterIPv6Details> ipv6Details = routerVlan.IPv6Details;

            string setLifeTime   = "ipv6 nd ra prefix {0}/{1} {2} {3}\n";
            string clearLifeTime = "no ipv6 nd ra prefix {0}/{1}\n";
            string command       = string.Empty;

            if (validLifeTime.TotalSeconds < preferredLifeTime.TotalSeconds)
            {
                Logger.LogInfo("Valid life time should be more than preferred life time.");
                return(false);
            }

            foreach (RouterIPv6Details v6Details in ipv6Details)
            {
                command += clearLifeTime.FormatWith(v6Details.AddressPrefix, v6Details.PrefixLength);
                command += setLifeTime.FormatWith(v6Details.AddressPrefix, v6Details.PrefixLength, validLifeTime.TotalSeconds, preferredLifeTime.TotalSeconds);
            }

            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);
                telnet.SendLine("wr mem");
                Thread.Sleep(TimeSpan.FromSeconds(10));
            }

            routerVlan = GetVirtualLanDetails(routerVlanId);

            return(routerVlan.IPv6Details.Any(x => !x.ValidLeaseTime.Equals(validLifeTime.TotalSeconds)) &&
                   routerVlan.IPv6Details.Any(x => !x.PreferredLeaseTime.Equals(preferredLifeTime.TotalSeconds)));
        }
Пример #16
0
        /// <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);
            }
        }
Пример #17
0
        /// <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);
            }
        }
Пример #18
0
        /// <summary>
        /// Disable IPv6 for a particular virtual LAN.
        /// </summary>
        /// <param name="routerVlanId">The virtual LAN id.</param>
        /// <returns>True if IPv6 is disabled, else false.</returns>
        public bool DisableIPv6Address(int routerVlanId)
        {
            Logger.LogInfo("Disabling IPv6 addresses in router.");

            RouterVirtualLAN routerVlan = GetVirtualLanDetails(routerVlanId);

            Collection <RouterIPv6Details> routerIpv6Details = routerVlan.IPv6Details;


            SetRouterFlag(routerVlanId, RouterFlags.None);

            // TODO: Remove hard coded prefix value.

            string ipData          = "no ipv6 address {0}/{1}\n";
            string prefixData      = "no ipv6 nd ra prefix {0}/{1}\n";
            string disableIPv6     = "no ipv6 enable\n";
            string reachableTime   = "no ipv6 nd reachable-time\n";
            string reachableTimera = "no ipv6 nd ra reachable-time\n";
            string linklocal       = "no ipv6 address {0} link-local\n".FormatWith(routerVlan.LinkLocalAddress);
            //  string ipv6helperAddress = "no ipv6 helper-address unicast {0}\n".FormatWith(routerVlan.IPv6HelperAddress);

            string command = string.Empty;

            command += disableIPv6;
            foreach (RouterIPv6Details ipv6Details in routerIpv6Details)
            {
                if (!ipv6Details.IPv6Address.Equals(IPAddress.IPv6None))
                {
                    command += ipData.FormatWith(ipv6Details.IPv6Address, ipv6Details.PrefixLength);
                }

                command += prefixData.FormatWith(ipv6Details.AddressPrefix, ipv6Details.PrefixLength);
            }
            command += reachableTime;
            command += reachableTimera;
            command += linklocal;
            // command += ipv6helperAddress;

            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);

                telnet.SendLine("wr mem");
                Thread.Sleep(TimeSpan.FromSeconds(10));
            }

            // Validation
            routerVlan = GetVirtualLanDetails(routerVlanId);

            if (routerVlan.IPv6Details.Count == 0)
            {
                Logger.LogInfo("Successfully disabled IPv6 addresses on VLAN: {0}".FormatWith(routerVlanId));
                return(true);
            }
            else
            {
                Logger.LogInfo("Failed to disable IPv6 addresses on VLAN: {0}".FormatWith(routerVlanId));
                return(false);
            }
        }
Пример #19
0
        /// <summary>
        /// Enable IPv6 for a particular virtual LAN.
        /// </summary>
        /// <param name="routerVlanId">The virtual LAN id.</param>
        /// <param name="ipv6Addresses">List of addresses to be added for the virtual LAN.</param>
        /// <returns>True if IPv6 is enabled, else false.</returns>
        public bool EnableIPv6Address(int routerVlanId, Collection <IPAddress> ipv6Addresses)
        {
            if (null == ipv6Addresses || ipv6Addresses.Count == 0)
            {
                Logger.LogInfo("IPv6 addresses can not be empty.");
                return(false);
            }

            Logger.LogInfo("Enabling IPv6 addresses in router.");

            // TODO: Remove hard coded prefix value.
            string ipData     = "ipv6 address {0}/64\n";
            string prefixData = "ipv6 nd ra prefix {0}/{1} {2} {3}\n";
            string command    = string.Empty;

            foreach (IPAddress ipv6Address in ipv6Addresses)
            {
                command += ipData.FormatWith(ipv6Address.ToString());
                //TODO: Get a logic to fetch the address prefix
                command += prefixData.FormatWith(GetPrefix(ipv6Address), 64, VALID_LIFE_TIME, PREFERRED_LIFE_TIME);
            }

            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);
                telnet.SendLine("wr mem");
                Thread.Sleep(TimeSpan.FromSeconds(10));
            }

            // Validation
            RouterVirtualLAN routerVlan          = GetVirtualLanDetails(routerVlanId);
            bool             isv6AddressPresesnt = false;

            foreach (IPAddress address in ipv6Addresses)
            {
                if (null == routerVlan.IPv6Details.Where(x => x.IPv6Address.Equals(address)).FirstOrDefault())
                {
                    isv6AddressPresesnt = false;
                    Logger.LogInfo("Failed to enable IPv6 addresses on VLAN: {0}".FormatWith(routerVlanId));
                    return(false);
                }
                else
                {
                    isv6AddressPresesnt = true;
                }
            }

            if (isv6AddressPresesnt)
            {
                Logger.LogInfo("Successfully enabled IPv6 addresses on VLAN: {0}".FormatWith(routerVlanId));
            }

            return(isv6AddressPresesnt);
        }
Пример #20
0
        /// <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);
        }
Пример #21
0
        /// <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)
        {
            /*  ????[?7l[?6l[?25lProCurve 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[?25h[?7l[?6l[?25h[?6l[?7l[?7l[?6l[?25hProCurve Switch 2824# [?25hconf t[?25hE[?25h[?25hProCurve Switch 2824(config)# [?25hshow inter[?25hface brief[?25h ethernet [?25h24[?25hE[?25h
             * 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[?25hProCurve Switch 2824(config)# [?25h*/

            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);
            }
        }