Changeset 51 for devel/dploy-dhcp
- Timestamp:
- 05/02/08 16:00:33 (4 years ago)
- File:
-
- 1 edited
-
devel/dploy-dhcp/bin/dploy-add2dhcp (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
devel/dploy-dhcp/bin/dploy-add2dhcp
r42 r51 15 15 use Getopt::Long qw(:config auto_abbrev no_ignore_case); 16 16 use Data::Dumper; 17 use ProjectBuilder::Base; 18 use Dploy::Base; 17 19 18 20 my $logfile = "/var/log/dploy.err"; 19 21 my $dhcpmain = "/etc/dhcpd.conf"; 20 my $dhcpsub = ""; 22 my $dploydir = "/etc/dploy"; 23 24 my $dhcpsub = undef; 21 25 my %opts; 22 my $mac = ""; 23 my $ip = ""; 24 my $gw = ""; 25 my $dns = ""; 26 my $pxe = ""; 27 my $ntp = ""; 28 my $netmask = ""; 29 my $iprange = ""; 30 my $cidr = ""; 26 my $mac = undef; 27 my $iprange = undef; 28 my $cidr = undef; 29 30 =pod 31 32 =head1 NAME 33 34 dploy-add2dhcp - Update DHCP configuration file for dploy.org 35 36 =head1 DESCRIPTION 37 38 dploy-add2dhcp helps you keeping your DHCP configuration up to date 39 40 =head1 SYNOPSIS 41 42 dploy-add2dhcp -m MAC-address -i IP-address -n IP-netmask [-g IP-Gateway -d IP-dns-server -p IP-PXE-server -t IP-NTP-server] 43 44 dploy-add2dhcp --mac MAC-address --ip IP-address --netmask IP-netmask [--gw IP-Gateway --dns IP-dns-server --pxe IP-PXE-server --time IP-NTP-server] 45 46 =head1 OPTIONS 47 48 =over 4 49 50 =item B<-h|--help> 51 52 Print a brief help message and exits. 53 54 =cut 55 56 pb_syntax_init("dploy-add2dhcp Version PBVER-PBREV\n"); 31 57 32 58 GetOptions("help|?|h" => \$opts{'h'}, 33 "mac|m=s" => \$opts{'mac'},59 "mac|m=s" => \$opts{'mac'}, 34 60 "ip|i=s" => \$opts{'ip'}, 35 61 "netmask|n=s" => \$opts{'mask'}, … … 38 64 "pxe|p=s" => \$opts{'pxe'}, 39 65 "time|t=s" => \$opts{'ntp'}, 40 ) || dploy_syntax(-1,0); 41 42 # Mac address check 43 if (defined $opts{'mac'}) { 44 $mac = $opts{'mac'}; 45 if ($mac !~ /^(?:[[:xdigit:]]{1,2}[-:]){5}[[:xdigit:]]{1,2}$/) { 46 print "Syntax: dploy-add2dhcp MAC ip\n"; 47 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 54 my $newmac = $mac; 55 $newmac =~ s/:/-/g; 56 57 # IP address check 58 if (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 } 64 } 66 ) || pb_syntax(-1,0); 65 67 66 68 # Other Parameters 67 69 if (defined $opts{'h'}) { 68 dploy_syntax(0,1); 69 } 70 if (defined $opts{'mask'}) { 71 $netmask = $opts{'mask'}; 72 } 73 if (defined $opts{'gw'}) { 74 $gw = $opts{'gw'}; 75 } 76 if (defined $opts{'pxe'}) { 77 $pxe = $opts{'pxe'}; 78 } 70 pb_syntax(0,1); 71 } 72 73 # Mandatory params checks 74 $mac = dploy_check_mac($opts{'mac'}); 75 dploy_check_ip($opts{'ip'},"Wrong IP address"); 76 dploy_check_ip($opts{'mask'},"Wrong Netmask"); 79 77 80 78 # Calculate IP subnet 81 my $block = new Net::Netmask ($ip, $netmask);79 my $block = new Net::Netmask($opts{'ip'}, $opts{'mask'}); 82 80 $iprange = $block->base(); 83 81 $cidr = $block->bits(); 84 82 85 83 # Check if a DHCP conf file already exist for the subnet 86 $dhcpsub="/etc/dploy/dhcp-$iprange-$cidr.conf"; 87 if (! -d "/etc/dploy" ) { 88 mkdir ("/etc/dploy",0755); 89 } 84 pb_mkdir_p("$dploydir"); 85 $dhcpsub="$dploydir/dhcp-$iprange-$cidr.conf"; 90 86 91 87 if (! -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); 88 # there are then more mandatory parameters 89 dploy_check_ip($opts{'gw'},"Wrong IP address for GW server"); 90 my $gwobj = new Net::Netmask($opts{'gw'},$opts{'mask'}); 91 if (($iprange != $gwobj->base()) || ($cidr != $gwobj->bits())) { 92 print "Gateway is not in the correct subnet \n"; 93 pb_syntax(-1,0); 94 } 95 # Create a DHCP file for the IP subnet 96 open(DHCPSUBNEW,"> $dhcpsub.new") || die "Unable to create $dhcpsub.new: $!"; 97 print DHCPSUBNEW "subnet $iprange netmask $opts{'mask'} {\n"; 98 print DHCPSUBNEW " option routers $opts{'gw'};\n"; 99 if (defined $opts{'dns'}) { 100 dploy_check_ip($opts{'dns'},"Wrong IP address for DNS server"); 101 print DHCPSUBNEW " option domain-name-servers $opts{'dns'};\n"; 102 } 103 if (defined $opts{'ntp'}) { 104 dploy_check_ip($opts{'ntp'},"Wrong IP address for NTP server"); 105 print DHCPSUBNEW " option ntp-servers $opts{'ntp'};\n"; 106 } 107 if (defined $opts{'pxe'}) { 108 dploy_check_ip($opts{'pxe'},"Wrong IP address for PXE server"); 109 print DHCPSUBNEW " next-server $opts{'pxe'};\n"; 110 print DHCPSUBNEW " authoritative;\n"; 111 print DHCPSUBNEW " if substring(option vendor-class-identifier, 0, 20) = \"PXEClient:Arch:00000\" {\n"; 112 print DHCPSUBNEW " filename \"pxelinux.bin\";\n"; 113 print DHCPSUBNEW " } else if substring(option vendor-class-identifier, 0, 20) = \"PXEClient:Arch:00002\" {\n"; 114 print DHCPSUBNEW " filename \"elilo-ia64.efi\";\n"; 115 print DHCPSUBNEW " }\n"; 116 } 117 print DHCPSUBNEW "}\n"; 118 close DHCPSUBNEW; 119 move("$dhcpsub.new",$dhcpsub); 114 120 } 115 121 … … 149 155 $bracket--; 150 156 } 151 if (/host host-$ newmac {/ .. /}\n/) {157 if (/host host-$mac {/ .. /}\n/) { 152 158 # We need to replace the old content with the new one 153 159 print "Skip $_"; … … 161 167 } 162 168 163 print DHCPSUBNEW " host host-$ newmac {\n";164 print DHCPSUBNEW " hardware ethernet $ mac;\n";165 print DHCPSUBNEW " fixed-address $ ip;\n";169 print DHCPSUBNEW " host host-$mac {\n"; 170 print DHCPSUBNEW " hardware ethernet $opts{'mac'};\n"; 171 print DHCPSUBNEW " fixed-address $opts{'ip'};\n"; 166 172 print DHCPSUBNEW " }\n"; 167 173 print DHCPSUBNEW $lastline; … … 172 178 173 179 # Relaunch DHCP server 174 system("sudo /etc/init.d/dhcpd restart"); 180 pb_system("sudo /etc/init.d/dhcpd restart","Restarting DHCP server"); 181 182 =back 183 184 =head1 WEB SITES 185 186 The main Web site of the project is available at L<http://www.dploy.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.dploy.org/>. 187 188 =head1 USER MAILING LIST 189 190 None exists for the moment. 191 192 =head1 AUTHORS 193 194 The dploy.org team L<http://trac.dploy.org/>. 195 196 =head1 COPYRIGHT 197 198 dploy.org is distributed under the GPL v2.0 license 199 described in the file C<COPYING> included with the distribution. 200 201 =cut 202
Note: See TracChangeset
for help on using the changeset viewer.
