Example #1
0
        static void CreateScope(IDhcpServer dhcpServer)
        {
            // gather name and range information
            var name = "Test Scope";

            var ipRange = DhcpServerIpRange.AsDhcpScope("192.168.128.0/24"); // use CIDR notation
            // "DhcpScope" automatically removes subnet and broadcast address: 192.168.128.1-192.168.128.254
            // or:
            // var ipRange = DhcpServerIpRange.AsDhcpScope("192.168.128.1", "192.168.128.254");
            // var ipRange = DhcpServerIpRange.AsDhcpScope("192.168.128.0", (DhcpServerIpMask)"255.255.255.0");

            // create scope (mask can be explicitly set if required)
            var dhcpScope = dhcpServer.Scopes.AddScope(name, ipRange);

            // specify excluded IP ranges
            dhcpScope.ExcludedIpRanges.AddExcludedIpRange(startAddress: "192.168.128.1", endAddress: "192.168.128.20");
            dhcpScope.ExcludedIpRanges.AddExcludedIpRange(startAddress: "192.168.128.240", endAddress: "192.168.128.254");

            // fetch the default gateway/router option
            var option3 = dhcpServer.Options.GetDefaultOption(DhcpServerOptionIds.Router);
            // prepare an option value
            var option3Value = option3.CreateOptionIpAddressValue("192.168.128.0");

            // add the option value to the scope
            dhcpScope.Options.AddOrSetOptionValue(option3Value);

            // fetch option 15 (DNS Domain Name) from the server and set a scope-wide value
            var option15      = dhcpServer.Options.GetDefaultOption(DhcpServerOptionIds.DomainName);
            var option15Value = option15.CreateOptionStringValue("mydomain.biz.local");

            dhcpScope.Options.AddOrSetOptionValue(option15Value);

            // fetch option 6 (DNS Name Server) from the server and set a scope-wide value
            var option6      = dhcpServer.Options.GetDefaultOption(DhcpServerOptionIds.DomainNameServer);
            var option6Value = option6.CreateOptionIpAddressValue("192.168.128.10", "192.168.128.11");

            dhcpScope.Options.AddOrSetOptionValue(option6Value);

            // fetch option 4 (Time Server) from the server and set a scope-wide value
            var option4      = dhcpServer.Options.GetDefaultOption(DhcpServerOptionIds.TimeServer);
            var option4Value = option4.CreateOptionIpAddressValue("192.168.128.10");

            dhcpScope.Options.AddOrSetOptionValue(option4Value);

            // activate the scope
            dhcpScope.Activate();

            // write out scope information
            DumpScope(dhcpScope);

            // modify the scope
            dhcpScope.Comment = "A test scope description";

            // remove one of the excluded IP ranges
            dhcpScope.ExcludedIpRanges.RemoveExcludedIpRange(DhcpServerIpRange.AsExcluded("192.168.128.240", "192.168.128.254"));

            // remove the Time Server value
            dhcpScope.Options.RemoveOptionValue(DhcpServerOptionIds.TimeServer);

            // update the router option
            option3Value = option3.CreateOptionIpAddressValue("192.168.128.1");
            dhcpScope.Options.SetOptionValue(option3Value);

            // add a client
            var client = dhcpScope.Clients.AddClient("192.168.128.5", "AABBCCDDEEFF", "MyWorkstation.mydomain.biz.local", "My Workstation Lease");

            Console.WriteLine($"Client: {client}");

            // convert client to reservation
            var clientReservation = client.ConvertToReservation();

            Console.WriteLine($"Client Reservation: {clientReservation}");

            // set different dns name server option for reservation
            var reservationOption6Value = option6.CreateOptionIpAddressValue("192.168.128.11", "192.168.128.12");

            clientReservation.Options.AddOrSetOptionValue(reservationOption6Value);

            // add a reservation
            var reservation = dhcpScope.Reservations.AddReservation("192.168.128.10", "AA:00:CC:dd:EE:FF");

            Console.WriteLine($"Place-holder Reservation: {reservation}");
            reservation.Client.Name    = "YourWorkstation.mydomain.biz.local";
            reservation.Client.Comment = "Your Workstation Lease";

            // set different dns domain name option for reservation
            var reservationOption15Value = option15.CreateOptionStringValue("youdomain.biz.local");

            reservation.Options.AddOrSetOptionValue(reservationOption15Value);

            // write out scope information
            DumpScope(dhcpScope);

            // remove dns domain name reservation option
            reservation.Options.RemoveOptionValue(DhcpServerOptionIds.DomainName);

            // deactivate scope
            dhcpScope.Deactivate();

            // write out scope information
            DumpScope(dhcpScope);

            // delete the scope
            dhcpScope.Delete();
        }
