File: lease_query.pl

package info (click to toggle)
libnet-dhcp-perl 0.693%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 368 kB
  • ctags: 111
  • sloc: perl: 1,974; sh: 51; makefile: 8
file content (35 lines) | stat: -rwxr-xr-x 1,202 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/perl
# Simple DHCP client - send a LeaseQuery (by IP) and receive the response

use IO::Socket::INET;
use Net::DHCP::Packet;
use Net::DHCP::Constants;

$usage = "usage: $0 DHCP_SERVER_IP DHCP_CLIENT_IP\n"; $ARGV[1] || die $usage;

# create a socket
$handle = IO::Socket::INET->new(Proto => 'udp',
                                Broadcast => 1,
                                PeerPort => '67',
                                LocalPort => '67',
                                PeerAddr => $ARGV[0])
              or die "socket: $@";     # yes, it uses $@ here

# create DHCP Packet
$inform = Net::DHCP::Packet->new(
                    op => BOOTREQUEST(),
                    Htype  => '0',
                    Hlen   => '0',
                    Ciaddr => $ARGV[1],
                    Giaddr => $handle->sockhost(),
                    Xid => int(rand(0xFFFFFFFF)),     # random xid
                    DHO_DHCP_MESSAGE_TYPE() => DHCPLEASEQUERY
                    );

# send request
$handle->send($inform->serialize()) or die "Error sending LeaseQuery: $!\n";

#receive response
$handle->recv($newmsg, 1024) or die;
$packet = Net::DHCP::Packet->new($newmsg);
print $packet->toString();