DHCP Tutorial

Scripting

The DHCP server can be configured to assign a specific ip address to a DHCP client. After running the following script, a DHCP server will be simulated. The server will wait for a DHCP discover message from a client. Once received, the process of assigning the ip address will be started according to the DHCP protocol.

Simulate DHCP Server

from time import sleep
discover_received = False
request_received = False
server_ip = "10.104.3.100"
client_ip = "10.104.3.200"
mask = "255.0.0.0"
dhcp_type_discover = 1
dhcp_type_request = 3

def on_message_received(msg):
    global discover_received, request_received, dhcp_type_discover, dhcp_type_request
    if msg.get_option(53)[0] == dhcp_type_discover and discover_received == False:
       discover_received = True
       dhcp = message_builder.create_dhcp_message(receiver.name, receiver.name)      
       dhcp.ip_header.ip_address_destination = "255.255.255.255"
       dhcp.ip_header.ip_address_source = server_ip
       dhcp.xid = msg.xid
       
       dhcp.offer_ip(client_ip,server_ip, mask,msg.ethernet_header.mac_address_source,2)
              
    if msg.get_option(53)[0]  == dhcp_type_request:   
        dhcp = message_builder.create_dhcp_message(receiver.name, receiver.name)      
        dhcp.ip_header.ip_address_destination = server_ip
        dhcp.ip_header.ip_address_source = client_ip
        dhcp.xid = msg.xid
        dhcp.ack_ip(client_ip, server_ip , mask ,msg.ethernet_header.mac_address_source)
        tc_return_success("client ip was assigned correctly")
          

DHCP_msg_offer= message_builder.create_dhcp_message(receiver.name, receiver.name)
DHCP_msg_offer.on_message_received += on_message_received
DHCP_msg_offer.start_capture()    
tc_wait_for_return(50)