I've gotten the below code to test if my DHCP server is work or not. However, there is some problem to it. If the DHCP reply is broadcast, the script will never responds. It is only when a unicast reply, the scripts will have response and terminates.
#!/usr/bin/perl # Simple DHCP client - sending a broadcasted DHCP Discover request use IO::Socket::INET; use Net::DHCP::Packet; use Net::DHCP::Constants; use POSIX qw(setsid strftime); # sample logger sub logger{ my $str = shift; print STDOUT strftime "[%d/%b/%Y:%H:%M:%S] ", localtime; print STDOUT "$str\n"; } logger("DHCPd tester - dummy client"); logger("Opening socket"); $handle = IO::Socket::INET->new(Proto => 'udp', Broadcast => 1, PeerPort => '67', LocalPort => '68', PeerAddr => '255.255.255.255', ) || die "Socket creation error: $@\n"; # yes, it uses $@ here # create DHCP Packet DISCOVER $discover = Net::DHCP::Packet->new( Xid => 0x12345678, DHO_DHCP_MESSAGE_TYPE() => DHCPDISCOVER(), DHO_VENDOR_CLASS_IDENTIFIER() => 'foo', ); logger("Sending DISCOVER to 127.0.0.1:67"); logger($discover->toString()); $handle->send($discover->serialize()) or die "Error sending:$!\n"; logger("Waiting for response from server"); $handle->recv($buf, 4096) || die("recv:$!"); logger("Got response"); $response = new Net::DHCP::Packet($buf); logger($response->toString()); # create DHCP Packet REQUEST $request = Net::DHCP::Packet->new( Xid => 0x12345678, Ciaddr => $response->yiaddr(), DHO_DHCP_MESSAGE_TYPE() => DHCPREQUEST(), DHO_VENDOR_CLASS_IDENTIFIER() => 'foo', DHO_DHCP_REQUESTED_ADDRESS() => $response->yiadd +r(), ); logger("Sending REQUEST to 127.0.0.1:67"); logger($request->toString()); $handle->send($request->serialize()) or die "Error sending:$!\n"; logger("Waiting for response from server"); $handle->recv($buf, 4096) || die("recv:$!"); logger("Got response"); $response = new Net::DHCP::Packet($buf); logger($response->toString());

In reply to Socket Listen/Send DHCP by maclaren_sg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.