Example #2
0
        static void DumpDhcpInfo(IDhcpServer dhcpServer)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"{dhcpServer.Name} (v{dhcpServer.VersionMajor}.{dhcpServer.VersionMinor} - {dhcpServer.Address})");

            // Configuration
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(" Configuration:");
            Console.ForegroundColor = ConsoleColor.Gray;
            var config = dhcpServer.Configuration;

            Console.WriteLine($"      Api Protocol Support: {config.ApiProtocolSupport}");
            Console.WriteLine($"             Database Name: {config.DatabaseName}");
            Console.WriteLine($"             Database Path: {config.DatabasePath}");
            Console.WriteLine($"               Backup Path: {config.BackupPath}");
            Console.WriteLine($"           Backup Interval: {config.BackupInterval}");
            Console.WriteLine($"  Database Logging Enabled: {config.DatabaseLoggingEnabled}");
            Console.WriteLine($"          Cleanup Interval: {config.DatabaseCleanupInterval}");

            // Audit Logging
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(" Audit Log:");
            Console.ForegroundColor = ConsoleColor.Gray;
            var auditLog = dhcpServer.AuditLog;

            Console.WriteLine($"        Log Directory: {auditLog.AuditLogDirectory}");
            Console.WriteLine($"  Disk Check Interval: {auditLog.DiskCheckInterval}");
            Console.WriteLine($"   Max Log Files Size: {auditLog.MaxLogFilesSize}");
            Console.WriteLine($"    Min Space On Disk: {auditLog.MinSpaceOnDisk}");

            // DNS Settings
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(" DNS Settings:");
            Console.ForegroundColor = ConsoleColor.Gray;
            var dnsSettings = dhcpServer.DnsSettings;

            Console.WriteLine($"              Dynamic DNS Updates Enabled: {dnsSettings.DynamicDnsUpdatesEnabled}");
            Console.WriteLine($"  Dynamic DNS Updates Only When Requested: {dnsSettings.DynamicDnsUpdatedOnlyWhenRequested}");
            Console.WriteLine($"               Dynamic DNS Updates Always: {dnsSettings.DynamicDnsUpdatedAlways}");
            Console.WriteLine($"      Discard Records When Leases Deleted: {dnsSettings.DiscardRecordsWhenLeasesDeleted}");
            Console.WriteLine($"    Update Records for Down-Level Clients: {dnsSettings.UpdateRecordsForDownLevelClients}");
            Console.WriteLine($"       Disable Dynamic PTR Record Updates: {dnsSettings.DisableDynamicPtrRecordUpdates}");

            // Binding Elements
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(" Binding Elements:");
            Console.ForegroundColor = ConsoleColor.Gray;
            foreach (var be in dhcpServer.BindingElements.ToList())
            {
                Console.WriteLine($"  {be.InterfaceDescription} {be.InterfaceGuidId}");
                Console.WriteLine($"       Unmodifiable Endpoint: {be.CantModify}");
                Console.WriteLine($"                    Is Bound: {be.IsBound}");
                Console.WriteLine($"  Adapter Primary IP Address: {be.AdapterPrimaryIpAddress}");
                Console.WriteLine($"      Adapter Subnet Address: {be.AdapterSubnetAddress}");
            }

            // Failover Relationships
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(" Failover Relationships:");
            Console.ForegroundColor = ConsoleColor.Gray;
            foreach (var failoverRelationship in dhcpServer.FailoverRelationships.ToList())
            {
                Console.WriteLine($"   Name: {failoverRelationship.Name}");
                Console.WriteLine($"                            Mode: {failoverRelationship.Mode}");
                Console.WriteLine($"                           State: {failoverRelationship.State}");
                Console.WriteLine($"                     Server Type: {failoverRelationship.ServerType}");
                Console.WriteLine($"                  Primary Server: {failoverRelationship.PrimaryServerName} [{failoverRelationship.PrimaryServerAddress}]");
                Console.WriteLine($"                Secondary Server: {failoverRelationship.SecondaryServerName} [{failoverRelationship.SecondaryServerAddress}]");
                Console.WriteLine($"                   Shared Secret: {failoverRelationship.SharedSecret}");
                Console.WriteLine($"        Maximum Client Lead Time: {failoverRelationship.MaximumClientLeadTime}");
                Console.WriteLine($"       State Switchover Interval: {failoverRelationship.StateSwitchoverInterval}");
                Console.WriteLine($"                  Load Balance %: {failoverRelationship.LoadBalancePercentage}");
                Console.WriteLine($"    Standby Addresses Reserved %: {failoverRelationship.HotStandbyAddressesReservedPercentage}");
                Console.WriteLine($"               Associated Scopes:");
                foreach (var failoverScope in failoverRelationship.Scopes.ToList())
                {
                    Console.WriteLine($"                    {failoverScope}");
                }
            }

            // Classes
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(" Classes:");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine($"  Default Vendor Class Name: {dhcpServer.SpecificStrings.DefaultVendorClassName}");
            Console.WriteLine($"    Default User Class Name: {dhcpServer.SpecificStrings.DefaultUserClassName}");

            foreach (var c in dhcpServer.Classes.ToList())
            {
                Console.WriteLine($"  {c.Name}");
                Console.WriteLine($"      Comment: {c.Comment}");
                Console.WriteLine($"         Type: {(c.IsVendorClass ? "Vendor Class" : "User Class")}");
                Console.WriteLine($"         Data: {c.DataText}");

                // Enum Class Options
                Console.WriteLine("      Options:");
                foreach (var option in c.Options.ToList())
                {
                    Console.WriteLine($"         {option}");
                }
            }

            // Global Options
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(" Global Options:");
            Console.ForegroundColor = ConsoleColor.Gray;
            foreach (var option in dhcpServer.Options.ToList())
            {
                Console.WriteLine($"  {option}");
            }

            // Global Option Values
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("   Global Option Values:");
            Console.ForegroundColor = ConsoleColor.Gray;
            foreach (var value in dhcpServer.Options.GetOptionValues().ToList())
            {
                Console.WriteLine($"     {value}");
            }

            // Server Clients
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("      Server Clients:");
            Console.ForegroundColor = ConsoleColor.Gray;
            foreach (var client in dhcpServer.Clients.ToList())
            {
                Console.WriteLine($"          {client}");
            }

            // Scopes
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(" Scopes:");
            Console.ForegroundColor = ConsoleColor.Gray;
            foreach (var scope in dhcpServer.Scopes.ToList())
            {
                Console.WriteLine($"   {scope.Address}");
                Console.WriteLine($"            IP Range: {scope.IpRange}");
                Console.WriteLine($"                Mask: {scope.Mask}");
                Console.WriteLine($"               State: {scope.State}");
                Console.WriteLine($"                Name: {scope.Name}");
                Console.WriteLine($"             Comment: {scope.Comment}");
                Console.WriteLine($"        Primary Host: {scope.PrimaryHost}");
                Console.WriteLine($"      Lease Duration: {scope.LeaseDuration?.ToString() ?? "Unlimited"}");
                Console.WriteLine($"         Delay Offer: {scope.TimeDelayOffer.TotalMilliseconds} milliseconds");
                Console.WriteLine($"       Quarantine On: {scope.QuarantineOn}");
                Console.ForegroundColor = ConsoleColor.White;
                var failoverRelationship = scope.GetFailoverRelationship();
                Console.Write("    Failover Relationship:");
                Console.ForegroundColor = ConsoleColor.Gray;
                if (failoverRelationship == null)
                {
                    Console.WriteLine($" Not in a Failover Relationship");
                }
                else
                {
                    Console.WriteLine($" {failoverRelationship}");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("      Failover Statistics:");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    var failoverStatistics = scope.GetFailoverStatistics();
                    Console.WriteLine($"            Addresses Total: {failoverStatistics.AddressesTotal}");
                    Console.WriteLine($"             Addresses Free: {failoverStatistics.AddressesFree}");
                    Console.WriteLine($"           Addresses In Use: {failoverStatistics.AddressesInUse}");
                    Console.WriteLine($"     Partner Addresses Free: {failoverStatistics.PartnerAddressesFree}");
                    Console.WriteLine($"   Partner Addresses In Use: {failoverStatistics.PartnerAddressesInUse}");
                    Console.WriteLine($"       Local Addresses Free: {failoverStatistics.LocalAddressesFree}");
                    Console.WriteLine($"     Local Addresses In Use: {failoverStatistics.LocalAddressesInUse}");
                }
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("      Excluded IP Ranges:");
                Console.ForegroundColor = ConsoleColor.Gray;
                foreach (var ipRange in scope.ExcludedIpRanges)
                {
                    Console.WriteLine($"        {ipRange}");
                }
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("      Options:");
                Console.ForegroundColor = ConsoleColor.Gray;
                foreach (var value in scope.Options.ToList())
                {
                    Console.WriteLine($"        {value}");
                }
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("      Reservations:");
                Console.ForegroundColor = ConsoleColor.Gray;
                foreach (var reservation in scope.Reservations.ToList())
                {
                    Console.WriteLine($"        {reservation}");
                    Console.WriteLine($"        Client: {reservation.Client}");
                    Console.WriteLine("          Options:");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    foreach (var value in reservation.Options.ToList())
                    {
                        Console.WriteLine($"            {value}");
                    }
                }
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("      Clients:");
                Console.ForegroundColor = ConsoleColor.Gray;
                foreach (var client in scope.Clients.ToList())
                {
                    Console.WriteLine($"        {client}");
                }
            }
        }