Ignore:
Timestamp:
04/15/08 17:49:15 (4 years ago)
Author:
bruno
Message:

Working dploy-add2dhcp with subnet

File:
1 edited

Legend:

Unmodified
Added
Removed
  • devel/dploy-dhcp/bin/dploy-add2dhcp

    r36 r41  
    33# $Id$ 
    44#  
    5 # (c) Bruno Cornec, HP, provided under the GPL v2 
     5# (c) Bruno Cornec, HP 
     6# (c) Gallig Renaud, HP 
    67# 
     8# Provided under the GPL v2 
    79 
    810use strict; 
    911use File::Copy; 
     12use Net::Netmask; 
     13use File::Basename; 
     14use Pod::Usage; 
     15use Getopt::Long qw(:config auto_abbrev no_ignore_case); 
     16use Data::Dumper; 
    1017 
    11 my $dhcp="PBCONF/dhcpd.conf"; 
    12 # 
    13 # /etc/dhcpd.conf should include that line: 
    14 # include "/etc/dhcpd.d/dploy.org.conf"; 
    15 # as part of the appropriate range 
    16 # does not support multiple ranges yet 
    17 # This is done at install time by the install script 
    18 # Also a dploy account should exist to run that script 
    19 # and be referenced in sudo. Also done by install script 
    20 # 
     18my $logfile = "/var/log/dploy.err"; 
     19my $dhcpmain = "/etc/dhcpd.conf"; 
     20my $dhcpsub = ""; 
     21my %opts; 
     22my $mac = ""; 
     23my $ip = ""; 
     24my $gw = ""; 
     25my $dns = ""; 
     26my $pxe = ""; 
     27my $ntp = ""; 
     28my $netmask = ""; 
     29my $iprange = ""; 
     30my $cidr = ""; 
    2131 
    22 my $mac=$ARGV[0] || ""; 
     32GetOptions("help|?|h" => \$opts{'h'}, 
     33            "mac|m=s" => \$opts{'mac'}, 
     34        "ip|i=s" => \$opts{'ip'}, 
     35        "netmask|n=s" => \$opts{'mask'}, 
     36        "gw|g=s" => \$opts{'gw'}, 
     37        "dns|d=s" => \$opts{'dns'}, 
     38        "pxe|p=s" => \$opts{'pxe'}, 
     39        "time|t=s" => \$opts{'ntp'}, 
     40    ) || pb_syntax(-1,0); 
    2341 
    24 if ($mac !~ /^(?:[[:xdigit:]]{1,2}[-:]){5}[[:xdigit:]]{1,2}$/) { 
     42# Mac address check 
     43if (defined $opts{'mac'}) { 
     44  $mac = $opts{'mac'}; 
     45  if ($mac !~ /^(?:[[:xdigit:]]{1,2}[-:]){5}[[:xdigit:]]{1,2}$/) { 
    2546    print "Syntax: dploy-add2dhcp MAC ip\n"; 
    2647    exit(-1); 
     48  } 
     49} 
     50# only lowercase for homogeneity in mac address 
     51$mac =~ tr/A-Z/a-z/; 
     52 
     53# newmac replaces : separator with - and only lowercase for dhcpd 
     54my $newmac = $mac; 
     55$newmac =~ s/:/-/g; 
     56 
     57# IP address check 
     58if (defined $opts{'ip'}) { 
     59  $ip = $opts{'ip'}; 
     60  if ($ip !~ /^([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])$/) { 
     61    print "Syntax: dploy-add2dhcp mac IP\n"; 
     62    exit(-1); 
     63  } 
    2764} 
    2865 
    29 # only lowercase for homogeneity 
    30 $mac =~ tr/A-Z/a-z/; 
    31  
    32 my $ip=$ARGV[1] || ""; 
    33  
    34 if ($ip !~ /^([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])$/) { 
    35     print "Syntax: dploy-add2dhcp mac IP\n"; 
    36     exit(-1); 
     66# Other Parameters 
     67if (defined $opts{'h'}) { 
     68  pb_syntax(0,1); 
    3769} 
    38 # 
    39 # Reads existing DHCP conf if any 
    40 # 
    41  
    42 my $foundit = 0; 
    43  
    44 open(DHCPNEW,"> $dhcp.new") || die "Unable to create $dhcp.new"; 
    45 if (open(DHCP,$dhcp)) { 
    46     while (<DHCP>) { 
    47         if (/host host-$mac \{/ .. /}\n/) { 
    48             # We need to replace the old content with the new one 
    49             print "Skip $_"; 
    50             $foundit = 1; 
    51         } else { 
    52             print "Keep $_"; 
    53             # Just write the existing content 
    54             print DHCPNEW $_; 
    55         } 
    56     } 
    57     close(DHCP); 
    58 } else { 
    59     $foundit = 1; 
     70if (defined $opts{'mask'}) { 
     71  $netmask = $opts{'mask'}; 
     72} 
     73if (defined $opts{'gw'}) { 
     74  $gw = $opts{'gw'}; 
     75} 
     76if (defined $opts{'pxe'}) { 
     77  $pxe = $opts{'pxe'}; 
    6078} 
    6179 
    62 if ($foundit == 1) { 
    63     # Writes new DHCP conf from scratch 
    64     # 
    65     print DHCPNEW "\n"; 
    66     print DHCPNEW "host host-$mac {\n"; 
    67     print DHCPNEW " hardware ethernet $mac;\n"; 
    68     print DHCPNEW " fixed-address $ip;\n"; 
    69     print DHCPNEW "}\n\n"; 
     80# Calculate IP subnet 
     81my  $block = new Net::Netmask ($ip, $netmask); 
     82$iprange = $block->base(); 
     83$cidr = $block->bits();  
     84 
     85# Check if a DHCP conf file already exist for the subnet 
     86$dhcpsub="/etc/dploy/dhcp-$iprange-$cidr.conf"; 
     87if (! -d "/etc/dploy" ) { 
     88  mkdir ("/etc/dploy",0755); 
    7089} 
    71 close(DHCPNEW); 
    72 move("$dhcp.new",$dhcp); 
    7390 
    74 # 
     91if (! -e "$dhcpsub" ) { 
     92  # Create a DHCP file for the IP subnet 
     93  open(DHCPSUBNEW,"> $dhcpsub.new") || die "Unable to create $dhcpsub.new: $!"; 
     94    print DHCPSUBNEW "subnet $iprange netmask $netmask {\n"; 
     95    print DHCPSUBNEW "  option routers              $gw;\n"; 
     96    if (defined $opts{'dns'}) { 
     97      $dns = $opts{'dns'}; 
     98      print DHCPSUBNEW "  option domain-name-servers  $dns;\n"; 
     99    } 
     100    if (defined $opts{'ntp'}) { 
     101      $ntp = $opts{'ntp'}; 
     102      print DHCPSUBNEW "  option ntp-servers          $ntp;\n"; 
     103    } 
     104    print DHCPSUBNEW "  next-server                 $pxe;\n"; 
     105    print DHCPSUBNEW "  authoritative;\n"; 
     106    print DHCPSUBNEW "  if substring(option vendor-class-identifier, 0, 20) = \"PXEClient:Arch:00000\" {\n"; 
     107    print DHCPSUBNEW "    filename \"pxelinux.bin\";\n"; 
     108    print DHCPSUBNEW "  } else if substring(option vendor-class-identifier, 0, 20) = \"PXEClient:Arch:00002\" {\n"; 
     109    print DHCPSUBNEW "    filename \"elilo-ia64.efi\";\n"; 
     110    print DHCPSUBNEW "  }\n"; 
     111    print DHCPSUBNEW "}\n"; 
     112  close DHCPSUBNEW; 
     113  move("$dhcpsub.new",$dhcpsub); 
     114} 
     115 
     116# Update the main DHCP configuration file 
     117my $foundit = 0; 
     118open(DHCPMAIN, "$dhcpmain") || die "Unable to open $dhcpmain: $!"; 
     119while (<DHCPMAIN>) { 
     120    if (/include\s+\"$dhcpsub\";/) { 
     121        $foundit=1; 
     122    } 
     123} 
     124close(DHCPMAIN); 
     125 
     126if ($foundit == 0) { 
     127    open(DHCPMAIN, ">>$dhcpmain") || die "Unable to open $dhcpmain: $!"; 
     128    print DHCPMAIN "include \"$dhcpsub\";\n"; 
     129    close(DHCPMAIN); 
     130} 
     131 
     132 
     133# Populate the subnet DHCP file with the new server entry 
     134# Replace server entry if already existing 
     135$foundit = 0; 
     136# let's try to find the right position in the file 
     137my $bracket = 0; 
     138my $bracketfound = 0; 
     139my $lastline = ""; 
     140 
     141open(DHCPSUBNEW,"> $dhcpsub.new") || die "Unable to create $dhcpsub.new: $!"; 
     142open(DHCPSUB, "$dhcpsub") || die "Why can't I open a file I just created: $!"; 
     143while (<DHCPSUB>) { 
     144    if (/{/) { 
     145        $bracket++; 
     146        $bracketfound=1; 
     147    } 
     148    if (/}/) { 
     149        $bracket--; 
     150    } 
     151    if (/host host-$newmac {/ .. /}\n/) { 
     152        # We need to replace the old content with the new one 
     153        print "Skip $_"; 
     154        $foundit = 1; 
     155    } else { 
     156        print "Keep $_"; 
     157        # Just write the existing content 
     158        print DHCPSUBNEW $_  unless (($_ =~ /}/) && ($bracket == 0) && ($bracketfound == 1)); 
     159    } 
     160    $lastline = $_; 
     161} 
     162 
     163print DHCPSUBNEW "  host host-$newmac {\n"; 
     164print DHCPSUBNEW "  hardware ethernet $mac;\n"; 
     165print DHCPSUBNEW "  fixed-address $ip;\n"; 
     166print DHCPSUBNEW "  }\n"; 
     167print DHCPSUBNEW $lastline; 
     168 
     169close DHCPSUBNEW; 
     170close DHCPSUB; 
     171move("$dhcpsub.new",$dhcpsub); 
     172 
    75173# Relaunch DHCP server 
    76 # 
    77174system("sudo /etc/init.d/dhcpd restart"); 
Note: See TracChangeset for help on using the changeset viewer.