static void Main(string[] args) { // Directly Connect to DHCP Server var server = DhcpServer.Connect("localhost"); CreateScope(server); }
public HttpResponseMessage scopelist() { // Connect to DHCP Server var dhcpServer = DhcpServer.Connect(dhcp_host); ScopeList mylist = new ScopeList(); // Retrieving scopes list and add each one to response foreach (var scope in dhcpServer.Scopes) { if (scope.State == DhcpServerScopeState.Enabled || scope.State == DhcpServerScopeState.EnabledSwitched) { mylist.scopes.Add(new Scope(scope.Address.ToString())); } } // Returning Json string from mylist omiting null values return(new HttpResponseMessage() { Content = new StringContent( JsonConvert.SerializeObject(mylist, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }), Encoding.UTF8, "application/json" ) }); }
static void ConfigureFailover(DhcpServer dhcpServer, DhcpServerScope scope) { // Sample snippets only // creating a failover relationship // - matching scopes are created on the partner server // including all settings supported by this library // create with an existing relationship var existingRelationship = dhcpServer.FailoverRelationships.GetRelationship("DHCPServer1-DHCPServer2"); scope.ConfigureFailover(existingRelationship); // create with a new relationship (name and settings are default) var partnerServer = DhcpServer.Connect("MyPartnerDhcpServer"); var relationship = scope.ConfigureFailover(partnerServer, "A Shared Secret", DhcpServerFailoverMode.HotStandby); // create with a new relationship (define a name) var namedRelationship = scope.ConfigureFailover(partnerServer: partnerServer, name: "Relationship Name", sharedSecret: "A Shared Secret", mode: DhcpServerFailoverMode.HotStandby); // create with a new relationship (define settings) var customizedRelationship = scope.ConfigureFailover(partnerServer: partnerServer, name: "Relationship Name", sharedSecret: "A Shared Secret", mode: DhcpServerFailoverMode.HotStandby, modePercentage: 10, maximumClientLeadTime: TimeSpan.FromHours(2), stateSwitchInterval: TimeSpan.FromMinutes(10)); // create load-balance failover var loadBalancedRelationship = scope.ConfigureFailover(partnerServer, "A Shared Secret", DhcpServerFailoverMode.LoadBalance); // retrieve scope failover relationship var scopeRelationship = scope.GetFailoverRelationship(); // replicate failover // - all supported settings are pushed to the partner server scope.ReplicateFailoverPartner(); // replicate all scopes in a failover relationship relationship.ReplicateRelationship(); // deconfigure failover // - scopes are removed from the partner server // (whichever isn't being used to deconfigure the failover) scope.DeconfigureFailover(); // enumerate scopes associated with a relationship foreach (var relationshipScope in relationship.Scopes) { Console.WriteLine(relationshipScope); } // delete failover relationship // (all associated scopes must be deconfigured first) relationship.Delete(); }
static void Main(string[] args) { // Discover DHCP Servers try { foreach (var dhcpServer in DhcpServer.Servers.ToList()) { DumpDhcpInfo(dhcpServer); Console.WriteLine(); } } catch (DhcpServerException ex) when(ex.ApiError == "DDS_NO_DS_AVAILABLE") { Console.WriteLine("No DHCP Servers could be automatically discovered"); } // Directly Connect to DHCP Server var server = DhcpServer.Connect("localhost"); DumpDhcpInfo(server); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(); Console.WriteLine("<Press any key to continue>"); Console.ReadKey(true); }
static void Main(string[] args) { // Discover DHCP Servers foreach (var dhcpServer in DhcpServer.Servers.ToList()) { DumpDhcpInfo(dhcpServer); Console.WriteLine(); } // Directly Connect to DHCP Server var server = DhcpServer.Connect("192.168.1.1"); DumpDhcpInfo(server); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(); Console.WriteLine("<Press any key to continue>"); Console.ReadKey(true); }
public HttpResponseMessage exclude(String scope) { // Connect to DHCP Server var dhcpServer = DhcpServer.Connect(dhcp_host); Boolean bFound = false; ScopeExcludeRangeList mylist = new ScopeExcludeRangeList(); // Browsing through scopes foreach (var searchscope in dhcpServer.Scopes) { if (searchscope.Address.ToString().Equals(scope)) { // Found the scope given in parameter bFound = true; // Browsing through exclude ranges for this scope foreach (var myex in searchscope.ExcludedIpRanges) { // Adding start and end values from this exclude range mylist.excluderanges.Add(new ScopeRange(myex.StartAddress.ToString(), myex.EndAddress.ToString())); } } } if (bFound) { // Returning Json string from mylist omiting null values return(new HttpResponseMessage() { Content = new StringContent( JsonConvert.SerializeObject(mylist, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }), Encoding.UTF8, "application/json" ) }); } else { // Scope given in parameter was not found throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Scope not found")); } }
public HttpResponseMessage range(String scope) { // Connect to DHCP Server var dhcpServer = DhcpServer.Connect(dhcp_host); Boolean bFound = false; String mys = ""; String mye = ""; // Browsing through scopes foreach (var myscope in dhcpServer.Scopes) { if (myscope.Address.ToString().Equals(scope)) { // Found the scope given in parameter bFound = true; // Store start and end of this scope range mys = myscope.IpRange.StartAddress.ToString(); mye = myscope.IpRange.EndAddress.ToString(); } } if (bFound) { // Creating range from previous start and end ScopeRange myrange = new ScopeRange(mys, mye); // Returning Json string from myrange omiting null values return(new HttpResponseMessage() { Content = new StringContent( JsonConvert.SerializeObject(myrange, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }), Encoding.UTF8, "application/json" ) }); } else { // Scope given in parameter was not found throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Scope not found")); } }
public HttpResponseMessage lease(String scope) { // Connect to DHCP Server var dhcpServer = DhcpServer.Connect(dhcp_host); Boolean bFound = false; ScopeLeaseList mylist = new ScopeLeaseList(); // Browsing through scopes foreach (var searchscope in dhcpServer.Scopes) { if (searchscope.Address.ToString().Equals(scope)) { // Found the scope given in parameter bFound = true; // MS DHCP return AddressState as full byte (state, Name protection and DNS informations) // We just want the first 2 bits : state so logical and with 3 (0000011) byte allactive = 3; // Get active DHCP client leases var activeClients = searchscope.Clients .Where(c => (Convert.ToByte(c.AddressState) & allactive) == 1) .Where(c => c.Type == DhcpServerClientTypes.DHCP); // Browsing through leases foreach (var client in activeClients) { // Retrieving client name String myname = ""; if (client.Name == null) { // If no name, send something anyway myname = "EMPTYNAME"; } else { myname = client.Name; } // Retrieving client hardware address String myhw = client.HardwareAddress.ToString(); // Applying Adrezo formatting if (adrezo_format) { // Keeping only the host part in name int firstpoint = myname.IndexOf('.'); if (firstpoint > 0) { myname = myname.Substring(0, firstpoint); } // Limit client name to 20 chars if (myname.Length > 20) { myname = myname.Substring(0, 20); } // Removing : from mac address string myhw = myhw.Replace(":", ""); //Limit mac to 12 chars if (myhw.Length > 12) { myhw = myhw.Substring(0, 12); } } // Add this lease with all attributes ip/mac/expiration date/name mylist.leases.Add(new Lease(client.IpAddress.ToString(), myhw, client.LeaseExpires.ToString("yyyy-MM-dd HH:mm:ss"), myname)); } } } if (bFound) { // Returning Json string from mylist omiting null values return(new HttpResponseMessage() { Content = new StringContent( JsonConvert.SerializeObject(mylist, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }), Encoding.UTF8, "application/json" ) }); } else { // Scope given in parameter was not found throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Scope not found")); } }
public HttpResponseMessage reserve(String scope) { // Connect to DHCP Server var dhcpServer = DhcpServer.Connect(dhcp_host); Boolean bFound = false; ScopeLeaseList mylist = new ScopeLeaseList(); // Browsing through scopes foreach (var searchscope in dhcpServer.Scopes) { if (searchscope.Address.ToString().Equals(scope)) { // Found the scope given in parameter bFound = true; // Browsing through reservations for this scope foreach (var myresa in searchscope.Reservations) { // Retrieving client name String myname = ""; if (myresa.Client.Name == null) { // If no name, send something anyway myname = "EMPTYNAME"; } else { myname = myresa.Client.Name; } // Retrieving client hardware address String myhw = myresa.HardwareAddress.ToString(); // Applying Adrezo formatting if (adrezo_format) { // Keeping only the host part in name int firstpoint = myname.IndexOf('.'); if (firstpoint > 0) { myname = myname.Substring(0, firstpoint); } // Limit client name to 20 chars if (myname.Length > 20) { myname = myname.Substring(0, 20); } // Removing : from mac address string myhw = myhw.Replace(":", ""); // Limit mac to 12 chars if (myhw.Length > 12) { myhw = myhw.Substring(0, 12); } } // Reserve Mac Address doesn't have to be removed // Add this reservation with no expiration date (null), just ip/mac/name mylist.leases.Add(new Lease(myresa.IpAddress.ToString(), myhw, null, myname)); } } } if (bFound) { // Returning Json string from mylist omiting null values return(new HttpResponseMessage() { Content = new StringContent( JsonConvert.SerializeObject(mylist, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }), Encoding.UTF8, "application/json" ) }); } else { // Scope given in parameter was not found throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Scope not found")); } }