Exemplo n.º 1
0
        /// <summary>
        /// Construct the VLan structure for the data
        /// </summary>
        /// <param name="line">VLan data</param>
        /// <param name="vlanIdentifierData">Data from "show vlan" telnet command which contains VLAn numbers information.</param>
        /// <param name="vlanDetails"><see cref="RouterVirtualLAN"/></param>
        private static bool GetVLan(string line, string vlanIdentifierData, out RouterVirtualLAN vlanDetails)
        {
            Regex ipAddressRegex = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");

            bool isIPAddress = false;

            vlanDetails = new RouterVirtualLAN();
            string[] localData;

            if (line.Contains('|'))
            {
                vlanDetails.Name = line.Split('|')[0].Trim();
                localData        = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                for (int j = 0; j < localData.Length; j++)
                {
                    //IP Config Method
                    if ((localData[j].StartsWith("DHCP", StringComparison.CurrentCulture)) || (localData[j].StartsWith("Manual", StringComparison.CurrentCulture)))
                    {
                        vlanDetails.ConfigMethod = localData[j];
                    }

                    //IP Address
                    if (!isIPAddress && ipAddressRegex.IsMatch(localData[j]))
                    {
                        vlanDetails.IPv4Address = IPAddress.Parse(localData[j]);
                        isIPAddress             = true;
                        continue;
                    }

                    //Subnet mask
                    if (isIPAddress && ipAddressRegex.IsMatch(localData[j]))
                    {
                        vlanDetails.SubnetMask = IPAddress.Parse(localData[j]);
                    }
                }

                // Get the Virtual LAN identifier
                vlanDetails.Identifier = GetVlanIdentifier(vlanIdentifierData, vlanDetails.Name);

                return(true);
            }

            return(false);
        }
Exemplo n.º 2
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)));
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get the additional details of a VLan
        /// </summary>
        /// <param name="vlanData">The data obtained with the command "sh run vlan vlan number".</param>
        /// <param name="routerVlan"></param>
        /// <returns><see cref="RouterVirtualLAN"/></returns>
        private static void GetVlanDetails(string vlanData, ref RouterVirtualLAN routerVlan)
        {
            vlanData = vlanData.Replace(controlCharacters, string.Empty).Replace("/r", string.Empty).Trim();

            string[] vlanDetails = vlanData.Split(new char[] { '\n' });

            // Get all the IPv4 details
            string ipv4Details = Array.Find(vlanDetails, x => x.Contains("ip address", StringComparison.CurrentCultureIgnoreCase));

            if (null != ipv4Details)
            {
                routerVlan.IPv4Address = GetIPAddress(ipv4Details);
                routerVlan.SubnetMask  = GetIPAddress(ipv4Details, true);
            }

            string ipv6StatusDetails = Array.Find(vlanDetails, x => x.Contains("ipv6 enable", StringComparison.CurrentCultureIgnoreCase));

            if (null != ipv6StatusDetails)
            {
                routerVlan.IPv6Status = true;
            }

            // Get all the IPv6 addresses
            string[] ipv6Addresses = Array.FindAll(vlanDetails, x => x.Contains("ipv6 address", StringComparison.CurrentCultureIgnoreCase) && !x.Contains("/") && !x.Contains("link-local", StringComparison.CurrentCultureIgnoreCase));

            Collection <IPAddress> IPv6AddressList = new Collection <IPAddress>();

            if (null != ipv6Addresses && ipv6Addresses.Count() > 0)
            {
                foreach (string ipv6Address in ipv6Addresses)
                {
                    IPv6AddressList.Add(GetIPAddress(ipv6Address));
                }
            }

            routerVlan.IPv6Details = GetIPv6Details(vlanDetails, IPv6AddressList);

            // Get the ipv4 helper address
            string ipv4helperAddressData = Array.Find(vlanDetails, x => x.Contains("ip helper-address", StringComparison.CurrentCultureIgnoreCase));

            if (null != ipv4helperAddressData)
            {
                routerVlan.IPv4HelperAddress = GetIPAddress(ipv4helperAddressData);
            }

            // Get IPv6 helper address
            string ipv6HelperAddress = Array.Find(vlanDetails, x => x.Contains("ipv6 helper-address", StringComparison.CurrentCultureIgnoreCase));

            if (null != ipv6HelperAddress)
            {
                routerVlan.IPv6HelperAddress = GetIPAddress(ipv6HelperAddress);
            }

            // Get Link Local address
            string LinkLocalAddress = Array.Find(vlanDetails, x => x.Contains("link-local", StringComparison.CurrentCultureIgnoreCase));

            if (null != LinkLocalAddress)
            {
                routerVlan.LinkLocalAddress = GetIPAddress(LinkLocalAddress);
            }
        }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
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);
        }