- #!/usr/bin/perl
- #server.pl
- #use strict;
- use Socket;
- use constant IPADDR => '192.168.1.254';
- use constant PORT => 4000;
- use constant MAX_RECV_LEN => 100;
- my $local_host = gethostbyname(IPADDR);
- my $local_port = PORT;
- my $proto = getprotobyname('udp');
- my $local_addr = sockaddr_in($local_port, $local_host);
- socket(DHCP_SOCK ,PF_INET,SOCK_DGRAM,$proto) or die "socket() failed: $!";
- bind(DHCP_SOCK ,$local_addr);
- my $all;
- my $myall=[];
- while( 1 )
- {
- my $my_all = recv( DHCP_SOCK, $all, MAX_RECV_LEN ,0 ) or warn "Problem with recv all";
- @myall=split(/;/,$all);
- my $ipaddr = @myall[0];
- my $macaddr = @myall[1];
- my $net = @myall[2];
- my $opt = @myall[3];
- my $username = @myall[4];
- if ( $my_all ){
- # my( $the_port, $the_ip ) = sockaddr_in( $my_all );
- #system("(echo %date:~0,10% %time:~0,5% ip:$ipaddr,mac:$macaddr,net:$net,opt:$opt,user:$username )");
- system("(echo %date:~0,10% %time:~0,5% ip:$ipaddr,mac:$macaddr,net:$net,opt:$opt,user:$username >> e:\\\\bat\\\\dhcp_debug.log )");
- if ( $opt eq 'addmac') {
- system("(echo %date:~0,10% %time:~0,5% add $ipaddr $macaddr $username >> e:\\\\bat\\\\dhcp_server.log )");
- system("(netsh dhcp server scope $net add reservedip $ipaddr $macaddr $username $username BOTH ||echo %date:~0,10% %time:~0,5% false add >> e:\\\\bat\\\\dhcp_server.log)");
- }
- elsif ( $opt eq 'rmvmac') {
- system("(echo %date:~0,10% %time:~0,5% delete $ipaddr $macaddr >> e:\\\\bat\\\\dhcp_server.log )");
- system("(netsh dhcp server scope $net delete reservedip $ipaddr $macaddr ||echo %date:~0,10% %time:~0,5% false delete >> e:\\\\bat\\\\dhcp_server.log )");
- }
- }
- else
- {
- warn "Problem with recv: @!\\n";
- }
- }
- #该片段来自于http://www.codesnippet.cn/detail/111120137027.html
来源: http://www.codesnippet.cn/detail/111120137027.html