[221] | 1 | #!/usr/bin/perl |
---|
| 2 | # |
---|
[303] | 3 | # Copyright (C) 2006-2018, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
[221] | 4 | # License GNU GPL version 2 or later and Perl equivalent |
---|
[224] | 5 | # |
---|
[350] | 6 | # apt-get install perl-base perl-modules libyaml-syck-perl libnet-netmask-perl libreadonly-perl libfile-touch-perl libtext-table-perl libnetaddr-ip-perl |
---|
[221] | 7 | |
---|
| 8 | package DDT::RE; |
---|
| 9 | |
---|
| 10 | use strict; |
---|
| 11 | #use warnings; |
---|
| 12 | |
---|
| 13 | use Readonly; |
---|
| 14 | |
---|
| 15 | Readonly our $MAC_ADDRESS => qr{ (?: [0-9A-F]{2} :){5} [0-9A-F]{2} }xms; |
---|
| 16 | Readonly our $IPv4_ADDRESS => qr{ [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} }xms; |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | package main; |
---|
| 20 | |
---|
| 21 | use strict; |
---|
| 22 | #use warnings; |
---|
[362] | 23 | use version; our $VERSION = version->declare('0.11.9'); |
---|
[221] | 24 | |
---|
| 25 | use Getopt::Long qw(GetOptions); |
---|
| 26 | #use YAML; |
---|
| 27 | use YAML::Syck; |
---|
| 28 | use Net::Netmask; |
---|
| 29 | use File::Touch; |
---|
| 30 | use File::Copy; |
---|
| 31 | use Socket; |
---|
[296] | 32 | use Text::Table; |
---|
[350] | 33 | use NetAddr::IP; |
---|
[221] | 34 | |
---|
| 35 | my $command = shift @ARGV || 'help'; |
---|
| 36 | |
---|
| 37 | my %cmd_db = ( |
---|
| 38 | 'add-alias' => \&cmd_add_alias, |
---|
| 39 | 'add-dhcp' => \&cmd_add_dhcp, |
---|
| 40 | 'add-float' => \&cmd_add_float, |
---|
| 41 | 'add-static' => \&cmd_add_static, |
---|
| 42 | 'add-virtual' => \&cmd_add_virtual, |
---|
| 43 | 'change-comment' => \&cmd_change_comment, |
---|
[345] | 44 | 'change-sector' => \&cmd_change_sector, |
---|
[221] | 45 | 'change-host' => \&cmd_change_host, |
---|
| 46 | 'change-ip' => \&cmd_change_ip, |
---|
| 47 | 'change-mac' => \&cmd_change_mac, |
---|
| 48 | 'change-tag' => \&cmd_change_tag, |
---|
| 49 | 'check-dns' => \&cmd_check_dns, |
---|
[345] | 50 | 'create-sector' => \&cmd_create_sector, |
---|
[221] | 51 | 'create-pool' => \&cmd_create_pool, |
---|
| 52 | 'create-pxe' => \&cmd_create_pxe, |
---|
| 53 | 'create-tag' => \&cmd_create_tag, |
---|
| 54 | 'del-pc' => \&cmd_del_pc, |
---|
| 55 | 'del-float' => \&cmd_del_float, |
---|
| 56 | 'disable-pc' => \&cmd_disable_pc, |
---|
| 57 | 'disable-float' => \&cmd_disable_float, |
---|
| 58 | 'disable-pxe' => \&cmd_disable_pxe, |
---|
| 59 | 'enable-pc' => \&cmd_enable_pc, |
---|
| 60 | 'enable-float' => \&cmd_enable_float, |
---|
| 61 | 'enable-pxe' => \&cmd_enable_pxe, |
---|
| 62 | 'gen-dhcp-file' => \&cmd_generate_dhcp_file, |
---|
| 63 | 'gen-dns-file' => \&cmd_generate_dns_file, |
---|
| 64 | 'help' => \&cmd_help, |
---|
| 65 | 'load-database' => \&cmd_load_database, |
---|
| 66 | 'remove-pxe' => \&cmd_remove_pxe, |
---|
| 67 | 'remove-tag' => \&cmd_remove_tag, |
---|
| 68 | 'search-mac' => \&cmd_search_mac, |
---|
[352] | 69 | 'sector-add-ip' => \&cmd_sector_add_ip, |
---|
[221] | 70 | 'show' => \&cmd_show_host, |
---|
[345] | 71 | 'show-sector' => \&cmd_show_sector, |
---|
[221] | 72 | 'show-pool' => \&cmd_show_pool, |
---|
| 73 | 'show-pxe' => \&cmd_show_pxe, |
---|
| 74 | 'show-tag' => \&cmd_show_tag, |
---|
| 75 | 'upgrade-db' => \&cmd_upgrade_db, |
---|
| 76 | 'version' => \&cmd_version, |
---|
| 77 | ); |
---|
| 78 | |
---|
| 79 | #------------------------------------------------------------------------------- |
---|
| 80 | |
---|
| 81 | my $CONFIG; |
---|
| 82 | |
---|
| 83 | my $xdg_config_home = $ENV{'XDG_CONFIG_HOME'} || "$ENV{'HOME'}/.config"; |
---|
[236] | 84 | $CONFIG = config_load("$xdg_config_home/ddt/config.yml") if -e "$xdg_config_home/ddt/config.yml"; |
---|
[221] | 85 | |
---|
| 86 | my $COMPUTER_BASENAME = $CONFIG->{'database'}{'basename'} || 'ddt'; |
---|
| 87 | my $COMPUTER_EXT = $CONFIG->{'database'}{'ext'} || 'db'; |
---|
| 88 | |
---|
| 89 | my $FOLDER_APP = $CONFIG->{'database'}{'folder'} || '/var/lib/ddt'; |
---|
| 90 | my $FOLDER_BACKUP = $CONFIG->{'database'}{'backup'} || "$FOLDER_APP/backup"; |
---|
| 91 | my $FOLDER_GEN_DHCP = $CONFIG->{'generate'}{'dhcp'} || "$FOLDER_APP/dhcp"; |
---|
| 92 | my $FOLDER_GEN_DNS = $CONFIG->{'generate'}{'dns'} || "$FOLDER_APP/dns"; |
---|
| 93 | my $SCRIPT_UPDATE = $CONFIG->{'script'}{'update'} || '/usr/share/ddt/update-dhcp-server'; |
---|
| 94 | |
---|
| 95 | my $COMPUTER_YAML = "$FOLDER_APP/$COMPUTER_BASENAME.$COMPUTER_EXT"; |
---|
| 96 | |
---|
| 97 | #------------------------------------------------------------------------------- |
---|
| 98 | |
---|
| 99 | mkdir $FOLDER_APP, 0755 if not -d $FOLDER_APP; |
---|
| 100 | mkdir $FOLDER_BACKUP, 0755 if not -d $FOLDER_BACKUP; |
---|
| 101 | mkdir $FOLDER_GEN_DHCP, 0755 if not -d $FOLDER_GEN_DHCP; |
---|
| 102 | mkdir $FOLDER_GEN_DNS, 0755 if not -d $FOLDER_GEN_DNS; |
---|
| 103 | |
---|
| 104 | if (defined $cmd_db{$command}) { |
---|
| 105 | $cmd_db{$command}->(@ARGV); |
---|
| 106 | } |
---|
| 107 | else { |
---|
| 108 | print {*STDERR} "ddt: command $command not found\n\n"; |
---|
| 109 | $cmd_db{'help'}->(); |
---|
| 110 | exit 1; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | exit; |
---|
| 114 | |
---|
| 115 | #-------------------------------------------------------------------------------- |
---|
[236] | 116 | # LOAD SAVE section |
---|
| 117 | #-------------------------------------------------------------------------------- |
---|
| 118 | |
---|
| 119 | sub config_load { |
---|
| 120 | my $config_file = shift; |
---|
| 121 | |
---|
| 122 | my $configdb = YAML::Syck::LoadFile($config_file); |
---|
| 123 | |
---|
| 124 | return $configdb; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | #--------------------------------------------------------------- |
---|
| 128 | # Load computer database |
---|
| 129 | |
---|
| 130 | sub ipamdb_load { |
---|
[238] | 131 | my $database_yaml = shift; |
---|
[236] | 132 | |
---|
[238] | 133 | touch $database_yaml if not -e $database_yaml; |
---|
| 134 | my $computer_db = YAML::Syck::LoadFile($database_yaml); |
---|
[236] | 135 | |
---|
[237] | 136 | # add database version if not exist |
---|
| 137 | if (not exists $computer_db->{'version'}) { |
---|
| 138 | $computer_db->{'version'} = 1; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | return $computer_db; |
---|
[236] | 142 | } |
---|
| 143 | |
---|
| 144 | #--------------------------------------------------------------- |
---|
| 145 | # Save computer database |
---|
| 146 | |
---|
| 147 | sub ipamdb_save { |
---|
[238] | 148 | my ($database_yaml, $computer_db) = @_; |
---|
[236] | 149 | |
---|
[238] | 150 | my $dirdb = $database_yaml; |
---|
[236] | 151 | $dirdb =~ s{ / [^/]* $}{}xms; |
---|
| 152 | mkdir "$dirdb", 0755 unless -d "$dirdb"; |
---|
[238] | 153 | YAML::Syck::DumpFile($database_yaml, $computer_db); |
---|
[236] | 154 | |
---|
[237] | 155 | return $computer_db; |
---|
[236] | 156 | } |
---|
| 157 | |
---|
| 158 | #-------------------------------------------------------------------------------- |
---|
[221] | 159 | # CONTROL section |
---|
| 160 | #-------------------------------------------------------------------------------- |
---|
| 161 | |
---|
| 162 | sub control_exist_pool { |
---|
[238] | 163 | my ($computer_db, $pool) = @_; |
---|
[221] | 164 | |
---|
[238] | 165 | return exists $computer_db->{'pool'}{$pool} ? 1 : 0; |
---|
[221] | 166 | } |
---|
| 167 | |
---|
| 168 | #------------------------------------------------------------------------------- |
---|
[345] | 169 | #Nom: control_exist_sector |
---|
| 170 | #Description: controle l'existence d'un sector dans le fichier YAML |
---|
[221] | 171 | # return 0 (faux) ou 1 (vrai) |
---|
| 172 | |
---|
[345] | 173 | sub control_exist_sector { |
---|
| 174 | my ($computer_db, $sector) = @_; |
---|
[221] | 175 | |
---|
[345] | 176 | return 1 if exists $computer_db->{$sector}; |
---|
[221] | 177 | |
---|
[345] | 178 | print {*STDERR} "Error: sector $sector not found\n"; |
---|
[221] | 179 | return 0; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | #------------------------------------------------------------------------------- |
---|
| 183 | #Nom: control_exist_hostname |
---|
| 184 | #Description: controle l'existence d'un nom de machine dans le fichier YAML |
---|
| 185 | # return 0 (si trouvé) ou 1 (si non trouvé) |
---|
| 186 | |
---|
| 187 | sub control_exist_hostname { |
---|
[345] | 188 | my ($computer_db, $sector, $hostname) = @_; |
---|
[221] | 189 | |
---|
[345] | 190 | if ($computer_db->{$sector} eq '') { |
---|
[221] | 191 | return 1; |
---|
| 192 | } |
---|
| 193 | |
---|
[345] | 194 | my @sectordb = @{$computer_db->{$sector}}; |
---|
[221] | 195 | |
---|
[345] | 196 | for my $computer (@sectordb) { |
---|
[307] | 197 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 198 | return 0 if $attribute->{'hostname'} eq $hostname; |
---|
[221] | 199 | } |
---|
| 200 | return 1; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | #------------------------------------------------------------------------------- |
---|
| 204 | #Nom: control_exist_mac |
---|
| 205 | #Description: controle l'existence d'une adresse MAC dans le fichier YAML |
---|
| 206 | # return 0 (si trouvé) ou 1 (si non trouvé) |
---|
| 207 | |
---|
| 208 | sub control_exist_mac { |
---|
[238] | 209 | my ($computer_db, $mac) = @_; |
---|
[221] | 210 | |
---|
[345] | 211 | for my $sector_current (keys %{$computer_db}) { |
---|
| 212 | next if $sector_current eq 'dset'; |
---|
| 213 | next if $sector_current eq 'pool'; |
---|
| 214 | next if $sector_current eq 'pxe'; |
---|
| 215 | next if $sector_current eq 'tag'; |
---|
| 216 | next if $sector_current eq 'version'; |
---|
[221] | 217 | |
---|
[345] | 218 | my @sectordb = @{$computer_db->{$sector_current}}; |
---|
[307] | 219 | |
---|
| 220 | LOOP_ON_COMPUTER: |
---|
[345] | 221 | for my $computer (@sectordb) { |
---|
[307] | 222 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 223 | return 0 if $mac_address eq $mac; |
---|
[221] | 224 | } |
---|
| 225 | } |
---|
| 226 | return 1; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | #------------------------------------------------------------------------------- |
---|
| 230 | #Nom: control_exist_ip |
---|
| 231 | #Description: controle l'existence d'une adresse IP dans le fichier YAML |
---|
| 232 | # return 0 (si trouvé) ou 1 (si non trouvé) |
---|
| 233 | |
---|
| 234 | sub control_exist_ip { |
---|
[238] | 235 | my ($computer_db, $ip) = @_; |
---|
[221] | 236 | |
---|
[345] | 237 | for my $sector_current (keys %{$computer_db}) { |
---|
| 238 | next if $sector_current eq 'dset'; |
---|
| 239 | next if $sector_current eq 'pool'; |
---|
| 240 | next if $sector_current eq 'pxe'; |
---|
| 241 | next if $sector_current eq 'tag'; |
---|
| 242 | next if $sector_current eq 'version'; |
---|
[221] | 243 | |
---|
[306] | 244 | LOOP_ON_COMPUTER: |
---|
[345] | 245 | for my $computer (@{$computer_db->{$sector_current}}) { |
---|
[306] | 246 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 247 | #print "Erreur: cette adresse IP $ip existe déjà\n"; |
---|
| 248 | return 0 if $attribute->{'ip'} eq $ip; |
---|
[221] | 249 | } |
---|
| 250 | } |
---|
| 251 | |
---|
[238] | 252 | for my $current_pool (keys %{$computer_db->{'pool'}}) { |
---|
[306] | 253 | #--- Cette partie pour tester les ip des pools est bonne ne plus la changer ---# |
---|
| 254 | my @T_pool_ip = @{$computer_db->{'pool'}{$current_pool}{'ip'}}; |
---|
[221] | 255 | |
---|
| 256 | for my $pool_ip (@T_pool_ip) { |
---|
| 257 | #print "Erreur: cette adresse IP $ip existe déjà\n"; |
---|
| 258 | return 0 if $pool_ip eq $ip; |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | return 1; |
---|
| 263 | } |
---|
| 264 | |
---|
[350] | 265 | #------------------------------------------------------------------------------- |
---|
| 266 | |
---|
| 267 | sub control_ip_in_range { |
---|
| 268 | my ($computer_db, $sector, $ip) = @_; |
---|
| 269 | |
---|
[351] | 270 | return 1 if not exists $computer_db->{'dset'}{$sector}{'ip_range'}; # No IP range defined for this sector |
---|
[350] | 271 | |
---|
| 272 | my $ip_addr = NetAddr::IP->new($ip); |
---|
| 273 | |
---|
| 274 | LOOP_ON_IP_RANGE: |
---|
[351] | 275 | for my $ip_range_current (@{$computer_db->{'dset'}{$sector}{'ip_range'}}) { |
---|
[350] | 276 | my $range = NetAddr::IP->new($ip_range_current); |
---|
| 277 | return 1 if $range->contains($ip_addr); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | return 0; |
---|
| 281 | } |
---|
| 282 | |
---|
[221] | 283 | #------------------------------------------------------------------------------------- |
---|
| 284 | #Nom: control_syntaxe_mac |
---|
| 285 | #Description: controle la syntaxe d'une adresse MAC (juste la longueur pas les valeurs) |
---|
| 286 | # return 0 (si trouvé) ou 1 (si non trouvé) |
---|
| 287 | |
---|
| 288 | sub control_syntax_mac_address { |
---|
| 289 | my $mac = shift; |
---|
| 290 | |
---|
| 291 | if (scalar(split /:/, $mac) == 6 and $mac =~ $DDT::RE::MAC_ADDRESS) { |
---|
| 292 | return 1; |
---|
| 293 | } |
---|
| 294 | |
---|
[332] | 295 | print {*STDERR} "Error: bad MAC syntax: $mac\n"; |
---|
[221] | 296 | return 0; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | #------------------------------------------------------------------------------------- |
---|
| 300 | #Nom: control_syntax_ip |
---|
| 301 | #Description: controle la syntaxe d'une adresse IP (juste la longueur pas les valeurs) |
---|
| 302 | # return 0 (si trouvé) ou 1 (si non trouvé) |
---|
| 303 | |
---|
| 304 | sub control_syntax_ip { |
---|
| 305 | my $ip = shift; |
---|
| 306 | |
---|
[357] | 307 | return 1 if $ip ne 'pool'; |
---|
[221] | 308 | |
---|
[357] | 309 | return 0 if $ip !~ m{^(\d+\.){3}\d+$}; |
---|
| 310 | return 0 if not NetAddr::IP->new("$ip/32"); |
---|
[221] | 311 | return 1; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | #------------------------------------------------------------------------------------- |
---|
| 315 | |
---|
[353] | 316 | sub control_syntax_cidr { |
---|
| 317 | my $cidr = shift; |
---|
| 318 | |
---|
[354] | 319 | return 0 if $cidr !~ m{^(\d+\.){3}\d+/\d+$}; |
---|
| 320 | return 0 if not NetAddr::IP->new($cidr); |
---|
| 321 | return 1; |
---|
[353] | 322 | } |
---|
| 323 | |
---|
| 324 | #------------------------------------------------------------------------------------- |
---|
| 325 | |
---|
[221] | 326 | sub control_syntax_comment { |
---|
| 327 | my $comment = shift; |
---|
| 328 | |
---|
| 329 | if ($comment !~ m{^20\d\d-\d\d-\d\d\s}) { |
---|
[332] | 330 | print {*STDERR} "Error: no date like 2014-01-10 at the beginning: $comment\n"; |
---|
[221] | 331 | return 0; |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | if ($comment !~ m{\(\w+\)$}) { |
---|
[332] | 335 | print {*STDERR} "Error: no (SERVICE) at the end: $comment\n"; |
---|
[221] | 336 | return 0; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | if ($comment =~ m{\s\s}) { |
---|
[332] | 340 | print {*STDERR} "Error: double space: $comment\n"; |
---|
[221] | 341 | return 0; |
---|
| 342 | } |
---|
| 343 | return 1; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | #-------------------------------------------------------------------------------- |
---|
| 347 | # UTILITY section |
---|
| 348 | #-------------------------------------------------------------------------------- |
---|
| 349 | |
---|
| 350 | sub get_cmd_name { |
---|
| 351 | my ($pkg, $sub) = split /::/, (caller(1))[3]; |
---|
| 352 | $sub =~ s/^cmd_//; |
---|
| 353 | $sub =~ s/_/-/g; |
---|
| 354 | return $sub; |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | #------------------------------------------------------------------------------- |
---|
| 358 | |
---|
| 359 | sub normalize_mac_address { |
---|
| 360 | my $mac_address = shift; |
---|
| 361 | |
---|
| 362 | # D07E-28D1-7AB8 or d07e28-d17ab8 |
---|
| 363 | if ($mac_address =~ m{^ (?: [0-9A-Fa-f]{4} -){2} [0-9A-Fa-f]{4} $}xms |
---|
| 364 | or $mac_address =~ m{^ [0-9A-Fa-f]{6} - [0-9A-Fa-f]{6} $}xms) { |
---|
| 365 | $mac_address =~ s/-//g; |
---|
| 366 | return join q{:}, unpack('(A2)*', uc($mac_address)); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | return join q{:}, map { substr( uc("00$_"), -2) } split m/ [:-] /xms, $mac_address; |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | #------------------------------------------------------------------------------- |
---|
| 373 | |
---|
| 374 | sub normalize_comment { |
---|
| 375 | my $comment = shift; |
---|
| 376 | |
---|
| 377 | $comment =~ s{^(20\d\d)/(\d\d)/(\d\d)\s(.*)$}{$1-$2-$3 $4}; |
---|
| 378 | |
---|
| 379 | return $comment; |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | #-------------------------------------------------------------------------------- |
---|
| 383 | |
---|
| 384 | sub get_mac_from_hostname { |
---|
[345] | 385 | my ($computer_db, $sector, $hostname, $mac) = @_; |
---|
[221] | 386 | |
---|
| 387 | return $mac if $mac ne ''; |
---|
| 388 | return '' if $hostname eq ''; |
---|
| 389 | |
---|
| 390 | LOOP_ON_COMPUTER: |
---|
[345] | 391 | for my $computer (@{$computer_db->{$sector}}) { |
---|
[221] | 392 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 393 | |
---|
| 394 | next LOOP_ON_COMPUTER if $attribute->{'hostname'} ne $hostname; |
---|
| 395 | |
---|
| 396 | return $mac_address; |
---|
| 397 | } |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | #-------------------------------------------------------------------------------- |
---|
| 401 | |
---|
| 402 | sub get_mac_from_ip { |
---|
[345] | 403 | my ($computer_db, $sector, $ip, $mac) = @_; |
---|
[221] | 404 | |
---|
| 405 | return $mac if $mac ne ''; |
---|
| 406 | return '' if $ip eq ''; |
---|
| 407 | |
---|
| 408 | LOOP_ON_COMPUTER: |
---|
[345] | 409 | for my $computer (@{$computer_db->{$sector}}) { |
---|
[221] | 410 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 411 | |
---|
| 412 | next LOOP_ON_COMPUTER if $attribute->{'ip'} ne $ip; |
---|
| 413 | return $mac_address; |
---|
| 414 | } |
---|
| 415 | } |
---|
| 416 | |
---|
| 417 | #-------------------------------------------------------------------------------- |
---|
| 418 | # return a tuple (hash computer, iostat) |
---|
| 419 | # iostat 0/ok, 1/not exist |
---|
| 420 | |
---|
| 421 | sub get_computer_from_mac { |
---|
[345] | 422 | my ($computer_db, $sector, $mac) = @_; |
---|
[221] | 423 | |
---|
| 424 | LOOP_ON_COMPUTER: |
---|
[345] | 425 | for my $computer (@{$computer_db->{$sector}}) { |
---|
[221] | 426 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 427 | |
---|
| 428 | next LOOP_ON_COMPUTER if $mac_address ne $mac; |
---|
| 429 | |
---|
| 430 | return $attribute, 0; |
---|
| 431 | } |
---|
| 432 | return {}, 1; |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | #------------------------------------------------------------------------------- |
---|
| 436 | # ADD computer section |
---|
| 437 | #------------------------------------------------------------------------------- |
---|
| 438 | |
---|
| 439 | #------------------------------------------------------------------------------- |
---|
| 440 | #Nom: add_alias |
---|
| 441 | #Description: ajoute un alias pour une machine. Pour la fonctionnalité CNAME dans le DNS. |
---|
| 442 | |
---|
| 443 | sub add_alias { |
---|
[345] | 444 | my ($computer_db, $hostname, $sector, $alias) = @_; |
---|
[221] | 445 | |
---|
[345] | 446 | control_exist_sector($computer_db, $sector) or exit; |
---|
| 447 | control_exist_hostname($computer_db, $sector, $hostname) or die "Error: host already exist in sector $sector: $hostname\n"; |
---|
[221] | 448 | |
---|
[345] | 449 | my @sectordb = @{$computer_db->{$sector}}; |
---|
[221] | 450 | |
---|
[307] | 451 | LOOP_ON_COMPUTER: |
---|
[345] | 452 | for my $computer (@sectordb) { |
---|
[307] | 453 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 454 | |
---|
| 455 | next LOOP_ON_COMPUTER if $attribute->{'hostname'} ne $hostname; |
---|
| 456 | |
---|
| 457 | $alias .= ' ' . $attribute->{'alias'}; |
---|
| 458 | $attribute->{'alias'} = $alias; |
---|
| 459 | $attribute->{'modify_time'} = time; |
---|
| 460 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[334] | 461 | print "Info: update attribute alias to $alias for host $hostname [OK]\n"; |
---|
[307] | 462 | exit; |
---|
[221] | 463 | } |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | #------------------------------------------------------------------------------- |
---|
| 467 | #Nom: add_static |
---|
| 468 | #Description: ajoute une machine non dhcp (donc à adressage fixe dans le fichier YAML) |
---|
| 469 | |
---|
| 470 | sub add_static { |
---|
[345] | 471 | my ($computer_db, $hostname, $sector, $ip, $mac, $comment) = @_; |
---|
[221] | 472 | |
---|
| 473 | $mac = normalize_mac_address($mac); |
---|
| 474 | $comment = normalize_comment($comment); |
---|
[345] | 475 | control_exist_hostname($computer_db, $sector, $hostname) or die "Error: host already exist in sector $sector: $hostname\n"; |
---|
[352] | 476 | control_syntax_mac_address($mac) or exit; |
---|
| 477 | control_exist_mac($computer_db, $mac) or die "Error: physical MAC address already exists: $mac\n"; |
---|
[357] | 478 | control_syntax_ip($ip) or die "Error: bad IP syntax $ip\n"; |
---|
[352] | 479 | control_exist_ip($computer_db, $ip) or die "Error: IP $ip address already exist in sector $sector\n"; |
---|
| 480 | control_ip_in_range($computer_db, $sector, $ip) or die "Error: IP $ip is not in sector $sector IP range.\n"; |
---|
| 481 | control_syntax_comment($comment) or exit; |
---|
[221] | 482 | my $timestamp = time; |
---|
[345] | 483 | push @{$computer_db->{$sector}}, { $mac => { |
---|
[221] | 484 | 'hostname' => $hostname, |
---|
| 485 | 'ip' => $ip, |
---|
| 486 | 'address_type' => 'static', |
---|
| 487 | 'enabled' => 'yes', |
---|
| 488 | 'create_time' => $timestamp, |
---|
| 489 | 'modify_time' => $timestamp, |
---|
| 490 | 'comment' => $comment, |
---|
| 491 | 'alias' => '', |
---|
| 492 | }}; |
---|
[345] | 493 | print "Info: add the host: $hostname, IP: $ip, MAC: $mac, sector: $sector [OK]\n"; |
---|
[221] | 494 | |
---|
[238] | 495 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[221] | 496 | } |
---|
| 497 | |
---|
| 498 | |
---|
| 499 | #------------------------------------------------------------------------------- |
---|
| 500 | #Nom: add_dhcp |
---|
| 501 | #Description: section à corriger pour prendre en compte l'ajout d'une machine dans un pool dhcp |
---|
[348] | 502 | #--- usage: ddt add-dhcp -s legi-sector03 -h meolpacif -m 00:18:F3:03:6F:66 -i 194.254.66.165 |
---|
[221] | 503 | |
---|
| 504 | sub add_dhcp { |
---|
[345] | 505 | my ($computer_db, $hostname, $sector, $ip, $mac, $comment) = @_; |
---|
[221] | 506 | |
---|
| 507 | my $timestamp = time; |
---|
| 508 | $mac = normalize_mac_address($mac); |
---|
| 509 | $comment = normalize_comment($comment); |
---|
[352] | 510 | control_exist_sector($computer_db, $sector) or exit; |
---|
[345] | 511 | control_exist_hostname($computer_db, $sector, $hostname) or die "Error: host already exist in sector $sector: $hostname\n"; |
---|
[352] | 512 | control_syntax_mac_address($mac) or exit; |
---|
| 513 | control_exist_mac($computer_db, $mac) or die "Error: physical MAC address already exists: $mac\n"; |
---|
[357] | 514 | control_syntax_ip($ip) or die "Error: bad IP syntax $ip\n"; |
---|
[352] | 515 | control_exist_ip($computer_db, $ip) or die "Error: IP address already exist in sector $sector: $ip.\n"; |
---|
| 516 | control_ip_in_range($computer_db, $sector, $ip) or die "Error: IP $ip is not in sector $sector IP range.\n"; |
---|
| 517 | control_syntax_comment($comment) or exit; |
---|
[221] | 518 | |
---|
[345] | 519 | push @{$computer_db->{$sector}}, { $mac => { |
---|
[221] | 520 | 'hostname' => $hostname, |
---|
| 521 | 'ip' => $ip, |
---|
| 522 | 'address_type' => 'dhcp', |
---|
| 523 | 'enabled' => 'yes', |
---|
| 524 | 'create_time' => $timestamp, |
---|
| 525 | 'modify_time' => $timestamp, |
---|
| 526 | 'comment' => $comment, |
---|
| 527 | 'alias' => '', |
---|
| 528 | }}; |
---|
[345] | 529 | print "Add the computer: $hostname, IP: $ip, MAC: $mac, sector: $sector\n"; |
---|
[221] | 530 | |
---|
[238] | 531 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[221] | 532 | } |
---|
| 533 | |
---|
| 534 | #------------------------------------------------------------------------------- |
---|
[348] | 535 | #--- usage: ddt add-float -s legi-sector03 -h meolpacif -m 00:18:F3:03:6F:66 -i 194.254.66.165 |
---|
[221] | 536 | |
---|
| 537 | sub add_float { |
---|
[345] | 538 | my ($computer_db, $pool, $sector, $mac, $comment) = @_; |
---|
[221] | 539 | |
---|
| 540 | my $timestamp = time; |
---|
| 541 | $mac = normalize_mac_address($mac); |
---|
| 542 | $comment = normalize_comment($comment); |
---|
[345] | 543 | control_exist_sector($computer_db, $sector) or exit; |
---|
[221] | 544 | control_syntax_mac_address($mac) or exit; |
---|
[332] | 545 | control_exist_mac($computer_db, $mac) or die "Error: physical MAC address already exists: $mac\n"; |
---|
| 546 | control_exist_pool($computer_db, $pool) or die "Error: the pool doesn't exists: $pool\n"; |
---|
[221] | 547 | control_syntax_comment($comment) or exit; |
---|
[345] | 548 | push @{$computer_db->{$sector}}, { $mac => { |
---|
[221] | 549 | 'hostname' => $pool, |
---|
| 550 | 'ip' => $pool, |
---|
| 551 | 'address_type' => 'pool-dhcp', |
---|
| 552 | 'enabled' => 'yes', |
---|
| 553 | 'create_time' => $timestamp, |
---|
| 554 | 'modify_time' => $timestamp, |
---|
| 555 | 'comment' => $comment, |
---|
| 556 | }}; |
---|
[345] | 557 | print "Info: add the computer in pool MAC: $mac, sector: $sector, Pool: $pool [OK]\n"; |
---|
[221] | 558 | |
---|
[238] | 559 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[221] | 560 | } |
---|
| 561 | |
---|
| 562 | #------------------------------------------------------------------------------- |
---|
| 563 | # ADD computer section |
---|
| 564 | #------------------------------------------------------------------------------- |
---|
| 565 | |
---|
| 566 | sub cmd_add_alias { |
---|
| 567 | local @ARGV = @_; |
---|
| 568 | |
---|
| 569 | my $help = get_cmd_name(); |
---|
[345] | 570 | my ($hostname, $sector, $alias); |
---|
[221] | 571 | |
---|
| 572 | GetOptions( |
---|
| 573 | 'hostname|h=s' => \$hostname, |
---|
[346] | 574 | 'sector|s|d=s' => \$sector, |
---|
[221] | 575 | 'alias|a=s' => \$alias, |
---|
| 576 | ); |
---|
| 577 | |
---|
[345] | 578 | ($hostname, $sector) = split /\./, $hostname, 2 if $hostname =~ m/\./; |
---|
[221] | 579 | exit_on_error_option($help) |
---|
| 580 | if $hostname eq '' |
---|
[345] | 581 | or $sector eq '' |
---|
[221] | 582 | or $alias eq ''; |
---|
| 583 | |
---|
[238] | 584 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
[345] | 585 | add_alias($computer_db, $hostname, $sector, $alias); |
---|
[221] | 586 | } |
---|
| 587 | |
---|
| 588 | #------------------------------------------------------------------------------- |
---|
| 589 | |
---|
| 590 | sub cmd_add_dhcp { |
---|
| 591 | local @ARGV = @_; |
---|
| 592 | |
---|
| 593 | my $help = get_cmd_name(); |
---|
[345] | 594 | my ($hostname, $sector, $ip, $mac, $comment); |
---|
[221] | 595 | |
---|
| 596 | GetOptions( |
---|
| 597 | 'hostname|h=s' => \$hostname, |
---|
[346] | 598 | 'sector|s|d=s' => \$sector, |
---|
[221] | 599 | 'ip|i=s' => \$ip, |
---|
| 600 | 'mac|m=s' => \$mac, |
---|
| 601 | 'comment|c=s' => \$comment, |
---|
| 602 | ); |
---|
| 603 | |
---|
[345] | 604 | ($hostname, $sector) = split /\./, $hostname, 2 if $hostname =~ m/\./; |
---|
[221] | 605 | exit_on_error_option($help) |
---|
| 606 | if $hostname eq '' |
---|
[345] | 607 | or $sector eq '' |
---|
[221] | 608 | or $ip eq '' |
---|
| 609 | or $mac eq '' |
---|
| 610 | or $comment eq ''; |
---|
| 611 | |
---|
[238] | 612 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
[345] | 613 | add_dhcp($computer_db, $hostname, $sector, $ip, $mac, $comment); |
---|
[221] | 614 | } |
---|
| 615 | |
---|
| 616 | #------------------------------------------------------------------------------- |
---|
| 617 | |
---|
| 618 | sub cmd_add_float { |
---|
| 619 | local @ARGV = @_; |
---|
| 620 | |
---|
| 621 | my $help = get_cmd_name(); |
---|
[345] | 622 | my ($pool, $sector, $mac, $comment); |
---|
[221] | 623 | |
---|
| 624 | GetOptions( |
---|
| 625 | 'pool|p=s' => \$pool, |
---|
[346] | 626 | 'sector|s|d=s' => \$sector, |
---|
[221] | 627 | 'mac|m=s' => \$mac, |
---|
| 628 | 'comment|c=s' => \$comment, |
---|
| 629 | ); |
---|
| 630 | |
---|
[345] | 631 | ($pool, $sector) = split /\./, $pool, 2 if $pool =~ m/\./; |
---|
[221] | 632 | exit_on_error_option($help) |
---|
| 633 | if $pool eq '' |
---|
[345] | 634 | or $sector eq '' |
---|
[221] | 635 | or $mac eq '' |
---|
| 636 | or $comment eq ''; |
---|
| 637 | |
---|
[238] | 638 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
[345] | 639 | add_float($computer_db, $pool, $sector, $mac, $comment); |
---|
[221] | 640 | } |
---|
| 641 | |
---|
| 642 | #------------------------------------------------------------------------------- |
---|
| 643 | |
---|
| 644 | sub cmd_add_static { |
---|
| 645 | local @ARGV = @_; |
---|
| 646 | |
---|
| 647 | my $help = get_cmd_name(); |
---|
[345] | 648 | my ($hostname, $sector, $ip, $mac, $comment); |
---|
[221] | 649 | |
---|
| 650 | GetOptions( |
---|
| 651 | 'hostname|h=s' => \$hostname, |
---|
[346] | 652 | 'sector|s|d=s' => \$sector, |
---|
[221] | 653 | 'ip|i=s' => \$ip, |
---|
| 654 | 'mac|m=s' => \$mac, |
---|
| 655 | 'comment|c=s' => \$comment, |
---|
| 656 | ); |
---|
| 657 | |
---|
[345] | 658 | ($hostname, $sector) = split /\./, $hostname, 2 if $hostname =~ m/\./; |
---|
[221] | 659 | exit_on_error_option($help) |
---|
| 660 | if $hostname eq '' |
---|
[345] | 661 | or $sector eq '' |
---|
[221] | 662 | or $ip eq '' |
---|
| 663 | or $mac eq '' |
---|
| 664 | or $comment eq ''; |
---|
| 665 | |
---|
[238] | 666 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
[345] | 667 | add_static($computer_db, $hostname, $sector, $ip, $mac, $comment); |
---|
[221] | 668 | } |
---|
| 669 | |
---|
| 670 | #------------------------------------------------------------------------------- |
---|
| 671 | # No real computer, just an entry A in DNS with virtual MAC |
---|
| 672 | |
---|
| 673 | sub cmd_add_virtual { |
---|
| 674 | local @ARGV = @_; |
---|
| 675 | |
---|
| 676 | my $help = get_cmd_name(); |
---|
[345] | 677 | my ($hostname, $sector, $ip, $comment); |
---|
[221] | 678 | |
---|
| 679 | GetOptions( |
---|
| 680 | 'hostname|h=s' => \$hostname, |
---|
[346] | 681 | 'sector|s|d=s' => \$sector, |
---|
[221] | 682 | 'ip|i=s' => \$ip, |
---|
| 683 | 'comment|c=s' => \$comment, |
---|
| 684 | ); |
---|
| 685 | |
---|
[345] | 686 | ($hostname, $sector) = split /\./, $hostname, 2 if $hostname =~ m/\./; |
---|
[221] | 687 | exit_on_error_option($help) |
---|
| 688 | if $hostname eq '' |
---|
[345] | 689 | or $sector eq '' |
---|
[221] | 690 | or $ip eq '' |
---|
| 691 | or $comment eq ''; |
---|
| 692 | |
---|
[238] | 693 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 694 | |
---|
[221] | 695 | $comment = normalize_comment($comment); |
---|
| 696 | my $timestamp = time; |
---|
| 697 | |
---|
[352] | 698 | control_exist_sector($computer_db, $sector) or exit; |
---|
[345] | 699 | control_exist_hostname($computer_db, $sector, $hostname) or die "Error: host already exist in sector $sector: $hostname\n"; |
---|
[357] | 700 | control_syntax_ip($ip) or die "Error: bad IP syntax $ip\n"; |
---|
[352] | 701 | control_exist_ip($computer_db, $ip) or die "Error: IP address already exist in sector $sector: $ip.\n"; |
---|
| 702 | control_ip_in_range($computer_db, $sector, $ip) or die "Error: IP $ip is not in sector $sector IP range.\n"; |
---|
| 703 | control_syntax_comment($comment) or exit; |
---|
[221] | 704 | |
---|
| 705 | my $mac = join ':', 'FF', 'FF', map({sprintf("%02X", $_)} split(/\./, $ip)); |
---|
| 706 | control_syntax_mac_address($mac) or exit; |
---|
[332] | 707 | control_exist_mac($computer_db, $mac) or die "Error: virtual physical MAC address already exists: $mac\n"; |
---|
[221] | 708 | |
---|
[345] | 709 | push @{$computer_db->{$sector}}, { $mac => { |
---|
[221] | 710 | 'hostname' => $hostname, |
---|
| 711 | 'ip' => $ip, |
---|
| 712 | 'address_type' => 'static', |
---|
| 713 | 'enabled' => 'yes', |
---|
| 714 | 'create_time' => $timestamp, |
---|
| 715 | 'modify_time' => $timestamp, |
---|
| 716 | 'comment' => $comment, |
---|
| 717 | }}; |
---|
[345] | 718 | print "Add the virtual computer: $hostname, IP: $ip, sector: $sector\n"; |
---|
[221] | 719 | |
---|
[238] | 720 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[221] | 721 | } |
---|
| 722 | |
---|
| 723 | #------------------------------------------------------------------------------- |
---|
| 724 | # CHANGE computer section |
---|
| 725 | #------------------------------------------------------------------------------- |
---|
| 726 | |
---|
| 727 | #------------------------------------------------------------------------------- |
---|
| 728 | #Nom: change_mac |
---|
| 729 | #Description: change la mac adresse d'une machine en saisissant soit l'ip |
---|
| 730 | # soit le nom de la mahcine et spécifiant le domaine |
---|
[348] | 731 | #--- usage: ddt change-mac -s legi-sector03 -h meolpacif -m 00:18:F3:03:6F:66 |
---|
| 732 | #--- usage: ddt change-mac -s legi-sector03 -i 194.254.66.187 -m 00:18:F3:03:6F:66 |
---|
[221] | 733 | |
---|
| 734 | sub change_mac { |
---|
[345] | 735 | my ($hostname, $sector, $ip, $mac) = @_; |
---|
[221] | 736 | |
---|
[238] | 737 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 738 | |
---|
[221] | 739 | $mac = normalize_mac_address($mac); |
---|
[345] | 740 | control_exist_sector($computer_db, $sector) or exit; |
---|
[221] | 741 | control_syntax_mac_address($mac) or exit; |
---|
[332] | 742 | control_exist_mac($computer_db, $mac) or die "Error: physical MAC address already exists: $mac\n"; |
---|
[221] | 743 | if ($ip ne '') { |
---|
[357] | 744 | control_syntax_ip($ip) or die "Error: bad IP syntax $ip\n"; |
---|
[238] | 745 | if ( control_exist_ip($computer_db, $ip) == 1 ) { |
---|
[332] | 746 | print "Error: unkown IP address: $ip\n"; |
---|
[221] | 747 | exit; |
---|
| 748 | } |
---|
[345] | 749 | my @sectordb = @{$computer_db->{$sector}}; |
---|
[308] | 750 | LOOP_ON_COMPUTER: |
---|
[345] | 751 | for my $computer (@sectordb) { |
---|
[308] | 752 | my ($mac_address, $attribute) = %{$computer}; |
---|
[345] | 753 | die "Error: physical MAC address $mac already exists in sector $sector\n" if $mac_address eq $mac; |
---|
[308] | 754 | |
---|
[332] | 755 | next LOOP_ON_COMPUTER if $attribute->{'ip'} ne $ip; |
---|
[308] | 756 | |
---|
| 757 | $attribute->{'modify_time'} = time; |
---|
[335] | 758 | $computer->{$mac} = $attribute; # add new mac |
---|
[332] | 759 | delete $computer->{$mac_address}; # remove old mac |
---|
| 760 | |
---|
[308] | 761 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[345] | 762 | print "Info: update host $attribute->{'hostname'}, sector $sector, MAC $mac, IP $attribute->{'ip'} [OK]\n"; |
---|
[308] | 763 | exit; |
---|
[221] | 764 | } |
---|
| 765 | } |
---|
| 766 | elsif ($hostname ne '') { |
---|
[345] | 767 | if ( control_exist_hostname($computer_db, $sector, $hostname) == 1 ) { |
---|
| 768 | die "Error: unkown host $hostname, sector $sector\n"; |
---|
[221] | 769 | } |
---|
[345] | 770 | my @sectordb = @{$computer_db->{$sector}}; |
---|
[308] | 771 | LOOP_ON_COMPUTER: |
---|
[345] | 772 | for my $computer (@sectordb) { |
---|
[308] | 773 | my ($mac_address, $attribute) = %{$computer}; |
---|
[345] | 774 | die "Error: physical MAC address $mac already exists in sector $sector\n" if $mac_address eq $mac; |
---|
[308] | 775 | |
---|
[332] | 776 | next LOOP_ON_COMPUTER if $attribute->{'hostname'} ne $hostname; |
---|
[308] | 777 | |
---|
| 778 | $attribute->{'modify_time'} = time; |
---|
[335] | 779 | $computer->{$mac} = $attribute; # add new mac |
---|
[332] | 780 | delete $computer->{$mac_address}; # remove old mac |
---|
| 781 | |
---|
[308] | 782 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[345] | 783 | print "Info: update host $attribute->{'hostname'}, sector $sector, MAC $mac, IP $attribute->{'ip'} [OK]\n"; |
---|
[308] | 784 | exit; |
---|
[221] | 785 | } |
---|
| 786 | } |
---|
| 787 | } |
---|
| 788 | |
---|
| 789 | #------------------------------------------------------------------------------- |
---|
| 790 | #Nom: change_ip |
---|
| 791 | #Description: change l'adresse IP d'une machine en saisissant le nom de la machine |
---|
| 792 | # et le domaine |
---|
| 793 | |
---|
| 794 | sub change_ip { |
---|
[345] | 795 | my ($hostname, $sector, $ip) = @_; |
---|
[221] | 796 | |
---|
[238] | 797 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 798 | |
---|
[345] | 799 | control_exist_sector($computer_db, $sector) or exit; |
---|
| 800 | if ( control_exist_hostname($computer_db, $sector, $hostname) == 1 ) { |
---|
| 801 | die "Error: unkown host: $hostname, in sector: $sector\n"; |
---|
[221] | 802 | } |
---|
[357] | 803 | control_syntax_ip($ip) or die "Error: bad IP syntax $ip\n"; |
---|
[352] | 804 | control_exist_ip($computer_db, $ip) or die "Error: IP $ip address already exist in sector $sector\n"; |
---|
| 805 | control_ip_in_range($computer_db, $sector, $ip) or die "Error: IP $ip is not in sector $sector IP range.\n"; |
---|
[308] | 806 | |
---|
[345] | 807 | my @sectordb = @{$computer_db->{$sector}}; |
---|
[308] | 808 | |
---|
| 809 | LOOP_ON_COMPUTER: |
---|
[345] | 810 | for my $computer (@sectordb) { |
---|
[308] | 811 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 812 | |
---|
[310] | 813 | next LOOP_ON_COMPUTER if $attribute->{'hostname'} ne $hostname; |
---|
[308] | 814 | |
---|
| 815 | if ($attribute->{'address_type'} eq 'pool-dhcp') { |
---|
[345] | 816 | die "Error: host $hostname from sector $sector belongs to a a pool [FAILED]" . |
---|
[308] | 817 | " ... use 'del-float' command before"; |
---|
[221] | 818 | } |
---|
[308] | 819 | |
---|
| 820 | $attribute->{'modify_time'} = time; |
---|
| 821 | $attribute->{'ip'} = $ip; |
---|
| 822 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[334] | 823 | print "Info: update host $hostname MAC: $mac_address IP: $ip [OK]\n"; |
---|
[308] | 824 | exit; |
---|
[221] | 825 | } |
---|
| 826 | } |
---|
| 827 | |
---|
| 828 | #------------------------------------------------------------------------------- |
---|
| 829 | #Nom: change_host |
---|
| 830 | #Description: change le computer hostname en saisissant l'IP et le domaine |
---|
| 831 | |
---|
| 832 | sub change_host { |
---|
[345] | 833 | my ($hostname, $sector, $ip) = @_; |
---|
[221] | 834 | |
---|
[238] | 835 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 836 | |
---|
[345] | 837 | control_exist_sector($computer_db, $sector) or exit; |
---|
[357] | 838 | control_syntax_ip($ip) or die "Error: bad IP syntax $ip\n"; |
---|
[238] | 839 | if ( control_exist_ip($computer_db, $ip) == 1 ) { |
---|
[332] | 840 | die "Error: unkown IP address: $ip\n"; |
---|
[221] | 841 | } |
---|
[345] | 842 | control_exist_hostname($computer_db, $sector, $hostname) or die "Error: host already exist in sector $sector: $hostname\n"; |
---|
[221] | 843 | |
---|
[345] | 844 | my @sectordb = @{$computer_db->{$sector}}; |
---|
[308] | 845 | |
---|
| 846 | LOOP_ON_COMPUTER: |
---|
[345] | 847 | for my $computer (@sectordb) { |
---|
[308] | 848 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 849 | |
---|
| 850 | next LOOP_ON_COMPUTER if $attribute->{'ip'} ne $ip; |
---|
| 851 | |
---|
| 852 | $attribute->{'modify_time'} = time; |
---|
| 853 | $attribute->{'hostname'} = $hostname; |
---|
| 854 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[334] | 855 | print "Info: update host $hostname MAC: $mac_address IP: $ip [OK]\n"; |
---|
[308] | 856 | exit; |
---|
[221] | 857 | } |
---|
[308] | 858 | |
---|
[332] | 859 | die "Error: failed to update hostname $hostname [FAILED]\n" . |
---|
[345] | 860 | " ... no IP $ip belongs to the sector $sector\n"; |
---|
[221] | 861 | } |
---|
| 862 | |
---|
| 863 | #-------------------------------------------------------------------------------- |
---|
| 864 | |
---|
| 865 | sub cmd_change_mac { |
---|
| 866 | local @ARGV = @_; |
---|
| 867 | |
---|
| 868 | my $help = get_cmd_name(); |
---|
[345] | 869 | my ($hostname, $sector, $ip, $mac); |
---|
[221] | 870 | |
---|
| 871 | GetOptions( |
---|
| 872 | 'hostname|h=s' => \$hostname, |
---|
[346] | 873 | 'sector|s|d=s' => \$sector, |
---|
[221] | 874 | 'ip|i=s' => \$ip, |
---|
| 875 | 'mac|m=s' => \$mac, |
---|
| 876 | ); |
---|
| 877 | |
---|
[345] | 878 | ($hostname, $sector) = split /\./, $hostname, 2 if $hostname =~ m/\./; |
---|
[221] | 879 | exit_on_error_option($help) |
---|
[345] | 880 | if $sector eq '' |
---|
[221] | 881 | or $mac eq ''; |
---|
| 882 | exit_on_error_option($help) |
---|
| 883 | if $hostname ne '' |
---|
| 884 | and $ip ne ''; |
---|
| 885 | |
---|
[345] | 886 | change_mac($hostname, $sector, $ip, $mac); |
---|
[221] | 887 | } |
---|
| 888 | |
---|
| 889 | #-------------------------------------------------------------------------------- |
---|
| 890 | |
---|
| 891 | sub cmd_change_ip { |
---|
| 892 | local @ARGV = @_; |
---|
| 893 | |
---|
| 894 | my $help = get_cmd_name(); |
---|
[345] | 895 | my ($hostname, $sector, $ip); |
---|
[221] | 896 | |
---|
| 897 | GetOptions( |
---|
| 898 | 'hostname|h=s' => \$hostname, |
---|
[346] | 899 | 'sector|s|d=s' => \$sector, |
---|
[221] | 900 | 'ip|i=s' => \$ip, |
---|
| 901 | ); |
---|
| 902 | |
---|
[345] | 903 | ($hostname, $sector) = split /\./, $hostname, 2 if $hostname =~ m/\./; |
---|
[221] | 904 | exit_on_error_option($help) |
---|
| 905 | if $hostname eq '' |
---|
[345] | 906 | or $sector eq '' |
---|
[221] | 907 | or $ip eq ''; |
---|
| 908 | |
---|
[345] | 909 | change_ip($hostname, $sector, $ip); |
---|
[221] | 910 | } |
---|
| 911 | |
---|
| 912 | #-------------------------------------------------------------------------------- |
---|
| 913 | |
---|
| 914 | sub cmd_change_host { |
---|
| 915 | local @ARGV = @_; |
---|
| 916 | |
---|
| 917 | my $help = get_cmd_name(); |
---|
[345] | 918 | my ($hostname, $sector, $ip); |
---|
[221] | 919 | |
---|
| 920 | GetOptions( |
---|
| 921 | 'hostname|h=s' => \$hostname, |
---|
[346] | 922 | 'sector|s|d=s' => \$sector, |
---|
[221] | 923 | 'ip|i=s' => \$ip, |
---|
| 924 | ); |
---|
| 925 | |
---|
[345] | 926 | ($hostname, $sector) = split /\./, $hostname, 2 if $hostname =~ m/\./; |
---|
[221] | 927 | exit_on_error_option($help) |
---|
| 928 | if $hostname eq '' |
---|
[345] | 929 | or $sector eq '' |
---|
[221] | 930 | or $ip eq ''; |
---|
| 931 | |
---|
[345] | 932 | change_host($hostname, $sector, $ip); |
---|
[221] | 933 | } |
---|
| 934 | |
---|
| 935 | #-------------------------------------------------------------------------------- |
---|
| 936 | |
---|
| 937 | sub cmd_change_comment { |
---|
| 938 | local @ARGV = @_; |
---|
| 939 | |
---|
| 940 | my $help = get_cmd_name(); |
---|
[345] | 941 | my ($sector, $mac, $comment); |
---|
[221] | 942 | |
---|
| 943 | GetOptions( |
---|
[346] | 944 | 'sector|s|d=s' => \$sector, |
---|
[221] | 945 | 'mac|m=s' => \$mac, |
---|
| 946 | 'comment|c=s' => \$comment, |
---|
| 947 | ); |
---|
| 948 | |
---|
| 949 | exit_on_error_option($help) |
---|
[345] | 950 | if $sector eq '' |
---|
[221] | 951 | or $mac eq '' |
---|
| 952 | or $comment eq ''; |
---|
| 953 | |
---|
| 954 | $mac = normalize_mac_address($mac); |
---|
| 955 | $comment = normalize_comment($comment); |
---|
| 956 | |
---|
[238] | 957 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 958 | |
---|
[345] | 959 | control_exist_sector($computer_db, $sector) or exit; |
---|
[221] | 960 | control_syntax_mac_address($mac) or exit; |
---|
| 961 | control_syntax_comment($comment) or exit; |
---|
| 962 | |
---|
[345] | 963 | my @sectordb = @{$computer_db->{$sector}}; |
---|
[221] | 964 | |
---|
[311] | 965 | LOOP_ON_COMPUTER: |
---|
[345] | 966 | for my $computer (@sectordb) { |
---|
[311] | 967 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 968 | |
---|
| 969 | next LOOP_ON_COMPUTER if $mac_address ne $mac; |
---|
| 970 | |
---|
| 971 | $attribute->{'modify_time'} = time; |
---|
| 972 | $attribute->{'comment'} = $comment; |
---|
| 973 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
| 974 | exit; |
---|
[221] | 975 | } |
---|
[312] | 976 | die "Error : Host $mac comment [FAILED]\n" . |
---|
[345] | 977 | " ... No MAC: $mac belongs to the domaine set $sector.\n"; |
---|
[221] | 978 | } |
---|
| 979 | |
---|
| 980 | #-------------------------------------------------------------------------------- |
---|
| 981 | |
---|
[345] | 982 | sub cmd_change_sector { |
---|
[221] | 983 | local @ARGV = @_; |
---|
| 984 | |
---|
| 985 | my $help = get_cmd_name(); |
---|
[345] | 986 | my ($sector, $ip, $mac); |
---|
[221] | 987 | |
---|
| 988 | GetOptions( |
---|
[346] | 989 | 'sector|s|d=s' => \$sector, |
---|
[221] | 990 | 'ip|i=s' => \$ip, |
---|
| 991 | 'mac|m=s' => \$mac, |
---|
| 992 | ); |
---|
| 993 | |
---|
| 994 | exit_on_error_option($help) |
---|
[345] | 995 | if $sector eq '' |
---|
[221] | 996 | or $ip eq '' |
---|
| 997 | or $mac eq ''; |
---|
| 998 | |
---|
| 999 | $mac = normalize_mac_address($mac); |
---|
| 1000 | |
---|
[238] | 1001 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 1002 | |
---|
[357] | 1003 | control_exist_sector($computer_db, $sector) or exit; |
---|
| 1004 | control_syntax_ip($ip) or die "Error: bad IP syntax $ip\n"; |
---|
| 1005 | control_syntax_mac_address($mac) or exit; |
---|
[221] | 1006 | |
---|
[345] | 1007 | LOOP_ON_SECTOR: |
---|
| 1008 | for my $sector_current (keys %{$computer_db}) { |
---|
| 1009 | next if $sector_current eq 'dset'; |
---|
| 1010 | next if $sector_current eq 'pool'; |
---|
| 1011 | next if $sector_current eq 'pxe'; |
---|
| 1012 | next if $sector_current eq 'tag'; |
---|
| 1013 | next if $sector_current eq 'version'; |
---|
[221] | 1014 | |
---|
[345] | 1015 | my @sectordb = @{$computer_db->{$sector_current}}; |
---|
[306] | 1016 | my $computer_index = 0; |
---|
[311] | 1017 | LOOP_ON_COMPUTER: |
---|
[345] | 1018 | for my $computer (@sectordb) { |
---|
[311] | 1019 | my ($mac_address, $attribute) = %{$computer}; |
---|
[221] | 1020 | |
---|
[329] | 1021 | $computer_index++, next LOOP_ON_COMPUTER if $mac_address ne $mac; |
---|
[345] | 1022 | next LOOP_ON_SECTOR if $attribute->{'ip'} ne $ip; |
---|
[221] | 1023 | |
---|
[311] | 1024 | $attribute->{'modify_time'} = time; |
---|
[345] | 1025 | splice(@{$computer_db->{$sector_current}}, $computer_index => 1); |
---|
| 1026 | push @{$computer_db->{$sector}}, { $mac => $attribute }; |
---|
[311] | 1027 | |
---|
| 1028 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
| 1029 | exit; |
---|
[221] | 1030 | } |
---|
| 1031 | } |
---|
[345] | 1032 | die "Error: update of sector $sector [FAILED]\n" . |
---|
[311] | 1033 | " ... MAC $mac and IP $ip don't exists in the database\n"; |
---|
[221] | 1034 | } |
---|
| 1035 | |
---|
| 1036 | #-------------------------------------------------------------------------------- |
---|
| 1037 | |
---|
| 1038 | sub cmd_change_tag { |
---|
| 1039 | local @ARGV = @_; |
---|
| 1040 | |
---|
| 1041 | my $help = get_cmd_name(); |
---|
[345] | 1042 | my ($hostname, $sector, $ip, $mac, $tags); |
---|
[221] | 1043 | |
---|
| 1044 | GetOptions( |
---|
| 1045 | 'hostname|h=s' => \$hostname, |
---|
[346] | 1046 | 'sector|s|d=s' => \$sector, |
---|
[221] | 1047 | 'ip|i=s' => \$ip, |
---|
| 1048 | 'mac|m=s' => \$mac, |
---|
| 1049 | 'tag|t=s' => \$tags, |
---|
| 1050 | ); |
---|
| 1051 | |
---|
[345] | 1052 | ($hostname, $sector) = split /\./, $hostname, 2 if $hostname =~ m/\./; |
---|
[221] | 1053 | |
---|
| 1054 | exit_on_error_option($help) |
---|
[345] | 1055 | if $sector eq '' |
---|
[221] | 1056 | or $tags eq ''; |
---|
| 1057 | exit_on_error_option($help) |
---|
| 1058 | if $mac eq '' |
---|
| 1059 | and $hostname eq '' |
---|
| 1060 | and $ip eq ''; |
---|
| 1061 | |
---|
| 1062 | $mac = normalize_mac_address($mac); |
---|
| 1063 | |
---|
[238] | 1064 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 1065 | |
---|
[221] | 1066 | if ($tags !~ m/^ (?:\w+,)* \w+ $/xms) { |
---|
[332] | 1067 | die "Error: bad format for tags (comma separated list): $tags\n"; |
---|
[221] | 1068 | } |
---|
| 1069 | |
---|
| 1070 | for my $tag (split/,/, $tags) { |
---|
| 1071 | next if $tag eq 'universal'; |
---|
[238] | 1072 | die "Error: TAG doesn't exist in the database. Create it before with create_tag: $tag\n" if not exists $computer_db->{'tag'}{$tag}; |
---|
[221] | 1073 | } |
---|
| 1074 | |
---|
[345] | 1075 | control_exist_sector($computer_db, $sector) or exit; |
---|
[227] | 1076 | |
---|
[345] | 1077 | $mac = get_mac_from_ip($computer_db, $sector, $ip, $mac) if $ip ne ''; |
---|
| 1078 | $mac = get_mac_from_hostname($computer_db, $sector, $hostname, $mac) if $hostname ne ''; |
---|
[221] | 1079 | control_syntax_mac_address($mac) or exit; |
---|
| 1080 | |
---|
| 1081 | LOOP_ON_COMPUTER: |
---|
[345] | 1082 | for my $computer (@{$computer_db->{$sector}}) { |
---|
[221] | 1083 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 1084 | |
---|
| 1085 | next LOOP_ON_COMPUTER if $mac_address ne $mac; |
---|
| 1086 | |
---|
| 1087 | $attribute->{'tag'} = $tags; |
---|
| 1088 | $attribute->{'modify_time'} = time; |
---|
| 1089 | |
---|
| 1090 | delete $attribute->{'tag'} if $tags eq 'universal'; |
---|
[238] | 1091 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[221] | 1092 | exit; |
---|
| 1093 | } |
---|
| 1094 | print "Mise à jour du commentaire de la machine [FAILED]\n"; |
---|
[345] | 1095 | print "L'adresse MAC: $mac n'existe pas dans le domaine: $sector.\n"; |
---|
[221] | 1096 | } |
---|
| 1097 | |
---|
| 1098 | #------------------------------------------------------------------------------- |
---|
| 1099 | # ACTIVATION section |
---|
| 1100 | #------------------------------------------------------------------------------- |
---|
| 1101 | |
---|
| 1102 | #------------------------------------------------------------------------------- |
---|
| 1103 | #Nom: disable_pc |
---|
| 1104 | #Description: désactive une machine (du DHCP ou en IP statique, et du DNS) (champs enabled=non) |
---|
| 1105 | |
---|
| 1106 | sub disable_pc { |
---|
[345] | 1107 | my ($hostname, $sector, $ip) = @_; |
---|
[221] | 1108 | |
---|
[238] | 1109 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 1110 | |
---|
[304] | 1111 | if ($ip ne '') { # disable by IP |
---|
[357] | 1112 | control_syntax_ip($ip) or die "Error: bad IP syntax $ip\n";; |
---|
[238] | 1113 | if ( control_exist_ip($computer_db, $ip) == 1 ) { |
---|
[332] | 1114 | die "Error: unkown IP address: $ip [FAILED]\n"; |
---|
[221] | 1115 | } |
---|
| 1116 | |
---|
[345] | 1117 | for my $sector_current (keys %{$computer_db}) { |
---|
| 1118 | next if $sector_current eq 'dset'; |
---|
| 1119 | next if $sector_current eq 'pool'; |
---|
| 1120 | next if $sector_current eq 'pxe'; |
---|
| 1121 | next if $sector_current eq 'tag'; |
---|
| 1122 | next if $sector_current eq 'version'; |
---|
[221] | 1123 | |
---|
[345] | 1124 | my @sectordb = @{$computer_db->{$sector_current}}; |
---|
[304] | 1125 | LOOP_ON_COMPUTER: |
---|
[345] | 1126 | for my $computer (@sectordb) { |
---|
[304] | 1127 | my ($mac_address, $attribute) = %{$computer}; |
---|
[221] | 1128 | |
---|
[304] | 1129 | next LOOP_ON_COMPUTER if $attribute->{'ip'} ne $ip; |
---|
| 1130 | |
---|
| 1131 | if ($attribute->{'enabled'} eq 'no') { |
---|
[345] | 1132 | print "Info: IP $ip from sector $sector_current is already disable [OK]" . |
---|
[304] | 1133 | " ... Status: $attribute->{'enabled'}\n"; |
---|
| 1134 | exit; |
---|
[221] | 1135 | } |
---|
[304] | 1136 | |
---|
| 1137 | my $timestamp = time; |
---|
| 1138 | $attribute->{'modify_time'} = $timestamp; |
---|
| 1139 | $attribute->{'enabled'} = 'no'; |
---|
| 1140 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[345] | 1141 | print "Info: disabling IP $ip from sector $sector_current [OK]" . |
---|
[304] | 1142 | " ... Status: $attribute->{'enabled'}\n"; |
---|
| 1143 | exit; |
---|
[221] | 1144 | } |
---|
| 1145 | } |
---|
| 1146 | } |
---|
[304] | 1147 | else { # disable by Hostname |
---|
[345] | 1148 | control_exist_sector($computer_db, $sector); |
---|
| 1149 | if ( control_exist_hostname($computer_db, $sector, $hostname) == 1 ) { |
---|
| 1150 | die "Error: unkown host: $hostname, in sector: $sector [FAILED]\n"; |
---|
[221] | 1151 | } |
---|
| 1152 | |
---|
[304] | 1153 | LOOP_ON_COMPUTER: |
---|
[345] | 1154 | for my $computer (@{$computer_db->{$sector}}) { |
---|
[304] | 1155 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 1156 | |
---|
| 1157 | next LOOP_ON_COMPUTER if $attribute->{'hostname'} ne $hostname; |
---|
| 1158 | |
---|
[305] | 1159 | if ($attribute->{'address_type'} eq 'pool-dhcp') { |
---|
[345] | 1160 | die "Error: host $hostname from sector $sector belongs to a a pool [FAILED]" . |
---|
[305] | 1161 | " ... use 'disable-float' command instead"; |
---|
| 1162 | } |
---|
[304] | 1163 | |
---|
[305] | 1164 | if ($attribute->{'enabled'} eq 'no') { |
---|
[345] | 1165 | print "Info: host $hostname from sector $sector is already disable [OK]" . |
---|
[304] | 1166 | " ... Status: $attribute->{'enabled'}\n"; |
---|
| 1167 | exit; |
---|
[221] | 1168 | } |
---|
[304] | 1169 | |
---|
[305] | 1170 | my $timestamp = time; |
---|
| 1171 | $attribute->{'modify_time'} = $timestamp; |
---|
| 1172 | $attribute->{'enabled'} = 'no'; |
---|
| 1173 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[345] | 1174 | print "Info: disabling host $hostname from sector $sector [OK]" . |
---|
[305] | 1175 | " ... Status: $attribute->{'enabled'}\n"; |
---|
| 1176 | exit; |
---|
[221] | 1177 | } |
---|
| 1178 | } |
---|
| 1179 | } |
---|
| 1180 | |
---|
| 1181 | #------------------------------------------------------------------------------- |
---|
| 1182 | |
---|
| 1183 | sub disable_float { |
---|
| 1184 | my ($pool, $mac) = @_; |
---|
| 1185 | |
---|
[238] | 1186 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 1187 | |
---|
| 1188 | if ( control_exist_mac($computer_db, $mac) == 1 ) { |
---|
[332] | 1189 | die "Error: unkown physical MAC address: $mac [FAILED]\n"; |
---|
[221] | 1190 | } |
---|
| 1191 | |
---|
[345] | 1192 | for my $sector_current (keys %{$computer_db}) { |
---|
| 1193 | next if $sector_current eq 'dset'; |
---|
| 1194 | next if $sector_current eq 'pool'; |
---|
| 1195 | next if $sector_current eq 'pxe'; |
---|
| 1196 | next if $sector_current eq 'tag'; |
---|
| 1197 | next if $sector_current eq 'version'; |
---|
[221] | 1198 | |
---|
[345] | 1199 | my @sectordb = @{$computer_db->{$sector_current}}; |
---|
[221] | 1200 | |
---|
[302] | 1201 | LOOP_ON_COMPUTER: |
---|
[345] | 1202 | for my $computer (@sectordb) { |
---|
[302] | 1203 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 1204 | next LOOP_ON_COMPUTER if $mac_address ne $mac; |
---|
| 1205 | |
---|
| 1206 | if ($attribute->{'ip'} eq $pool) { |
---|
[304] | 1207 | if ($attribute->{'enabled'} eq 'no') { |
---|
[333] | 1208 | print "Info: host $mac from pool $pool is already disable [OK]" . |
---|
[304] | 1209 | " ... Status: $attribute->{'enabled'}\n"; |
---|
| 1210 | exit; |
---|
| 1211 | } |
---|
[302] | 1212 | my $timestamp = time; |
---|
| 1213 | $attribute->{'modify_time'} = $timestamp; |
---|
| 1214 | $attribute->{'enabled'} = 'no'; |
---|
| 1215 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[333] | 1216 | print "Info: disabling host $mac from pool $pool [OK]" . |
---|
[304] | 1217 | " ... Status: $attribute->{'enabled'}\n"; |
---|
[302] | 1218 | exit; |
---|
[221] | 1219 | } |
---|
[302] | 1220 | else { |
---|
[332] | 1221 | die "Error: host disable $mac [FAILED]" . |
---|
[304] | 1222 | " ... The host $mac does not belong to the $pool pool.\n"; |
---|
[302] | 1223 | } |
---|
[221] | 1224 | } |
---|
| 1225 | } |
---|
| 1226 | } |
---|
| 1227 | |
---|
| 1228 | #------------------------------------------------------------------------------- |
---|
| 1229 | #Nom: enable_pc |
---|
| 1230 | #Description: active une machine désactivée(du DHCP ou en IP statique, et du DNS) (champs enabled=non) |
---|
| 1231 | |
---|
| 1232 | sub enable_pc { |
---|
[345] | 1233 | my ($hostname, $sector, $ip) = @_; |
---|
[221] | 1234 | |
---|
[238] | 1235 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 1236 | |
---|
[345] | 1237 | control_exist_sector($computer_db, $sector) or exit; |
---|
[306] | 1238 | |
---|
| 1239 | if ($ip ne '') { # enable by IP |
---|
[357] | 1240 | control_syntax_ip($ip) or die "Error: bad IP syntax $ip\n";; |
---|
[238] | 1241 | if ( control_exist_ip($computer_db, $ip) == 1 ) { |
---|
[332] | 1242 | print "Error: unkown IP address: $ip\n"; |
---|
[221] | 1243 | exit; |
---|
| 1244 | } |
---|
| 1245 | |
---|
[345] | 1246 | for my $sector_current (keys %{$computer_db}) { |
---|
| 1247 | next if $sector_current eq 'dset'; |
---|
| 1248 | next if $sector_current eq 'pool'; |
---|
| 1249 | next if $sector_current eq 'pxe'; |
---|
| 1250 | next if $sector_current eq 'tag'; |
---|
| 1251 | next if $sector_current eq 'version'; |
---|
[221] | 1252 | |
---|
[345] | 1253 | my @sectordb = @{$computer_db->{$sector_current}}; |
---|
[306] | 1254 | |
---|
| 1255 | LOOP_ON_COMPUTER: |
---|
[345] | 1256 | for my $computer (@sectordb) { |
---|
[306] | 1257 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 1258 | if ($attribute->{'ip'} eq $ip) { |
---|
| 1259 | |
---|
| 1260 | if ($attribute->{'enabled'} eq 'yes') { |
---|
[345] | 1261 | print "Info: IP $ip belongs to sector $sector is already enable [OK]" . |
---|
[306] | 1262 | " ... Status: $attribute->{'enabled'}\n"; |
---|
[221] | 1263 | exit; |
---|
| 1264 | } |
---|
[306] | 1265 | |
---|
| 1266 | my $timestamp = time; |
---|
| 1267 | $attribute->{'modify_time'} = $timestamp; |
---|
| 1268 | $attribute->{'enabled'} = 'yes'; |
---|
| 1269 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
| 1270 | print "Info: IP $ip is now enable [OK]" . |
---|
| 1271 | " ... Status: $attribute->{'enabled'}\n"; |
---|
| 1272 | exit; |
---|
[221] | 1273 | } |
---|
| 1274 | } |
---|
| 1275 | } |
---|
| 1276 | } |
---|
[306] | 1277 | else { # enable by Hostname |
---|
[345] | 1278 | if ( control_exist_hostname($computer_db, $sector, $hostname) == 1 ) { |
---|
| 1279 | die "Error: unkown host: $hostname, in sector: $sector\n"; |
---|
[221] | 1280 | } |
---|
| 1281 | |
---|
[306] | 1282 | LOOP_ON_COMPUTER: |
---|
[345] | 1283 | for my $computer (@{$computer_db->{$sector}}) { |
---|
[306] | 1284 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 1285 | next LOOP_ON_COMPUTER if $attribute->{'hostname'} ne $hostname; |
---|
[221] | 1286 | |
---|
[306] | 1287 | if ($attribute->{'address_type'} eq 'pool-dhcp') { |
---|
[345] | 1288 | die "Error: host $hostname from sector $sector belongs to a a pool [FAILED]" . |
---|
[306] | 1289 | " ... use 'enable-float' command instead"; |
---|
[221] | 1290 | } |
---|
[306] | 1291 | |
---|
| 1292 | if ($attribute->{'enabled'} eq 'yes') { |
---|
[345] | 1293 | print "Info: host $hostname belongs to sector $sector is already enable [OK]" . |
---|
[306] | 1294 | " ... Status: $attribute->{'enabled'}\n"; |
---|
| 1295 | exit; |
---|
| 1296 | } |
---|
| 1297 | |
---|
| 1298 | my $timestamp = time; |
---|
| 1299 | $attribute->{'modify_time'} = $timestamp; |
---|
| 1300 | $attribute->{'enabled'} = 'yes'; |
---|
| 1301 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[333] | 1302 | print "Info: host $hostname is now enable [OK]" . |
---|
[306] | 1303 | " ... Status: $attribute->{'enabled'}\n"; |
---|
| 1304 | exit; |
---|
[221] | 1305 | } |
---|
| 1306 | } |
---|
| 1307 | } |
---|
| 1308 | |
---|
| 1309 | #------------------------------------------------------------------------------- |
---|
| 1310 | |
---|
| 1311 | sub enable_float { |
---|
| 1312 | my ($pool, $mac) = @_; |
---|
| 1313 | |
---|
[238] | 1314 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 1315 | |
---|
| 1316 | if ( control_exist_mac($computer_db, $mac) == 1 ) { |
---|
[332] | 1317 | die "Error: unkown physical MAC address: $mac [FAILED]\n"; |
---|
[221] | 1318 | } |
---|
| 1319 | |
---|
[345] | 1320 | for my $sector_current (keys %{$computer_db}) { |
---|
| 1321 | next if $sector_current eq 'dset'; |
---|
| 1322 | next if $sector_current eq 'pool'; |
---|
| 1323 | next if $sector_current eq 'pxe'; |
---|
| 1324 | next if $sector_current eq 'tag'; |
---|
| 1325 | next if $sector_current eq 'version'; |
---|
[221] | 1326 | |
---|
[345] | 1327 | my @sectordb = @{$computer_db->{$sector_current}}; |
---|
[221] | 1328 | |
---|
[307] | 1329 | LOOP_ON_COMPUTER: |
---|
[345] | 1330 | for my $computer (@sectordb) { |
---|
[307] | 1331 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 1332 | next LOOP_ON_COMPUTER if $mac_address ne $mac; |
---|
| 1333 | |
---|
| 1334 | if ($attribute->{'ip'} ne $pool) { |
---|
[332] | 1335 | die "Error: host enable $mac [FAILED]" . |
---|
[307] | 1336 | " ... The host $mac does not belong to the $pool pool.\n"; |
---|
[221] | 1337 | } |
---|
[307] | 1338 | |
---|
| 1339 | if ($attribute->{'enabled'} eq 'yes') { |
---|
[333] | 1340 | print "Info: host $mac from pool $pool is already enable [OK]" . |
---|
[307] | 1341 | " ... Status: $attribute->{'enabled'}\n"; |
---|
| 1342 | exit; |
---|
| 1343 | } |
---|
| 1344 | |
---|
| 1345 | my $timestamp = time; |
---|
| 1346 | $attribute->{'modify_time'} = $timestamp; |
---|
| 1347 | $attribute->{'enabled'} = 'yes'; |
---|
| 1348 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[334] | 1349 | print "Info: enabling host $mac from pool $pool [OK]" . |
---|
[307] | 1350 | " ... Status: $attribute->{'enabled'}\n"; |
---|
| 1351 | exit; |
---|
[221] | 1352 | } |
---|
| 1353 | } |
---|
| 1354 | } |
---|
| 1355 | |
---|
| 1356 | #------------------------------------------------------------------------------- |
---|
| 1357 | |
---|
| 1358 | sub cmd_enable_pc { |
---|
| 1359 | local @ARGV = @_; |
---|
| 1360 | |
---|
| 1361 | my $help = get_cmd_name(); |
---|
[345] | 1362 | my ($hostname, $sector, $ip); |
---|
[221] | 1363 | |
---|
| 1364 | GetOptions( |
---|
| 1365 | 'hostname|h=s' => \$hostname, |
---|
[346] | 1366 | 'sector|s|d=s' => \$sector, |
---|
[221] | 1367 | 'ip|i=s' => \$ip, |
---|
| 1368 | ); |
---|
| 1369 | |
---|
[345] | 1370 | ($hostname, $sector) = split /\./, $hostname, 2 if $hostname =~ m/\./; |
---|
[221] | 1371 | exit_on_error_option($help) |
---|
[345] | 1372 | if $sector eq ''; |
---|
[221] | 1373 | exit_on_error_option($help) |
---|
| 1374 | if $hostname eq '' |
---|
| 1375 | and $ip eq ''; |
---|
| 1376 | exit_on_error_option($help) |
---|
| 1377 | if $hostname ne '' |
---|
| 1378 | and $ip ne ''; |
---|
| 1379 | |
---|
[345] | 1380 | enable_pc($hostname, $sector, $ip); |
---|
[221] | 1381 | } |
---|
| 1382 | |
---|
| 1383 | #------------------------------------------------------------------------------- |
---|
| 1384 | |
---|
| 1385 | sub cmd_disable_pc { |
---|
| 1386 | local @ARGV = @_; |
---|
| 1387 | |
---|
| 1388 | my $help = get_cmd_name(); |
---|
[345] | 1389 | my ($hostname, $sector, $ip); |
---|
[221] | 1390 | |
---|
| 1391 | GetOptions( |
---|
| 1392 | 'hostname|h=s' => \$hostname, |
---|
[346] | 1393 | 'sector|s|d=s' => \$sector, |
---|
[221] | 1394 | 'ip|i=s' => \$ip, |
---|
| 1395 | ); |
---|
| 1396 | |
---|
[345] | 1397 | ($hostname, $sector) = split /\./, $hostname, 2 if $hostname =~ m/\./; |
---|
[221] | 1398 | exit_on_error_option($help) |
---|
[345] | 1399 | if $sector eq ''; |
---|
[221] | 1400 | exit_on_error_option($help) |
---|
| 1401 | if $hostname eq '' |
---|
| 1402 | and $ip eq ''; |
---|
| 1403 | exit_on_error_option($help) |
---|
| 1404 | if $hostname ne '' |
---|
| 1405 | and $ip ne ''; |
---|
| 1406 | |
---|
[345] | 1407 | disable_pc($hostname, $sector, $ip); |
---|
[221] | 1408 | } |
---|
| 1409 | |
---|
| 1410 | #------------------------------------------------------------------------------- |
---|
| 1411 | |
---|
| 1412 | sub cmd_disable_float { |
---|
| 1413 | local @ARGV = @_; |
---|
| 1414 | |
---|
| 1415 | my $help = get_cmd_name(); |
---|
| 1416 | my ($pool, $mac); |
---|
| 1417 | |
---|
| 1418 | GetOptions( |
---|
| 1419 | 'pool|p=s' => \$pool, |
---|
| 1420 | 'mac|m=s' => \$mac, |
---|
| 1421 | ); |
---|
| 1422 | |
---|
[239] | 1423 | ($pool) = split /\./, $pool, 2 if $pool =~ m/\./; |
---|
[221] | 1424 | exit_on_error_option($help) |
---|
| 1425 | if $pool eq '' |
---|
| 1426 | or $mac eq ''; |
---|
| 1427 | |
---|
| 1428 | disable_float($pool, $mac); |
---|
| 1429 | } |
---|
| 1430 | |
---|
| 1431 | #------------------------------------------------------------------------------- |
---|
| 1432 | |
---|
| 1433 | sub cmd_enable_float { |
---|
| 1434 | local @ARGV = @_; |
---|
| 1435 | |
---|
| 1436 | my $help = get_cmd_name(); |
---|
| 1437 | my ($pool, $mac); |
---|
| 1438 | |
---|
| 1439 | GetOptions( |
---|
| 1440 | 'pool|p=s' => \$pool, |
---|
| 1441 | 'mac|m=s' => \$mac, |
---|
| 1442 | ); |
---|
| 1443 | |
---|
[239] | 1444 | ($pool) = split /\./, $pool, 2 if $pool =~ m/\./; |
---|
[221] | 1445 | exit_on_error_option($help) |
---|
| 1446 | if $pool eq '' |
---|
| 1447 | or $mac eq ''; |
---|
| 1448 | |
---|
| 1449 | enable_float($pool, $mac); |
---|
| 1450 | } |
---|
| 1451 | |
---|
| 1452 | #------------------------------------------------------------------------------- |
---|
| 1453 | # DELETE section |
---|
| 1454 | #------------------------------------------------------------------------------- |
---|
| 1455 | |
---|
| 1456 | #------------------------------------------------------------------------------- |
---|
| 1457 | #Nom: del_pc |
---|
| 1458 | #Description: supprime une machine en DHCP ou en IP statique. |
---|
| 1459 | |
---|
| 1460 | sub del_pc { |
---|
[345] | 1461 | my ($hostname, $sector, $ip) = @_; |
---|
[221] | 1462 | |
---|
[238] | 1463 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 1464 | |
---|
[345] | 1465 | control_exist_sector($computer_db, $sector) or exit; |
---|
[314] | 1466 | if ($ip ne '') { # delete by IP |
---|
[238] | 1467 | if ( control_exist_ip($computer_db, $ip) == 1 ) { |
---|
[332] | 1468 | die "Error: unkown IP address: $ip\n"; |
---|
[221] | 1469 | } |
---|
[314] | 1470 | |
---|
| 1471 | my $computer_index = 0; |
---|
| 1472 | |
---|
| 1473 | LOOP_ON_COMPUTER: |
---|
[345] | 1474 | for my $computer (@{$computer_db->{$sector}}) { |
---|
[314] | 1475 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 1476 | |
---|
[329] | 1477 | $computer_index++, next LOOP_ON_COMPUTER if $attribute->{'ip'} ne $ip; |
---|
[314] | 1478 | |
---|
[345] | 1479 | splice(@{$computer_db->{$sector}}, $computer_index => 1); |
---|
[314] | 1480 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[345] | 1481 | print "Info: host $ip has been removed from the sector $sector [OK]\n"; |
---|
[314] | 1482 | exit; |
---|
[221] | 1483 | } |
---|
| 1484 | } |
---|
| 1485 | else { |
---|
[345] | 1486 | if ( control_exist_hostname($computer_db, $sector, $hostname) == 1 ) { |
---|
| 1487 | die "Error: unkown host: $hostname, in sector: $sector\n"; |
---|
[221] | 1488 | } |
---|
[314] | 1489 | |
---|
| 1490 | my $computer_index = 0; |
---|
| 1491 | |
---|
| 1492 | LOOP_ON_COMPUTER: |
---|
[345] | 1493 | for my $computer (@{$computer_db->{$sector}}) { |
---|
[314] | 1494 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 1495 | |
---|
[329] | 1496 | $computer_index++, next LOOP_ON_COMPUTER if $attribute->{'hostname'} ne $hostname; |
---|
[314] | 1497 | |
---|
[315] | 1498 | if ($attribute->{'address_type'} eq 'pool-dhcp') { |
---|
[345] | 1499 | die "Error: host remove $hostname from the sector $sector [FAILED]" . |
---|
[315] | 1500 | " ... The host $hostname belongs to a DHCP pool.\n"; |
---|
| 1501 | } |
---|
[221] | 1502 | |
---|
[345] | 1503 | splice(@{$computer_db->{$sector}}, $computer_index => 1); |
---|
[315] | 1504 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[345] | 1505 | print "Info: host $hostname has been removed from the sector $sector [OK]\n"; |
---|
[315] | 1506 | exit; |
---|
[221] | 1507 | } |
---|
| 1508 | } |
---|
| 1509 | } |
---|
| 1510 | |
---|
| 1511 | #------------------------------------------------------------------------------- |
---|
| 1512 | #Nom: del_float |
---|
| 1513 | #Description: supprime une machine d'un pool DHCP |
---|
| 1514 | |
---|
| 1515 | sub del_float { |
---|
| 1516 | my ($pool, $mac) = @_; |
---|
| 1517 | |
---|
[238] | 1518 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 1519 | |
---|
| 1520 | if ( control_exist_mac($computer_db, $mac) == 1 ) { |
---|
[221] | 1521 | print "Adresse MAC $mac non trouvée.\n"; |
---|
| 1522 | exit; |
---|
| 1523 | } |
---|
| 1524 | |
---|
[345] | 1525 | for my $sector_current (keys %{$computer_db}) { |
---|
| 1526 | next if $sector_current eq 'dset'; |
---|
| 1527 | next if $sector_current eq 'pool'; |
---|
| 1528 | next if $sector_current eq 'pxe'; |
---|
| 1529 | next if $sector_current eq 'tag'; |
---|
| 1530 | next if $sector_current eq 'version'; |
---|
[221] | 1531 | |
---|
[345] | 1532 | my @sectordb = @{$computer_db->{$sector_current}}; |
---|
[221] | 1533 | |
---|
[306] | 1534 | my $computer_index = 0; |
---|
[221] | 1535 | |
---|
[306] | 1536 | LOOP_ON_COMPUTER: |
---|
[345] | 1537 | for my $computer (@sectordb) { |
---|
[306] | 1538 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 1539 | |
---|
[329] | 1540 | $computer_index++, next LOOP_ON_COMPUTER if $mac_address ne $mac; |
---|
[306] | 1541 | |
---|
| 1542 | if ($attribute->{'ip'} ne $pool) { |
---|
[332] | 1543 | die "Error: host remove $mac [FAILED]" . |
---|
[306] | 1544 | " ... The host $mac does not belong to the $pool pool.\n"; |
---|
[221] | 1545 | } |
---|
[306] | 1546 | |
---|
[345] | 1547 | splice(@{$computer_db->{$sector_current}}, $computer_index => 1); |
---|
[306] | 1548 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
| 1549 | print "Info: remove host $mac from the pool $pool [OK]\n"; |
---|
| 1550 | exit; |
---|
[221] | 1551 | } |
---|
| 1552 | } |
---|
| 1553 | } |
---|
| 1554 | |
---|
| 1555 | #------------------------------------------------------------------------------- |
---|
| 1556 | |
---|
| 1557 | sub cmd_del_pc { |
---|
| 1558 | local @ARGV = @_; |
---|
| 1559 | |
---|
| 1560 | my $help = get_cmd_name(); |
---|
[345] | 1561 | my ($hostname, $sector, $ip); |
---|
[221] | 1562 | |
---|
| 1563 | GetOptions( |
---|
| 1564 | 'hostname|h=s' => \$hostname, |
---|
[346] | 1565 | 'sector|s|d=s' => \$sector, |
---|
[221] | 1566 | 'ip|i=s' => \$ip, |
---|
| 1567 | ); |
---|
| 1568 | |
---|
[345] | 1569 | ($hostname, $sector) = split /\./, $hostname, 2 if $hostname =~ m/\./; |
---|
[221] | 1570 | exit_on_error_option($help) |
---|
[345] | 1571 | if $sector eq ''; |
---|
[221] | 1572 | exit_on_error_option($help) |
---|
| 1573 | if $hostname eq '' |
---|
| 1574 | and $ip eq ''; |
---|
| 1575 | exit_on_error_option($help) |
---|
| 1576 | if $hostname ne '' |
---|
| 1577 | and $ip ne ''; |
---|
| 1578 | |
---|
[345] | 1579 | del_pc($hostname, $sector, $ip); |
---|
[221] | 1580 | } |
---|
| 1581 | |
---|
| 1582 | #------------------------------------------------------------------------------- |
---|
| 1583 | |
---|
| 1584 | sub cmd_del_float { |
---|
| 1585 | local @ARGV = @_; |
---|
| 1586 | |
---|
| 1587 | my $help = get_cmd_name(); |
---|
| 1588 | my ($pool, $mac); |
---|
| 1589 | |
---|
| 1590 | GetOptions( |
---|
| 1591 | 'pool|p=s' => \$pool, |
---|
| 1592 | 'mac|m=s' => \$mac, |
---|
| 1593 | ); |
---|
| 1594 | |
---|
[239] | 1595 | ($pool) = split /\./, $pool, 2 if $pool =~ m/\./; |
---|
[221] | 1596 | exit_on_error_option($help) |
---|
| 1597 | if $pool eq '' |
---|
| 1598 | or $mac eq ''; |
---|
| 1599 | |
---|
| 1600 | del_float($pool, $mac); |
---|
| 1601 | } |
---|
| 1602 | |
---|
| 1603 | #------------------------------------------------------------------------------- |
---|
[345] | 1604 | # SECTOR section |
---|
[221] | 1605 | #------------------------------------------------------------------------------- |
---|
| 1606 | |
---|
[345] | 1607 | sub cmd_create_sector { |
---|
[221] | 1608 | local @ARGV = @_; |
---|
| 1609 | |
---|
| 1610 | my $help = get_cmd_name(); |
---|
[345] | 1611 | my ($sector, $dns_extension, $comment); |
---|
[221] | 1612 | |
---|
| 1613 | GetOptions( |
---|
[352] | 1614 | 'sector|s|d=s' => \$sector, |
---|
[221] | 1615 | 'dns-extension|e=s' => \$dns_extension, |
---|
| 1616 | 'comment|c=s' => \$comment, |
---|
| 1617 | ); |
---|
| 1618 | |
---|
| 1619 | exit_on_error_option($help) |
---|
[353] | 1620 | if $sector eq '' |
---|
[221] | 1621 | or $dns_extension eq '' |
---|
| 1622 | or $comment eq ''; |
---|
| 1623 | |
---|
[358] | 1624 | $sector =~ m{^[\w\d]} or die "Error: sector $sector must begin by a letter or a digit\n"; |
---|
| 1625 | $sector =~ m{^[\w\d][\w\d-_]*$} or die "Error: sector $sector must only use letter digit dash and underscore character\n"; |
---|
| 1626 | |
---|
[221] | 1627 | $comment = normalize_comment($comment); |
---|
| 1628 | |
---|
[238] | 1629 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
[221] | 1630 | |
---|
[238] | 1631 | $computer_db->{'dset'} ||= {}; |
---|
[345] | 1632 | die "Error: sector already exists: $sector\n" if exists $computer_db->{'dset'}{$sector}; |
---|
[238] | 1633 | |
---|
[221] | 1634 | control_syntax_comment($comment) or exit; |
---|
| 1635 | |
---|
| 1636 | my $timestamp = time; |
---|
[345] | 1637 | $computer_db->{'dset'}{$sector} = { |
---|
[221] | 1638 | 'dns_extension' => $dns_extension, |
---|
| 1639 | 'comment' => $comment, |
---|
| 1640 | 'create_time' => $timestamp, |
---|
| 1641 | 'modify_time' => $timestamp, |
---|
| 1642 | }; |
---|
[345] | 1643 | $computer_db->{$sector} ||= []; # Create empty sector computer list by default |
---|
[238] | 1644 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[221] | 1645 | } |
---|
| 1646 | |
---|
| 1647 | #------------------------------------------------------------------------------- |
---|
[353] | 1648 | |
---|
| 1649 | sub cmd_sector_add_ip { |
---|
| 1650 | local @ARGV = @_; |
---|
| 1651 | |
---|
| 1652 | my $help = get_cmd_name(); |
---|
| 1653 | my ($sector, $ip_range); |
---|
| 1654 | |
---|
| 1655 | GetOptions( |
---|
| 1656 | 'sector|s|d=s' => \$sector, |
---|
| 1657 | 'ip-range|i=s' => \$ip_range, |
---|
| 1658 | ); |
---|
| 1659 | |
---|
| 1660 | exit_on_error_option($help) |
---|
| 1661 | if $sector eq '' |
---|
| 1662 | or $ip_range eq ''; |
---|
| 1663 | |
---|
| 1664 | control_syntax_cidr($ip_range) or die "Error: bad IP range $ip_range syntax (CIDR)\n";; |
---|
| 1665 | |
---|
| 1666 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 1667 | exists $computer_db->{'dset'}{$sector} or die "Error: sector not exists: $sector\n"; |
---|
| 1668 | |
---|
| 1669 | my $timestamp = time; |
---|
[361] | 1670 | $computer_db->{'dset'}{$sector}{'ip_range'} ||= []; |
---|
[353] | 1671 | LOOP_ON_CIDR: |
---|
[361] | 1672 | for my $cidr_current (@{$computer_db->{'dset'}{$sector}{'ip_range'}}) { |
---|
[353] | 1673 | next LOOP_ON_CIDR if $cidr_current ne $ip_range; |
---|
| 1674 | |
---|
| 1675 | die "Error: IP range $ip_range already in sector $sector\n"; |
---|
| 1676 | } |
---|
| 1677 | |
---|
| 1678 | my $timestamp = time; |
---|
[361] | 1679 | push @{$computer_db->{'dset'}{$sector}{'ip_range'}}, $ip_range; |
---|
[353] | 1680 | $computer_db->{'dset'}{$sector}{'modify_time'} = $timestamp; |
---|
| 1681 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
| 1682 | } |
---|
| 1683 | |
---|
| 1684 | #------------------------------------------------------------------------------- |
---|
[221] | 1685 | # POOL section |
---|
| 1686 | #------------------------------------------------------------------------------- |
---|
| 1687 | |
---|
| 1688 | #------------------------------------------------------------------------------- |
---|
| 1689 | #Nom: create_pool |
---|
| 1690 | #Description: crée un pool dans le fichier de données YAML et dans le DHCP. |
---|
| 1691 | # |
---|
| 1692 | #Commentaires: il y a un petit bug si jamais on rentre que des adresses ip qui existent déjà. |
---|
| 1693 | # Le pool est créé mais sans adresses ip. |
---|
| 1694 | |
---|
| 1695 | sub cmd_create_pool { |
---|
| 1696 | local @ARGV = @_; |
---|
| 1697 | |
---|
| 1698 | my $help = get_cmd_name(); |
---|
[345] | 1699 | my ($pool, $sector, $file_pool, $ipaddress_pool); |
---|
[221] | 1700 | |
---|
| 1701 | GetOptions( |
---|
| 1702 | 'pool|p=s' => \$pool, |
---|
[346] | 1703 | 'sector|s|d=s' => \$sector, |
---|
[221] | 1704 | 'file-pool|f=s' => \$file_pool, |
---|
| 1705 | 'ipaddress-pool|i=s' => \$ipaddress_pool, |
---|
| 1706 | ); |
---|
| 1707 | |
---|
| 1708 | exit_on_error_option($help) |
---|
| 1709 | if $pool eq '' |
---|
[345] | 1710 | or $sector eq '' |
---|
[221] | 1711 | or $file_pool eq '' |
---|
| 1712 | or $ipaddress_pool eq ''; |
---|
| 1713 | |
---|
[238] | 1714 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 1715 | |
---|
| 1716 | if ($computer_db->{'pool'}) { |
---|
[332] | 1717 | die "Error: pool already exists: $pool\n" if exists $computer_db->{'pool'}{$pool}; |
---|
[221] | 1718 | } |
---|
| 1719 | |
---|
| 1720 | #--- control if the domain's pool exist ---# |
---|
[345] | 1721 | control_exist_sector($computer_db, $sector) or exit; |
---|
[221] | 1722 | |
---|
| 1723 | my @ip_list = (); |
---|
| 1724 | #---control if address exist ---# |
---|
| 1725 | if ($ipaddress_pool =~ /,/) { |
---|
[357] | 1726 | LOOP_ON_IP: |
---|
[221] | 1727 | for my $ip (split /,/, $ipaddress_pool) { |
---|
| 1728 | if ($ip =~ /-/) { |
---|
| 1729 | my ($ip1, $ip2, $ip3, $range) = split /\./, $ip; |
---|
| 1730 | my ($first, $last) = split /-/, $range; |
---|
| 1731 | for (my $cpt = $first; $cpt <= $last; $cpt++) { |
---|
| 1732 | my $ip_loc = "$ip1.$ip2.$ip3.$cpt"; |
---|
[332] | 1733 | control_syntax_ip($ip_loc) or die "Error: bad IP syntax: $ip_loc\n"; |
---|
[238] | 1734 | control_exist_ip($computer_db, $ip_loc) or die "Error: IP address already exists: $ip_loc\n"; |
---|
[221] | 1735 | push @ip_list, $ip_loc; |
---|
| 1736 | } |
---|
| 1737 | } |
---|
| 1738 | else { |
---|
[357] | 1739 | control_syntax_ip($ip) or next LOOP_ON_IP; |
---|
[238] | 1740 | if ( control_exist_ip($computer_db, $ip) == 0 ) { |
---|
[221] | 1741 | print "L'adresse IP $ip existe déjà\n"; |
---|
| 1742 | next; |
---|
| 1743 | } |
---|
| 1744 | push @ip_list, $ip; |
---|
| 1745 | } |
---|
| 1746 | } |
---|
| 1747 | } |
---|
| 1748 | |
---|
| 1749 | my $timestamp = time; |
---|
[238] | 1750 | $computer_db->{'pool'}{$pool} = { |
---|
[221] | 1751 | 'ip' => [@ip_list], |
---|
| 1752 | 'enabled' => 'yes', |
---|
| 1753 | 'create_time' => $timestamp, |
---|
| 1754 | 'modify_time' => $timestamp, |
---|
| 1755 | 'file' => $file_pool, |
---|
[345] | 1756 | 'domain' => $sector, |
---|
[221] | 1757 | }; |
---|
[238] | 1758 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[221] | 1759 | } |
---|
| 1760 | |
---|
| 1761 | #------------------------------------------------------------------------------- |
---|
| 1762 | |
---|
| 1763 | sub cmd_show_pool { |
---|
| 1764 | local @ARGV = @_; |
---|
| 1765 | |
---|
| 1766 | my ($no_header); |
---|
| 1767 | |
---|
| 1768 | GetOptions( |
---|
| 1769 | 'no-header|H' => \$no_header, |
---|
| 1770 | ); |
---|
| 1771 | |
---|
[238] | 1772 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 1773 | |
---|
[221] | 1774 | printf "%-17s %-17s %s\n", 'Pool', 'File', 'DNS-Domain' if not $no_header; |
---|
| 1775 | LOOP_ON_PXE: |
---|
[238] | 1776 | for my $pool ( keys %{$computer_db->{'pool'}} ) { |
---|
[227] | 1777 | |
---|
[221] | 1778 | printf "%-17s %-17s %s\n", |
---|
| 1779 | $pool, |
---|
[238] | 1780 | $computer_db->{'pool'}{$pool}{'file'}, |
---|
| 1781 | $computer_db->{'pool'}{$pool}{'domain'}, |
---|
[221] | 1782 | } |
---|
| 1783 | } |
---|
| 1784 | |
---|
| 1785 | #------------------------------------------------------------------------------- |
---|
| 1786 | # PXE section |
---|
| 1787 | #------------------------------------------------------------------------------- |
---|
| 1788 | |
---|
| 1789 | sub cmd_create_pxe { |
---|
| 1790 | local @ARGV = @_; |
---|
| 1791 | |
---|
| 1792 | my $help = get_cmd_name(); |
---|
| 1793 | my ($pxe_config, $ip_next_server, $filename, $comment); |
---|
| 1794 | |
---|
| 1795 | GetOptions( |
---|
| 1796 | 'bootp|b=s' => \$pxe_config, |
---|
| 1797 | 'next-server|n=s' => \$ip_next_server, |
---|
| 1798 | 'filename|f=s' => \$filename, |
---|
| 1799 | 'comment|c=s' => \$comment, |
---|
| 1800 | ); |
---|
| 1801 | |
---|
| 1802 | exit_on_error_option($help) |
---|
| 1803 | if $pxe_config eq '' |
---|
| 1804 | or $ip_next_server eq '' |
---|
| 1805 | or $filename eq '' |
---|
| 1806 | or $comment eq ''; |
---|
| 1807 | |
---|
[238] | 1808 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 1809 | |
---|
[221] | 1810 | $comment = normalize_comment($comment); |
---|
| 1811 | |
---|
[238] | 1812 | $computer_db->{'pxe'} ||= {}; |
---|
| 1813 | die "Error: PXE config already exists: $pxe_config\n" if exists $computer_db->{'pxe'}{$pxe_config}; |
---|
[221] | 1814 | |
---|
[332] | 1815 | control_syntax_ip($ip_next_server) or die "Error: bad IP syntax: $ip_next_server\n"; |
---|
[221] | 1816 | control_syntax_comment($comment) or exit; |
---|
| 1817 | |
---|
| 1818 | my $timestamp = time; |
---|
[238] | 1819 | $computer_db->{'pxe'}{$pxe_config} = { |
---|
[221] | 1820 | 'ip_next_server' => $ip_next_server, |
---|
| 1821 | 'filename' => $filename, |
---|
| 1822 | 'comment' => $comment, |
---|
| 1823 | 'create_time' => $timestamp, |
---|
| 1824 | 'modify_time' => $timestamp, |
---|
| 1825 | }; |
---|
[238] | 1826 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[221] | 1827 | } |
---|
| 1828 | |
---|
| 1829 | #------------------------------------------------------------------------------- |
---|
| 1830 | |
---|
| 1831 | sub cmd_remove_pxe { |
---|
| 1832 | local @ARGV = @_; |
---|
| 1833 | |
---|
| 1834 | my $help = get_cmd_name(); |
---|
| 1835 | my ($pxe_config); |
---|
| 1836 | |
---|
| 1837 | GetOptions( |
---|
| 1838 | 'bootp|b=s' => \$pxe_config, |
---|
| 1839 | ); |
---|
| 1840 | |
---|
| 1841 | exit_on_error_option($help) |
---|
| 1842 | if $pxe_config eq ''; |
---|
| 1843 | |
---|
[238] | 1844 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
[221] | 1845 | |
---|
[238] | 1846 | $computer_db->{'pxe'} ||= {}; |
---|
| 1847 | die "Error: PXE config does not exist: $pxe_config\n" if not exists $computer_db->{'pxe'}{$pxe_config}; |
---|
| 1848 | |
---|
[221] | 1849 | # Test if some computer use this config |
---|
[345] | 1850 | LOOP_ON_SECTOR: |
---|
| 1851 | for my $sector_current (keys %{$computer_db}) { |
---|
| 1852 | next if $sector_current eq 'dset'; |
---|
| 1853 | next if $sector_current eq 'pool'; |
---|
| 1854 | next if $sector_current eq 'pxe'; |
---|
| 1855 | next if $sector_current eq 'tag'; |
---|
| 1856 | next if $sector_current eq 'version'; |
---|
[221] | 1857 | |
---|
| 1858 | LOOP_ON_COMPUTER: |
---|
[345] | 1859 | for my $computer (@{$computer_db->{$sector_current}}) { |
---|
[221] | 1860 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 1861 | |
---|
| 1862 | if (exists $attribute->{'pxe_config'}) { |
---|
| 1863 | my $hostname = $attribute->{'hostname'}; |
---|
[345] | 1864 | die "Error: computer still use this PXE config: $hostname.$sector_current $mac_address\n" if $pxe_config eq $attribute->{'pxe_config'}; |
---|
[221] | 1865 | } |
---|
| 1866 | } |
---|
| 1867 | } |
---|
| 1868 | |
---|
[238] | 1869 | delete $computer_db->{'pxe'}{$pxe_config}; |
---|
| 1870 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[221] | 1871 | } |
---|
| 1872 | |
---|
| 1873 | #-------------------------------------------------------------------------------- |
---|
| 1874 | |
---|
| 1875 | sub cmd_show_pxe { |
---|
| 1876 | local @ARGV = @_; |
---|
| 1877 | |
---|
| 1878 | my ($no_header); |
---|
| 1879 | |
---|
| 1880 | GetOptions( |
---|
| 1881 | 'no-header|H' => \$no_header, |
---|
| 1882 | ); |
---|
| 1883 | |
---|
[238] | 1884 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 1885 | |
---|
[221] | 1886 | printf "%-12s %-13s %-30s %s\n", 'PXE-Config', 'Next-Server', 'Filename', 'Comment' if not $no_header; |
---|
| 1887 | LOOP_ON_PXE: |
---|
[238] | 1888 | for my $pxe_config ( keys %{$computer_db->{'pxe'}} ) { |
---|
| 1889 | my $ip_next_server = $computer_db->{'pxe'}{$pxe_config}{'ip_next_server'}; |
---|
| 1890 | my $filename = $computer_db->{'pxe'}{$pxe_config}{'filename'}; |
---|
| 1891 | my $comment = $computer_db->{'pxe'}{$pxe_config}{'comment'}; |
---|
[227] | 1892 | |
---|
[221] | 1893 | printf "%-12s %-13s %-30s %s\n", $pxe_config, $ip_next_server, $filename, $comment; |
---|
| 1894 | } |
---|
| 1895 | } |
---|
| 1896 | |
---|
| 1897 | #------------------------------------------------------------------------------- |
---|
| 1898 | |
---|
| 1899 | sub cmd_enable_pxe { |
---|
| 1900 | local @ARGV = @_; |
---|
| 1901 | |
---|
| 1902 | my $help = get_cmd_name(); |
---|
[345] | 1903 | my ($hostname, $sector, $ip, $pxe_config); |
---|
[221] | 1904 | |
---|
| 1905 | GetOptions( |
---|
| 1906 | 'hostname|h=s' => \$hostname, |
---|
[346] | 1907 | 'sector|s|d=s' => \$sector, |
---|
[221] | 1908 | 'ip|i=s' => \$ip, |
---|
| 1909 | 'bootp|b=s' => \$pxe_config, |
---|
| 1910 | ); |
---|
| 1911 | |
---|
[345] | 1912 | ($hostname, $sector) = split /\./, $hostname, 2 if $hostname =~ m/\./; |
---|
[221] | 1913 | exit_on_error_option($help) |
---|
[345] | 1914 | if $sector eq '' |
---|
[221] | 1915 | or $pxe_config eq ''; |
---|
| 1916 | exit_on_error_option($help) |
---|
| 1917 | if $hostname eq '' |
---|
| 1918 | and $ip eq ''; |
---|
| 1919 | exit_on_error_option($help) |
---|
| 1920 | if $hostname ne '' |
---|
| 1921 | and $ip ne ''; |
---|
| 1922 | |
---|
[238] | 1923 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
[221] | 1924 | |
---|
[238] | 1925 | die "Error: PXE config not exists: $pxe_config\n" if not exists $computer_db->{'pxe'}{$pxe_config}; |
---|
| 1926 | |
---|
[345] | 1927 | control_exist_sector($computer_db, $sector) or exit; |
---|
[221] | 1928 | if ($ip ne '') { |
---|
[357] | 1929 | control_syntax_ip($ip) or die "Error: bad IP syntax $ip\n";; |
---|
[238] | 1930 | if ( control_exist_ip($computer_db, $ip) == 1 ) { |
---|
[332] | 1931 | die "Error: unkown IP address: $ip\n"; |
---|
[221] | 1932 | } |
---|
| 1933 | |
---|
[345] | 1934 | for my $sector_current (keys %{$computer_db}) { |
---|
| 1935 | next if $sector_current eq 'dset'; |
---|
| 1936 | next if $sector_current eq 'pool'; |
---|
| 1937 | next if $sector_current eq 'pxe'; |
---|
| 1938 | next if $sector_current eq 'tag'; |
---|
| 1939 | next if $sector_current eq 'version'; |
---|
[221] | 1940 | |
---|
[333] | 1941 | LOOP_ON_COMPUTER: |
---|
[345] | 1942 | for my $computer (@{$computer_db->{$sector_current}}) { |
---|
[333] | 1943 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 1944 | next LOOP_ON_COMPUTER if $attribute->{'ip'} ne $ip; |
---|
| 1945 | |
---|
| 1946 | $attribute->{'modify_time'} = time; |
---|
| 1947 | $attribute->{'pxe_config'} = $pxe_config; |
---|
| 1948 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[345] | 1949 | print "Info: host $attribute->{'hostname'} ($sector_current), IP $ip, PXE enabled: $pxe_config\n"; |
---|
[333] | 1950 | exit; |
---|
[221] | 1951 | } |
---|
| 1952 | } |
---|
| 1953 | } |
---|
| 1954 | else { |
---|
[345] | 1955 | if ( control_exist_hostname($computer_db, $sector, $hostname) == 1 ) { |
---|
| 1956 | die "Error: unkown host: $hostname, in sector: $sector\n"; |
---|
[221] | 1957 | } |
---|
| 1958 | |
---|
[314] | 1959 | LOOP_ON_COMPUTER: |
---|
[345] | 1960 | for my $computer (@{$computer_db->{$sector}}) { |
---|
[314] | 1961 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 1962 | next LOOP_ON_COMPUTER if $attribute->{'hostname'} ne $hostname; |
---|
| 1963 | |
---|
| 1964 | if ($attribute->{'address_type'} eq 'pool-dhcp') { |
---|
[345] | 1965 | die "Error. Host $hostname ($sector) in a pool. No PXE possible [FAILED]\n"; |
---|
[314] | 1966 | } |
---|
[221] | 1967 | |
---|
[314] | 1968 | $attribute->{'modify_time'} = time; |
---|
| 1969 | $attribute->{'pxe_config'} = $pxe_config; |
---|
| 1970 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[345] | 1971 | print "Info: host $hostname ($sector), IP $attribute->{'ip'}, PXE enabled: $pxe_config [OK]\n"; |
---|
[314] | 1972 | exit; |
---|
[221] | 1973 | } |
---|
| 1974 | } |
---|
| 1975 | } |
---|
| 1976 | |
---|
| 1977 | #------------------------------------------------------------------------------- |
---|
| 1978 | |
---|
| 1979 | sub cmd_disable_pxe { |
---|
| 1980 | local @ARGV = @_; |
---|
| 1981 | |
---|
| 1982 | my $help = get_cmd_name(); |
---|
[345] | 1983 | my ($hostname, $sector, $ip); |
---|
[221] | 1984 | |
---|
| 1985 | GetOptions( |
---|
| 1986 | 'hostname|h=s' => \$hostname, |
---|
[346] | 1987 | 'sector|s|d=s' => \$sector, |
---|
[221] | 1988 | 'ip|i=s' => \$ip, |
---|
| 1989 | ); |
---|
| 1990 | |
---|
[345] | 1991 | ($hostname, $sector) = split /\./, $hostname, 2 if $hostname =~ m/\./; |
---|
[221] | 1992 | exit_on_error_option($help) |
---|
[345] | 1993 | if $sector eq ''; |
---|
[221] | 1994 | exit_on_error_option($help) |
---|
| 1995 | if $hostname eq '' |
---|
| 1996 | and $ip eq ''; |
---|
| 1997 | exit_on_error_option($help) |
---|
| 1998 | if $hostname ne '' |
---|
| 1999 | and $ip ne ''; |
---|
| 2000 | |
---|
[238] | 2001 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 2002 | |
---|
[345] | 2003 | control_exist_sector($computer_db, $sector) or exit; |
---|
[221] | 2004 | if ($ip ne '') { |
---|
[357] | 2005 | control_syntax_ip($ip) or die "Error: bad IP syntax $ip\n";; |
---|
[238] | 2006 | if ( control_exist_ip($computer_db, $ip) == 1 ) { |
---|
[332] | 2007 | die "Error: unkown IP address: $ip\n"; |
---|
[221] | 2008 | } |
---|
| 2009 | |
---|
[345] | 2010 | for my $sector_current (keys %{$computer_db}) { |
---|
| 2011 | next if $sector_current eq 'dset'; |
---|
| 2012 | next if $sector_current eq 'pool'; |
---|
| 2013 | next if $sector_current eq 'pxe'; |
---|
| 2014 | next if $sector_current eq 'tag'; |
---|
| 2015 | next if $sector_current eq 'version'; |
---|
[221] | 2016 | |
---|
[314] | 2017 | LOOP_ON_COMPUTER: |
---|
[345] | 2018 | for my $computer (@{$computer_db->{$sector_current}}) { |
---|
[314] | 2019 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 2020 | |
---|
| 2021 | next LOOP_ON_COMPUTER if $attribute->{'ip'} ne $ip; |
---|
| 2022 | next LOOP_ON_COMPUTER if not exists $attribute->{'pxe_config'}; |
---|
[227] | 2023 | |
---|
[314] | 2024 | my $pxe_config = $attribute->{'pxe_config'}; |
---|
| 2025 | $attribute->{'modify_time'} = time; |
---|
| 2026 | delete $attribute->{'pxe_config'}; |
---|
| 2027 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[332] | 2028 | print "Info: IP address: $ip, PXE disable from config: $pxe_config [OK]\n"; |
---|
[314] | 2029 | exit; |
---|
[221] | 2030 | } |
---|
| 2031 | } |
---|
| 2032 | } |
---|
| 2033 | else { |
---|
[345] | 2034 | if ( control_exist_hostname($computer_db, $sector, $hostname) == 1 ) { |
---|
| 2035 | die "Error: unkown host: $hostname, in sector: $sector\n"; |
---|
[221] | 2036 | } |
---|
| 2037 | |
---|
[314] | 2038 | LOOP_ON_COMPUTER: |
---|
[345] | 2039 | for my $computer (@{$computer_db->{$sector}}) { |
---|
[314] | 2040 | my ($mac_address, $attribute) = %{$computer}; |
---|
[227] | 2041 | |
---|
[314] | 2042 | next LOOP_ON_COMPUTER if $attribute->{'hostname'} eq $hostname; |
---|
[221] | 2043 | |
---|
[314] | 2044 | if ($attribute->{'address_type'} eq 'pool-dhcp') { |
---|
[345] | 2045 | die "Error: host $hostname ($sector) in a pool. No PXE possible [FAILED]\n"; |
---|
[221] | 2046 | } |
---|
[314] | 2047 | |
---|
| 2048 | next LOOP_ON_COMPUTER if not exists $attribute->{'pxe_config'}; |
---|
| 2049 | |
---|
| 2050 | my $pxe_config = $attribute->{'pxe_config'}; |
---|
| 2051 | $attribute->{'modify_time'} = time; |
---|
| 2052 | delete $attribute->{'pxe_config'}; |
---|
| 2053 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[345] | 2054 | print "Info: host $hostname ($sector), PXE disable from config: $pxe_config [OK]\n"; |
---|
[314] | 2055 | exit; |
---|
[221] | 2056 | } |
---|
| 2057 | } |
---|
| 2058 | } |
---|
| 2059 | |
---|
| 2060 | #------------------------------------------------------------------------------- |
---|
| 2061 | # TAG section |
---|
| 2062 | #------------------------------------------------------------------------------- |
---|
| 2063 | |
---|
| 2064 | sub cmd_create_tag { |
---|
| 2065 | local @ARGV = @_; |
---|
| 2066 | |
---|
| 2067 | my $help = get_cmd_name(); |
---|
| 2068 | my ($tag, $comment); |
---|
| 2069 | |
---|
| 2070 | GetOptions( |
---|
| 2071 | 'tag|t=s' => \$tag, |
---|
| 2072 | 'comment|c=s' => \$comment, |
---|
| 2073 | ); |
---|
| 2074 | |
---|
| 2075 | exit_on_error_option($help) |
---|
| 2076 | if $tag eq '' |
---|
| 2077 | or $comment eq ''; |
---|
| 2078 | |
---|
[238] | 2079 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 2080 | |
---|
[221] | 2081 | $comment = normalize_comment($comment); |
---|
| 2082 | |
---|
[238] | 2083 | $computer_db->{'tag'} ||= {}; |
---|
| 2084 | die "Error: TAG already exists: $tag\n" if exists $computer_db->{'tag'}{$tag}; |
---|
[221] | 2085 | |
---|
| 2086 | die "Error: TAG 'universal' is intrinsic. It's not possible to create it.\n" if $tag eq 'universal'; |
---|
| 2087 | |
---|
| 2088 | if ($tag !~ m/^ \w+ $/xms) { |
---|
[332] | 2089 | die "Error: bad format for TAG (alphanumeric string): $tag\n"; |
---|
[221] | 2090 | } |
---|
| 2091 | |
---|
| 2092 | control_syntax_comment($comment) or exit; |
---|
| 2093 | |
---|
| 2094 | my $timestamp = time; |
---|
[238] | 2095 | $computer_db->{'tag'}{$tag} = { |
---|
[221] | 2096 | 'comment' => $comment, |
---|
| 2097 | 'create_time' => $timestamp, |
---|
| 2098 | 'modify_time' => $timestamp, |
---|
| 2099 | }; |
---|
[238] | 2100 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[221] | 2101 | } |
---|
| 2102 | |
---|
| 2103 | #------------------------------------------------------------------------------- |
---|
| 2104 | |
---|
| 2105 | sub cmd_remove_tag { |
---|
| 2106 | local @ARGV = @_; |
---|
| 2107 | |
---|
| 2108 | my $help = get_cmd_name(); |
---|
| 2109 | my ($tag); |
---|
| 2110 | |
---|
| 2111 | GetOptions( |
---|
| 2112 | 'tag|t=s' => \$tag, |
---|
| 2113 | ); |
---|
| 2114 | |
---|
| 2115 | exit_on_error_option($help) |
---|
| 2116 | if $tag eq ''; |
---|
| 2117 | |
---|
[238] | 2118 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
[221] | 2119 | |
---|
[238] | 2120 | $computer_db->{'tag'} ||= {}; |
---|
| 2121 | die "Error: TAG does not exist: $tag\n" if not exists $computer_db->{'tag'}{$tag}; |
---|
| 2122 | |
---|
[221] | 2123 | # Test if some computer use this config |
---|
[345] | 2124 | LOOP_ON_SECTOR: |
---|
| 2125 | for my $sector_current (keys %{$computer_db}) { |
---|
| 2126 | next if $sector_current eq 'dset'; |
---|
| 2127 | next if $sector_current eq 'pool'; |
---|
| 2128 | next if $sector_current eq 'pxe'; |
---|
| 2129 | next if $sector_current eq 'tag'; |
---|
| 2130 | next if $sector_current eq 'version'; |
---|
[221] | 2131 | |
---|
| 2132 | LOOP_ON_COMPUTER: |
---|
[345] | 2133 | for my $computer (@{$computer_db->{$sector_current}}) { |
---|
[221] | 2134 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 2135 | |
---|
| 2136 | if (exists $attribute->{'tag'}) { |
---|
| 2137 | my $hostname = $attribute->{'hostname'}; |
---|
[345] | 2138 | die "Error: computer still use this TAG: $hostname.$sector_current $mac_address\n" if $tag eq $attribute->{'tag'}; |
---|
[221] | 2139 | } |
---|
| 2140 | } |
---|
| 2141 | } |
---|
| 2142 | |
---|
[238] | 2143 | delete $computer_db->{'tag'}{$tag}; |
---|
| 2144 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[221] | 2145 | } |
---|
| 2146 | |
---|
| 2147 | #-------------------------------------------------------------------------------- |
---|
| 2148 | |
---|
| 2149 | sub cmd_show_tag { |
---|
| 2150 | local @ARGV = @_; |
---|
| 2151 | |
---|
| 2152 | my ($no_header); |
---|
| 2153 | |
---|
| 2154 | GetOptions( |
---|
| 2155 | 'no-header|H' => \$no_header, |
---|
| 2156 | ); |
---|
| 2157 | |
---|
[238] | 2158 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 2159 | |
---|
[221] | 2160 | printf "%-12s %s\n", 'TAG', 'Comment' if not $no_header; |
---|
| 2161 | LOOP_ON_TAG: |
---|
[238] | 2162 | for my $tag ( keys %{$computer_db->{'tag'}} ) { |
---|
| 2163 | my $comment = $computer_db->{'tag'}{$tag}{'comment'}; |
---|
[221] | 2164 | |
---|
| 2165 | printf "%-12s %s\n", $tag, $comment; |
---|
| 2166 | } |
---|
| 2167 | } |
---|
| 2168 | |
---|
| 2169 | #-------------------------------------------------------------------------------- |
---|
| 2170 | # GLOBAL section |
---|
| 2171 | #-------------------------------------------------------------------------------- |
---|
| 2172 | |
---|
| 2173 | sub cmd_upgrade_db { |
---|
| 2174 | my $flag_change; |
---|
| 2175 | |
---|
[238] | 2176 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 2177 | |
---|
[345] | 2178 | LOOP_ON_SECTOR: |
---|
| 2179 | for my $sector_current (keys %{$computer_db}) { |
---|
| 2180 | next if $sector_current eq 'dset'; |
---|
| 2181 | next if $sector_current eq 'pool'; |
---|
| 2182 | next if $sector_current eq 'pxe'; |
---|
| 2183 | next if $sector_current eq 'tag'; |
---|
| 2184 | next if $sector_current eq 'version'; |
---|
[221] | 2185 | |
---|
[345] | 2186 | my @sectordb = @{$computer_db->{$sector_current}}; |
---|
[221] | 2187 | |
---|
| 2188 | LOOP_ON_COMPUTER: |
---|
[345] | 2189 | for my $computer (@sectordb) { |
---|
[221] | 2190 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 2191 | my $new_mac = normalize_mac_address($mac_address); |
---|
| 2192 | print "perl -pi -e 's/$mac_address:/$new_mac:/' $COMPUTER_YAML\n" if "$mac_address" ne "$new_mac"; |
---|
| 2193 | |
---|
| 2194 | my $comment = $attribute->{'comment'}; |
---|
| 2195 | $comment =~ s/\s\s+/ /g and $flag_change++; |
---|
| 2196 | $comment =~ s/^\s+\S// and $flag_change++; |
---|
| 2197 | $comment =~ s/\S\s+$// and $flag_change++; |
---|
| 2198 | $comment =~ s{^(\d\d\d\d)\/O(\d\/\d\d)}{$1/0$2} and $flag_change++; |
---|
| 2199 | $comment =~ s{^(\d\d\d\d\/\d\d\/)O(\d)}{$1/0$2} and $flag_change++; |
---|
| 2200 | $comment =~ s{^(\d\d\d\d)\/(\d\d)\/(\d\d)}{$1-$2-$3} and $flag_change++; |
---|
| 2201 | if ($comment !~ m/^\d\d\d\d-\d\d-\d\d/) { |
---|
| 2202 | print "# no date at beginning of comment $mac_address\n"; |
---|
| 2203 | } |
---|
| 2204 | |
---|
| 2205 | $attribute->{'comment'} = $comment; |
---|
| 2206 | } |
---|
| 2207 | } |
---|
| 2208 | print "# FLAG :$flag_change\n"; |
---|
| 2209 | |
---|
[238] | 2210 | ipamdb_save("$COMPUTER_YAML", $computer_db) if $flag_change; |
---|
[221] | 2211 | } |
---|
| 2212 | |
---|
| 2213 | #-------------------------------------------------------------------------------- |
---|
| 2214 | |
---|
[345] | 2215 | sub cmd_show_sector { |
---|
[317] | 2216 | local @ARGV = @_; |
---|
[221] | 2217 | |
---|
[317] | 2218 | my ($no_header); |
---|
| 2219 | |
---|
| 2220 | GetOptions( |
---|
| 2221 | 'no-header|H' => \$no_header, |
---|
| 2222 | ); |
---|
| 2223 | |
---|
[238] | 2224 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 2225 | |
---|
[317] | 2226 | my $tb_computer = Text::Table->new( |
---|
[345] | 2227 | {align => 'left', align_title => 'left', title => 'Sector'}, |
---|
[317] | 2228 | {is_sep => 1, body => ' '}, |
---|
[356] | 2229 | {align => 'left', align_title => 'left', title => 'DNS-Extension'}, |
---|
[317] | 2230 | {is_sep => 1, body => ' '}, |
---|
[355] | 2231 | {align => 'left', align_title => 'left', title => 'IP-Range'}, |
---|
[317] | 2232 | {align => 'left', align_title => 'left', title => 'Date'}, |
---|
| 2233 | {align => 'left', align_title => 'left', title => 'Comment'}, |
---|
[331] | 2234 | {align => 'left', align_title => 'left', title => 'Category'}, |
---|
[317] | 2235 | ); |
---|
| 2236 | |
---|
[345] | 2237 | LOOP_ON_SECTOR: |
---|
| 2238 | for my $sector_current (sort keys %{$computer_db}) { |
---|
| 2239 | next if $sector_current eq 'dset'; |
---|
| 2240 | next if $sector_current eq 'pool'; |
---|
| 2241 | next if $sector_current eq 'pxe'; |
---|
| 2242 | next if $sector_current eq 'tag'; |
---|
| 2243 | next if $sector_current eq 'version'; |
---|
[221] | 2244 | |
---|
[345] | 2245 | $tb_computer->add($sector_current), next LOOP_ON_SECTOR if not exists $computer_db->{'dset'}{$sector_current}; |
---|
[317] | 2246 | |
---|
[345] | 2247 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $computer_db->{'dset'}{$sector_current}{'modify_time'}; |
---|
[317] | 2248 | $year += 1900; |
---|
| 2249 | $mon++; |
---|
| 2250 | my $date = sprintf '%04i-%02i-%02i', $year, $mon, $mday; |
---|
| 2251 | |
---|
[355] | 2252 | my $ip_range; |
---|
[361] | 2253 | $ip_range = join ',', @{$computer_db->{'dset'}{$sector_current}{'ip_range'}} if exists $computer_db->{'dset'}{$sector_current}{'ip_range'}; |
---|
[355] | 2254 | |
---|
[331] | 2255 | my $category; |
---|
[345] | 2256 | my $comment = $computer_db->{'dset'}{$sector_current}{'comment'}; |
---|
[317] | 2257 | $comment =~ s/^\d\d\d\d-\d\d-\d\d\s//; |
---|
[331] | 2258 | $comment =~ s/\s+(\(\w+\))$// and $category = $1; |
---|
| 2259 | |
---|
[345] | 2260 | $tb_computer->add($sector_current, |
---|
| 2261 | $computer_db->{'dset'}{$sector_current}{'dns_extension'}, |
---|
[355] | 2262 | $ip_range, |
---|
[317] | 2263 | $date, |
---|
| 2264 | $comment, |
---|
[331] | 2265 | $category, |
---|
[317] | 2266 | ); |
---|
[221] | 2267 | } |
---|
[317] | 2268 | |
---|
| 2269 | print $tb_computer->title(), |
---|
| 2270 | $tb_computer->rule('-') if not $no_header; |
---|
| 2271 | print $tb_computer->body(); |
---|
[221] | 2272 | } |
---|
| 2273 | |
---|
| 2274 | #-------------------------------------------------------------------------------- |
---|
| 2275 | |
---|
| 2276 | sub cmd_search_mac { |
---|
| 2277 | local @ARGV = @_; |
---|
| 2278 | |
---|
| 2279 | my $help = get_cmd_name(); |
---|
| 2280 | my ($mac); |
---|
| 2281 | |
---|
| 2282 | GetOptions( |
---|
| 2283 | 'mac|m=s' => \$mac, |
---|
| 2284 | ); |
---|
| 2285 | |
---|
| 2286 | exit_on_error_option($help) |
---|
| 2287 | if $mac eq ''; |
---|
| 2288 | |
---|
| 2289 | $mac = normalize_mac_address($mac); |
---|
| 2290 | |
---|
[238] | 2291 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 2292 | |
---|
[221] | 2293 | control_syntax_mac_address($mac) or exit; |
---|
| 2294 | |
---|
[345] | 2295 | LOOP_ON_SECTOR: |
---|
| 2296 | for my $sector_current (keys %{$computer_db}) { |
---|
| 2297 | next if $sector_current eq 'dset'; |
---|
| 2298 | next if $sector_current eq 'pool'; |
---|
| 2299 | next if $sector_current eq 'pxe'; |
---|
| 2300 | next if $sector_current eq 'tag'; |
---|
| 2301 | next if $sector_current eq 'version'; |
---|
[221] | 2302 | |
---|
[345] | 2303 | my @sectordb = @{$computer_db->{$sector_current}}; |
---|
[221] | 2304 | |
---|
| 2305 | LOOP_ON_COMPUTER: |
---|
[345] | 2306 | for my $computer (@sectordb) { |
---|
[221] | 2307 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 2308 | |
---|
| 2309 | next LOOP_ON_COMPUTER if $mac_address ne $mac; |
---|
| 2310 | |
---|
| 2311 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $attribute->{'modify_time'}; |
---|
| 2312 | $year += 1900; |
---|
| 2313 | $mon++; |
---|
| 2314 | my $date = sprintf '%04i-%02i-%02i', $year, $mon, $mday; |
---|
| 2315 | |
---|
| 2316 | my $comment = $attribute->{'comment'}; |
---|
| 2317 | $comment =~ s/^\d\d\d\d-\d\d-\d\d\s//; |
---|
| 2318 | |
---|
| 2319 | my $enable = $attribute->{'enabled'}; |
---|
| 2320 | if (exists $attribute->{'pxe_config'}) { |
---|
| 2321 | $enable .= '/' . $attribute->{'pxe_config'}; |
---|
| 2322 | } |
---|
| 2323 | if (exists $attribute->{'tag'}) { |
---|
| 2324 | $enable .= ':' . $attribute->{'tag'}; |
---|
| 2325 | } |
---|
| 2326 | |
---|
| 2327 | printf "%-30s %-20s %17s %9s %3s %10s %s\n", |
---|
[345] | 2328 | $attribute->{'hostname'} . '.' . $sector_current, |
---|
[221] | 2329 | $attribute->{'ip'}, |
---|
| 2330 | $mac_address, |
---|
| 2331 | $attribute->{'address_type'}, |
---|
| 2332 | $enable, |
---|
| 2333 | $date, |
---|
| 2334 | $comment; |
---|
| 2335 | } |
---|
| 2336 | } |
---|
| 2337 | } |
---|
| 2338 | |
---|
| 2339 | #-------------------------------------------------------------------------------- |
---|
| 2340 | #Nom: show |
---|
| 2341 | #Description: liste les machines à partir du fichier YAML par nom de domaine. |
---|
| 2342 | |
---|
| 2343 | sub cmd_show_host { |
---|
| 2344 | my %ipdb = (); |
---|
| 2345 | |
---|
[238] | 2346 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 2347 | |
---|
[299] | 2348 | my $tb_computer = Text::Table->new( |
---|
[345] | 2349 | {align => 'left', align_title => 'left', title => 'Hostname.Sector'}, |
---|
[299] | 2350 | {is_sep => 1, body => ' '}, |
---|
| 2351 | {align => 'left', align_title => 'left', title => 'IPv4-Address'}, |
---|
| 2352 | {is_sep => 1, body => ' '}, |
---|
| 2353 | {align => 'center', align_title => 'center', title => 'MAC-Address'}, |
---|
| 2354 | {is_sep => 1, body => ' '}, |
---|
| 2355 | {align => 'right', align_title => 'right', title => 'Type'}, |
---|
| 2356 | {align => 'right', align_title => 'right', title => 'Status'}, |
---|
| 2357 | {is_sep => 1, body => ' '}, |
---|
| 2358 | {align => 'left', align_title => 'left', title => 'Date'}, |
---|
| 2359 | {align => 'left', align_title => 'left', title => 'Comment'}, |
---|
| 2360 | ); |
---|
| 2361 | |
---|
[345] | 2362 | LOOP_ON_SECTOR: |
---|
| 2363 | for my $sector_current (sort keys %{$computer_db}) { |
---|
| 2364 | next if $sector_current eq 'dset'; |
---|
| 2365 | next if $sector_current eq 'pool'; |
---|
| 2366 | next if $sector_current eq 'pxe'; |
---|
| 2367 | next if $sector_current eq 'tag'; |
---|
| 2368 | next if $sector_current eq 'version'; |
---|
[221] | 2369 | |
---|
[345] | 2370 | my @sectordb = @{$computer_db->{$sector_current}}; |
---|
[221] | 2371 | |
---|
| 2372 | LOOP_ON_COMPUTER: |
---|
[345] | 2373 | for my $computer (@sectordb) { |
---|
[221] | 2374 | my ($mac_address, $attribute) = %{$computer}; |
---|
[295] | 2375 | my $ip = $attribute->{'ip'}; |
---|
[221] | 2376 | |
---|
[295] | 2377 | if ($ip =~ m/$DDT::RE::IPv4_ADDRESS/xms) { |
---|
| 2378 | if ( not exists $ipdb{$ip} ) { |
---|
| 2379 | $ipdb{$ip} = { |
---|
[221] | 2380 | 'mac_address' => $mac_address, |
---|
| 2381 | %{$attribute}, |
---|
[345] | 2382 | 'sector' => $sector_current, |
---|
[221] | 2383 | }; |
---|
| 2384 | } |
---|
| 2385 | else { |
---|
[299] | 2386 | print {*STDERR} "# Warning: $ip already exists in the database with MAC $mac_address!\n"; |
---|
[221] | 2387 | } |
---|
[299] | 2388 | next LOOP_ON_COMPUTER; |
---|
[221] | 2389 | } |
---|
| 2390 | |
---|
| 2391 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $attribute->{'modify_time'}; |
---|
| 2392 | $year += 1900; |
---|
| 2393 | $mon++; |
---|
| 2394 | my $date = sprintf '%04i-%02i-%02i', $year, $mon, $mday; |
---|
| 2395 | |
---|
| 2396 | my $comment = normalize_comment($attribute->{'comment'}); |
---|
| 2397 | $comment =~ s/^\d\d\d\d-\d\d-\d\d\s//; |
---|
| 2398 | |
---|
| 2399 | my $enable = $attribute->{'enabled'}; |
---|
| 2400 | if (exists $attribute->{'pxe_config'}) { |
---|
| 2401 | $enable .= '/' . $attribute->{'pxe_config'}; |
---|
| 2402 | } |
---|
[295] | 2403 | if (exists $attribute->{'tag'}) { |
---|
| 2404 | $enable .= ':' . $attribute->{'tag'}; |
---|
[221] | 2405 | } |
---|
| 2406 | |
---|
[296] | 2407 | #printf "%-30s %-20s %17s %9s %3s %10s %s\n", |
---|
| 2408 | $tb_computer->add( |
---|
[345] | 2409 | $attribute->{'hostname'} . '.' . $sector_current, |
---|
[221] | 2410 | $ip, |
---|
| 2411 | $mac_address, |
---|
| 2412 | $attribute->{'address_type'}, |
---|
| 2413 | $enable, |
---|
| 2414 | $date, |
---|
[296] | 2415 | $comment, |
---|
| 2416 | ); |
---|
[221] | 2417 | } |
---|
[345] | 2418 | #print "\n# *** List of pool computers in the sector: $sector_current ***\n"; |
---|
[221] | 2419 | } |
---|
| 2420 | |
---|
[345] | 2421 | #print "\n# *** List of computers ordered by IP and sector ***\n"; |
---|
[221] | 2422 | LOOP_ON_IP_ADDRESS: |
---|
| 2423 | foreach my $ip (Net::Netmask::sort_by_ip_address(keys %ipdb)) { |
---|
| 2424 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $ipdb{$ip}->{'modify_time'}; |
---|
| 2425 | $year += 1900; |
---|
| 2426 | $mon++; |
---|
| 2427 | my $date = sprintf '%04i-%02i-%02i', $year, $mon, $mday; |
---|
| 2428 | |
---|
| 2429 | my $comment =$ipdb{$ip}->{'comment'}; |
---|
| 2430 | $comment =~ s/^\d\d\d\d-\d\d-\d\d\s//; |
---|
| 2431 | |
---|
| 2432 | my $enable = $ipdb{$ip}->{'enabled'}; |
---|
| 2433 | if (exists $ipdb{$ip}->{'pxe_config'}) { |
---|
| 2434 | $enable .= '/' . $ipdb{$ip}->{'pxe_config'}; |
---|
| 2435 | } |
---|
| 2436 | if (exists $ipdb{$ip}->{'tag'}) { |
---|
| 2437 | $enable .= ':' . $ipdb{$ip}->{'tag'}; |
---|
| 2438 | } |
---|
| 2439 | |
---|
[299] | 2440 | #printf "%-30s %-20s %17s %9s %3s %10s %s\n", |
---|
| 2441 | $tb_computer->add( |
---|
[345] | 2442 | $ipdb{$ip}->{'hostname'} . '.' . $ipdb{$ip}->{'sector'}, |
---|
[221] | 2443 | $ip, |
---|
| 2444 | normalize_mac_address($ipdb{$ip}->{'mac_address'}), |
---|
| 2445 | $ipdb{$ip}->{'address_type'}, |
---|
| 2446 | $enable, |
---|
| 2447 | $date, |
---|
[299] | 2448 | $comment |
---|
| 2449 | ); |
---|
[221] | 2450 | } |
---|
[299] | 2451 | |
---|
| 2452 | print $tb_computer->title(); |
---|
| 2453 | print $tb_computer->rule('-'); |
---|
| 2454 | print $tb_computer->body(); |
---|
[221] | 2455 | } |
---|
| 2456 | |
---|
| 2457 | #------------------------------------------------------------------------------- |
---|
| 2458 | #Nom: cmd_generate_dhcp_file |
---|
| 2459 | #Description: génère les fichiers de configuration des machines et des pools du dhcp |
---|
| 2460 | |
---|
| 2461 | sub cmd_generate_dhcp_file { |
---|
| 2462 | backup_database(); |
---|
| 2463 | |
---|
[238] | 2464 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 2465 | |
---|
[221] | 2466 | my %file_pool; |
---|
| 2467 | |
---|
[345] | 2468 | for my $sector_current (keys %{$computer_db}) { |
---|
| 2469 | next if $sector_current eq 'dset'; |
---|
| 2470 | next if $sector_current eq 'pool'; |
---|
| 2471 | next if $sector_current eq 'pxe'; |
---|
| 2472 | next if $sector_current eq 'tag'; |
---|
| 2473 | next if $sector_current eq 'version'; |
---|
[221] | 2474 | |
---|
[345] | 2475 | open FILE_VLAN, '>', "$FOLDER_GEN_DHCP/$sector_current"; |
---|
| 2476 | my @sectordb = @{$computer_db->{$sector_current}}; |
---|
| 2477 | for my $value (@sectordb) { |
---|
[221] | 2478 | ALL_MAC_ADDRESS: |
---|
| 2479 | for my $mac_addres (keys %{$value}) { |
---|
| 2480 | #host pcdavoust { deny-unknown-clients; |
---|
| 2481 | #hardware ethernet 0:6:5b:b8:13:d1; |
---|
| 2482 | #fixed-address 194.254.66.72; |
---|
| 2483 | #} |
---|
| 2484 | |
---|
| 2485 | my $hostname = $value->{$mac_addres}{'hostname'}; |
---|
| 2486 | my $ip = $value->{$mac_addres}{'ip'}; |
---|
| 2487 | my $comment = $value->{$mac_addres}{'comment'}; |
---|
| 2488 | my $address_type = $value->{$mac_addres}{'address_type'}; |
---|
| 2489 | my $enabled = $value->{$mac_addres}{'enabled'}; |
---|
| 2490 | my $tags = $value->{$mac_addres}{'tag'} || 'universal'; |
---|
| 2491 | |
---|
| 2492 | my $buffer; |
---|
| 2493 | if ($address_type eq 'dhcp') { |
---|
| 2494 | if ($enabled eq 'yes') { |
---|
| 2495 | $buffer = "host $hostname {\n"; # deny-unknown-clients; |
---|
| 2496 | $buffer .= " hardware ethernet $mac_addres;\n"; |
---|
| 2497 | $buffer .= " fixed-address $ip;\n"; |
---|
[227] | 2498 | |
---|
[221] | 2499 | if (exists $value->{$mac_addres}{'pxe_config'}) { |
---|
| 2500 | my $pxe_config = $value->{$mac_addres}{'pxe_config'}; |
---|
[238] | 2501 | my $ip_next_server = $computer_db->{'pxe'}{$pxe_config}{'ip_next_server'}; |
---|
| 2502 | my $filename = $computer_db->{'pxe'}{$pxe_config}{'filename'}; |
---|
[221] | 2503 | $buffer .= " next-server $ip_next_server;\n"; |
---|
| 2504 | $buffer .= " filename \"$filename\";\n"; |
---|
| 2505 | } |
---|
| 2506 | $buffer .= " #comment: $comment\n"; |
---|
| 2507 | $buffer .= " }\n"; |
---|
| 2508 | $buffer .= "\n"; |
---|
| 2509 | |
---|
| 2510 | for my $tag (split/,/, $tags) { |
---|
| 2511 | $file_pool{"tag-$tag"} ||= []; |
---|
| 2512 | push @{$file_pool{"tag-$tag"}}, "subclass \"tag-$tag\" 1:$mac_addres; # $comment\n"; |
---|
| 2513 | } |
---|
| 2514 | } |
---|
| 2515 | else { |
---|
| 2516 | $buffer = "#host $hostname {\n"; # deny-unknown-clients; |
---|
| 2517 | $buffer .= "# hardware ethernet $mac_addres;\n"; |
---|
| 2518 | $buffer .= "# fixed-address $ip;\n"; |
---|
| 2519 | $buffer .= "# comment: $comment \n"; |
---|
| 2520 | $buffer .= "# }\n"; |
---|
| 2521 | $buffer .= "\n"; |
---|
| 2522 | } |
---|
| 2523 | print FILE_VLAN $buffer; |
---|
| 2524 | } |
---|
| 2525 | elsif ($address_type eq 'pool-dhcp') { |
---|
| 2526 | #--- Génère les fichiers pool dhcp ---# |
---|
[238] | 2527 | for my $current_pool (keys %{$computer_db->{'pool'}}) { |
---|
[221] | 2528 | next if $current_pool ne $ip; |
---|
| 2529 | |
---|
| 2530 | if ($enabled eq 'yes') { |
---|
| 2531 | $buffer = "subclass \"$current_pool\" 1:$mac_addres; # $comment\n"; |
---|
| 2532 | |
---|
| 2533 | for my $tag (split/,/, $tags) { |
---|
| 2534 | $file_pool{"tag-$tag"} ||= []; |
---|
| 2535 | push @{$file_pool{"tag-$tag"}}, "subclass \"tag-$tag\" 1:$mac_addres; # $comment\n"; |
---|
| 2536 | } |
---|
| 2537 | } |
---|
| 2538 | else { |
---|
| 2539 | $buffer = "#subclass \"$current_pool\" 1:$mac_addres; # $comment\n"; |
---|
| 2540 | } |
---|
| 2541 | |
---|
[238] | 2542 | my $current_pool_file_name = $computer_db->{'pool'}{$current_pool}{'file'}; |
---|
[221] | 2543 | |
---|
| 2544 | $file_pool{$current_pool_file_name} ||= []; |
---|
| 2545 | push @{$file_pool{$current_pool_file_name}}, $buffer; |
---|
| 2546 | } |
---|
| 2547 | } |
---|
| 2548 | } |
---|
| 2549 | } |
---|
| 2550 | |
---|
| 2551 | close FILE_VLAN; |
---|
| 2552 | |
---|
| 2553 | for my $file_name (keys %file_pool) { |
---|
| 2554 | open FILE_POOL, '>', "$FOLDER_GEN_DHCP/$file_name"; |
---|
| 2555 | print FILE_POOL @{$file_pool{$file_name}}; |
---|
| 2556 | close FILE_POOL; |
---|
| 2557 | } |
---|
| 2558 | } |
---|
| 2559 | print "Copy DHCP files from $FOLDER_GEN_DHCP to /etc/dhcp/include/\n"; |
---|
| 2560 | exec $SCRIPT_UPDATE; |
---|
| 2561 | } |
---|
| 2562 | |
---|
| 2563 | #------------------------------------------------------------------------------- |
---|
| 2564 | #Nom: cmd_generate_dns_file |
---|
| 2565 | #Description: génère les fichiers d'enregistrements DNS |
---|
| 2566 | |
---|
| 2567 | sub cmd_generate_dns_file { |
---|
[337] | 2568 | local @ARGV = @_; |
---|
| 2569 | |
---|
| 2570 | my $help = get_cmd_name(); |
---|
| 2571 | my ($verbose); |
---|
| 2572 | |
---|
| 2573 | GetOptions( |
---|
| 2574 | 'verbose|v' => \$verbose, |
---|
| 2575 | ); |
---|
| 2576 | |
---|
[318] | 2577 | my $buffer_fwd; |
---|
[221] | 2578 | my $buffer_rev; |
---|
| 2579 | my $pool_domain; |
---|
| 2580 | |
---|
[238] | 2581 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 2582 | |
---|
[345] | 2583 | for my $sector_current (keys %{$computer_db}) { |
---|
| 2584 | next if $sector_current eq 'dset'; |
---|
| 2585 | next if $sector_current eq 'pool'; |
---|
| 2586 | next if $sector_current eq 'pxe'; |
---|
| 2587 | next if $sector_current eq 'tag'; |
---|
| 2588 | next if $sector_current eq 'version'; |
---|
[221] | 2589 | |
---|
[345] | 2590 | if ($sector_current eq 'pool') { |
---|
[318] | 2591 | LOOP_ON_COMPUTER: |
---|
[345] | 2592 | for my $computer (@{$computer_db->{$sector_current}}) { |
---|
[318] | 2593 | for my $pool_name (keys %{$computer}) { |
---|
| 2594 | $pool_domain = $computer->{$pool_name}->{'domain'}."\n"; |
---|
| 2595 | #print $computer->{$pool_name}->{'file'}; |
---|
[221] | 2596 | chomp $pool_domain; |
---|
| 2597 | open FILE_FORWARD_DNS, '>>', "$FOLDER_GEN_DNS/db.$pool_domain.fwd"; |
---|
| 2598 | open FILE_REVERSE_DNS, '>>', "$FOLDER_GEN_DNS/db.$pool_domain.rev"; |
---|
[318] | 2599 | my @T_pool_ip = @{$computer->{$pool_name}->{'ip'}}; |
---|
[221] | 2600 | for my $pool_ip (@T_pool_ip) { |
---|
| 2601 | my @T_split = split(/\./ , $pool_ip); |
---|
[318] | 2602 | $buffer_fwd = sprintf "%-24s IN A %-15s ;\n", "$pool_name$T_split[3]", $pool_ip; |
---|
[221] | 2603 | $buffer_rev = "$T_split[3] IN PTR $pool_name$T_split[3].$pool_domain.\n"; |
---|
[318] | 2604 | print FILE_FORWARD_DNS $buffer_fwd; |
---|
[221] | 2605 | print FILE_REVERSE_DNS $buffer_rev; |
---|
| 2606 | } |
---|
| 2607 | close FILE_FORWARD_DNS; |
---|
| 2608 | close FILE_REVERSE_DNS; |
---|
| 2609 | } |
---|
| 2610 | } |
---|
| 2611 | } |
---|
| 2612 | |
---|
| 2613 | else { |
---|
| 2614 | #--- Création du fichier non-reverse ---# |
---|
[345] | 2615 | open FILE_FORWARD_DNS, ">> $FOLDER_GEN_DNS/db.$sector_current.fwd"; |
---|
| 2616 | open FILE_REVERSE_DNS, ">> $FOLDER_GEN_DNS/db.$sector_current.rev"; |
---|
[318] | 2617 | |
---|
[345] | 2618 | my @sectordb = @{$computer_db->{$sector_current}}; |
---|
[227] | 2619 | |
---|
[318] | 2620 | LOOP_ON_COMPUTER: |
---|
[345] | 2621 | for my $computer (@sectordb) { |
---|
[318] | 2622 | my ($mac_address, $attribute) = %{$computer}; |
---|
[221] | 2623 | |
---|
[318] | 2624 | #host pcdavoust { deny-unknown-clients; |
---|
| 2625 | #hardware ethernet 0:6:5b:b8:13:d1; |
---|
| 2626 | #fixed-address 194.254.66.72; |
---|
| 2627 | #} |
---|
| 2628 | |
---|
| 2629 | my $hostname = $attribute->{'hostname'}; |
---|
| 2630 | my $ip = $attribute->{'ip'}; |
---|
| 2631 | my $comment = $attribute->{'comment'}; |
---|
| 2632 | my $address_type = $attribute->{'address_type'}; |
---|
| 2633 | my $enabled = $attribute->{'enabled'}; |
---|
| 2634 | |
---|
| 2635 | next LOOP_ON_COMPUTER if not (($address_type eq 'dhcp') or ($address_type eq 'static')); |
---|
| 2636 | |
---|
[345] | 2637 | my $dns_domain = $sector_current; |
---|
| 2638 | if (exists $computer_db->{'dset'}{$sector_current}) { |
---|
| 2639 | $dns_domain = $computer_db->{'dset'}{$sector_current}{'dns_extension'}; |
---|
[318] | 2640 | } |
---|
| 2641 | |
---|
| 2642 | my @ip_split = split /\./, $ip; |
---|
| 2643 | if ($enabled eq 'yes') { |
---|
| 2644 | if (exists $attribute->{'dns_extension'} |
---|
| 2645 | and "$attribute->{'dns_extension'}" != "$dns_domain") { |
---|
| 2646 | print "A FAIRE\n"; |
---|
[221] | 2647 | } |
---|
[318] | 2648 | $buffer_fwd = sprintf "%-24s IN A %-15s ; %s\n", $hostname, $ip, $comment; |
---|
| 2649 | $buffer_rev = sprintf "%3i IN PTR %-15s\n", $ip_split[3], "$hostname.$dns_domain."; |
---|
[221] | 2650 | } |
---|
[318] | 2651 | |
---|
| 2652 | else { |
---|
| 2653 | $buffer_fwd = sprintf ";%-24s IN A %-15s ; %s\n", $hostname, $ip, $comment; |
---|
| 2654 | $buffer_rev = sprintf ";%3i IN PTR %-15s\n", $ip_split[3], "$hostname.$dns_domain."; |
---|
| 2655 | } |
---|
| 2656 | print FILE_REVERSE_DNS $buffer_rev; |
---|
| 2657 | print FILE_FORWARD_DNS $buffer_fwd; |
---|
[221] | 2658 | } |
---|
| 2659 | close FILE_REVERSE_DNS; |
---|
| 2660 | close FILE_FORWARD_DNS; |
---|
[345] | 2661 | print "- DNS: db.$sector_current.fwd db.$sector_current.rev [CREATE].\n" if $verbose; |
---|
| 2662 | print " Ex : sort -k 4n -t . $FOLDER_GEN_DNS/db.$sector_current.fwd\n" if $verbose; |
---|
[221] | 2663 | } |
---|
| 2664 | } |
---|
| 2665 | } |
---|
| 2666 | |
---|
| 2667 | #-------------------------------------------------------------------------------- |
---|
| 2668 | |
---|
| 2669 | sub shell_command { |
---|
| 2670 | my $cmd = shift; |
---|
| 2671 | |
---|
| 2672 | require FileHandle; |
---|
| 2673 | my $fh = new FileHandle; |
---|
| 2674 | my @result = (); |
---|
| 2675 | open $fh, q{-|}, "LANG=C $cmd" or die "Can't exec $cmd\n"; |
---|
| 2676 | @result = <$fh>; |
---|
| 2677 | close $fh; |
---|
| 2678 | chomp @result; |
---|
| 2679 | return @result; |
---|
| 2680 | } |
---|
| 2681 | |
---|
[338] | 2682 | #-------------------------------------------------------------------------------- |
---|
| 2683 | |
---|
[221] | 2684 | sub cmd_check_dns { |
---|
[338] | 2685 | local @ARGV = @_; |
---|
| 2686 | |
---|
| 2687 | my $help = get_cmd_name(); |
---|
[359] | 2688 | my ($opt_direct, $opt_reverse, $opt_byip); |
---|
[338] | 2689 | |
---|
| 2690 | GetOptions( |
---|
[342] | 2691 | 'direct|d' => \$opt_direct, |
---|
[338] | 2692 | 'reverse|r' => \$opt_reverse, |
---|
[359] | 2693 | 'by-ip|b' => \$opt_byip, |
---|
[338] | 2694 | ); |
---|
| 2695 | |
---|
[238] | 2696 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
[221] | 2697 | |
---|
[359] | 2698 | if ($opt_direct or not ($opt_reverse or $opt_byip)) { # DDT to DNS check |
---|
[345] | 2699 | LOOP_ON_SECTOR: |
---|
| 2700 | for my $sector_current (keys %{$computer_db}) { |
---|
| 2701 | next if $sector_current eq 'dset'; |
---|
| 2702 | next if $sector_current eq 'pool'; |
---|
| 2703 | next if $sector_current eq 'pxe'; |
---|
| 2704 | next if $sector_current eq 'tag'; |
---|
| 2705 | next if $sector_current eq 'version'; |
---|
[221] | 2706 | |
---|
[345] | 2707 | my @sectordb = @{$computer_db->{$sector_current}}; |
---|
[221] | 2708 | |
---|
[342] | 2709 | LOOP_ON_COMPUTER: |
---|
[345] | 2710 | for my $computer (@sectordb) { |
---|
[342] | 2711 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 2712 | #my $new_mac = normalize_mac_address($mac_address); |
---|
| 2713 | my $ip = $attribute->{'ip'}; |
---|
| 2714 | next LOOP_ON_COMPUTER if not $ip =~ m/$DDT::RE::IPv4_ADDRESS/xms; |
---|
| 2715 | next LOOP_ON_COMPUTER if $attribute->{'enabled'} eq 'no'; |
---|
[221] | 2716 | |
---|
[342] | 2717 | my $dns_hostname_fq = scalar gethostbyaddr(inet_aton($ip), AF_INET); |
---|
| 2718 | my ($dns_hostname) = split /\./, $dns_hostname_fq; |
---|
[221] | 2719 | |
---|
[342] | 2720 | if ($attribute->{'hostname'} ne $dns_hostname) { |
---|
[345] | 2721 | print "$mac_address ($sector_current) $ip - $dns_hostname / $attribute->{'hostname'} # $attribute->{'comment'}\n"; |
---|
[221] | 2722 | next LOOP_ON_COMPUTER; |
---|
| 2723 | } |
---|
[342] | 2724 | |
---|
| 2725 | my $packed_ip = scalar gethostbyname($dns_hostname_fq); |
---|
| 2726 | if (defined $packed_ip) { |
---|
| 2727 | my $ip_address = inet_ntoa($packed_ip); |
---|
| 2728 | if ($ip ne $ip_address) { |
---|
[360] | 2729 | print "Error: bad IP $ip for reverse DNS on $dns_hostname_fq\n"; |
---|
[342] | 2730 | next LOOP_ON_COMPUTER; |
---|
| 2731 | } |
---|
| 2732 | } |
---|
[221] | 2733 | } |
---|
| 2734 | } |
---|
| 2735 | } |
---|
| 2736 | |
---|
[342] | 2737 | if ($opt_reverse) { # DNS to DDT check |
---|
[341] | 2738 | my %saw; # count for unique member |
---|
| 2739 | my @dns_domain_list = sort grep !$saw{$_}++, |
---|
| 2740 | map $computer_db->{'dset'}{$_}{'dns_extension'}, |
---|
| 2741 | grep exists($computer_db->{'dset'}{$_}{'dns_extension'}), |
---|
| 2742 | keys $computer_db->{'dset'}; |
---|
| 2743 | LOOP_ON_DNS: |
---|
| 2744 | for my $dns (@dns_domain_list) { |
---|
| 2745 | LOOP_ON_IP: |
---|
| 2746 | for (shell_command("host -t A -l $dns")) { |
---|
| 2747 | # smtp2.legi.grenoble-inp.fr has address 194.254.67.37 |
---|
| 2748 | next if not m/has address/; |
---|
| 2749 | next if not m/^(\w[\w-_\.]+\w)\s+has\saddress\s+(\d[\d\.]+\d)$/; |
---|
| 2750 | my ($hostname_fq, $ip) = ($1, $2); |
---|
| 2751 | control_syntax_ip($ip) or next LOOP_ON_IP; |
---|
| 2752 | if (control_exist_ip($computer_db, $ip) == 1) { |
---|
[344] | 2753 | printf "Unkown IP: %-15s / %s\n", $ip, $hostname_fq; |
---|
[341] | 2754 | next LOOP_ON_IP; |
---|
| 2755 | } |
---|
[221] | 2756 | } |
---|
| 2757 | } |
---|
| 2758 | } |
---|
[359] | 2759 | |
---|
| 2760 | if ($opt_byip) { # IP Range DDT check |
---|
| 2761 | my @ip_check; |
---|
| 2762 | LOOP_ON_SECTOR: |
---|
[360] | 2763 | for my $sector_current (keys %{$computer_db->{'dset'}}) { |
---|
| 2764 | next LOOP_ON_SECTOR if not exists $computer_db->{'dset'}{$sector_current}{'ip_range'}; |
---|
[359] | 2765 | |
---|
| 2766 | LOOP_ON_CIDR: |
---|
[360] | 2767 | for my $ip_range (@{$computer_db->{'dset'}{$sector_current}{'ip_range'}}) { |
---|
[359] | 2768 | |
---|
| 2769 | LOOP_ON_IP: |
---|
| 2770 | for my $ip (NetAddr::IP->new($ip_range)->hostenum()) { |
---|
[361] | 2771 | $ip =~ s{/32$}{}; |
---|
[360] | 2772 | |
---|
| 2773 | my $dns_hostname_fq = scalar gethostbyaddr(inet_aton($ip), AF_INET); |
---|
| 2774 | my ($dns_hostname) = split /\./, $dns_hostname_fq; |
---|
| 2775 | |
---|
| 2776 | # Verify reverse return same IP |
---|
| 2777 | my $packed_ip = scalar gethostbyname($dns_hostname_fq); |
---|
| 2778 | if (defined $packed_ip) { |
---|
| 2779 | my $ip_address = inet_ntoa($packed_ip); |
---|
| 2780 | if ($ip ne $ip_address) { |
---|
[362] | 2781 | print "Error: bad IP $ip for reverse DNS on $dns_hostname_fq sector $sector_current\n"; |
---|
[360] | 2782 | } |
---|
| 2783 | } |
---|
| 2784 | |
---|
[362] | 2785 | # Verify direct return same hostname |
---|
[360] | 2786 | LOOP_ON_COMPUTER: |
---|
[361] | 2787 | for my $computer (@{$computer_db->{$sector_current}}) { |
---|
[360] | 2788 | my ($mac_address, $attribute) = %{$computer}; |
---|
| 2789 | |
---|
| 2790 | next LOOP_ON_COMPUTER if $attribute->{'ip'} ne $ip; |
---|
| 2791 | |
---|
[362] | 2792 | print "Error: bad DNS host $dns_hostname / DDT host $attribute->{'hostname'} in sector $sector_current with IP $ip\n" |
---|
| 2793 | if $attribute->{'hostname'} ne $dns_hostname; |
---|
| 2794 | |
---|
[360] | 2795 | next LOOP_ON_IP; |
---|
| 2796 | } |
---|
| 2797 | |
---|
| 2798 | # Declare in DNS but not in DDT |
---|
[361] | 2799 | print "Error: DNS host $dns_hostname in sector $sector_current with IP $ip not declare in DDT database\n" if $dns_hostname ne ''; |
---|
[359] | 2800 | } |
---|
| 2801 | } |
---|
| 2802 | } |
---|
| 2803 | } |
---|
[221] | 2804 | } |
---|
| 2805 | |
---|
| 2806 | #------------------------------------------------------------------------------- |
---|
| 2807 | #Nom: load_data_dhcp |
---|
| 2808 | #Description: permet de charger le fichier de données YAML via les fichiers de configuration |
---|
| 2809 | # machines. |
---|
| 2810 | # ATTENTION: LES COMMENTAIRES DU FICHIER DISPARAITRONT. |
---|
| 2811 | |
---|
| 2812 | sub load_data_dhcp { |
---|
[345] | 2813 | my ($sector, $input_file) = @_; |
---|
[221] | 2814 | |
---|
[238] | 2815 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 2816 | |
---|
[221] | 2817 | my @T_mac; |
---|
| 2818 | my @T_host; |
---|
| 2819 | my @T_ip; |
---|
| 2820 | my $cpt; |
---|
| 2821 | open (FILE, "<$input_file"); |
---|
| 2822 | my @buffer = <FILE>; |
---|
| 2823 | close(FILE); |
---|
| 2824 | |
---|
[357] | 2825 | LINE: |
---|
[221] | 2826 | for my $ligne (@buffer) { |
---|
| 2827 | #-- |
---|
| 2828 | $ligne =~ s/#.*$//; |
---|
| 2829 | $ligne =~ s/\s+/ /; |
---|
| 2830 | $ligne =~ s/^\s+//; |
---|
| 2831 | next if $ligne eq ''; |
---|
| 2832 | |
---|
| 2833 | if ($ligne =~ /^host /) { |
---|
| 2834 | $cpt=0; |
---|
| 2835 | my @T_split = split(/host\s+/, $ligne); |
---|
| 2836 | @T_host = split(/ /, $T_split[1]); |
---|
| 2837 | chomp($T_host[0]); |
---|
| 2838 | |
---|
| 2839 | $cpt++; |
---|
| 2840 | } |
---|
| 2841 | |
---|
| 2842 | if ($ligne =~ /^*ethernet /) { |
---|
| 2843 | $ligne =~ s/;//g; |
---|
| 2844 | @T_mac = split(/ethernet\s+/, $ligne); |
---|
| 2845 | chomp($T_mac[1]); |
---|
| 2846 | $cpt++; |
---|
| 2847 | } |
---|
| 2848 | |
---|
| 2849 | if ($ligne =~ /^*address /) { |
---|
| 2850 | $ligne =~ s/;//g; |
---|
| 2851 | @T_ip = split(/address\s+/, $ligne); |
---|
| 2852 | chomp($T_ip[1]); |
---|
| 2853 | |
---|
| 2854 | $cpt++; |
---|
| 2855 | } |
---|
| 2856 | |
---|
| 2857 | if ($cpt == 3) { |
---|
| 2858 | # print "MAC $T_mac[1] HOST $T_host[0] IP $T_ip[1].\n"; |
---|
| 2859 | my $mac = $T_mac[1]; |
---|
| 2860 | my $hostname = $T_host[0]; |
---|
| 2861 | my $ip = $T_ip[1]; |
---|
| 2862 | $cpt = 0; |
---|
| 2863 | |
---|
[345] | 2864 | if ( control_exist_hostname($computer_db, $sector, $hostname) == 0 ) { |
---|
| 2865 | print "Error: host already exist in sector $sector: $hostname\n"; |
---|
[357] | 2866 | next LINE; |
---|
[221] | 2867 | } |
---|
[357] | 2868 | control_syntax_mac_address($mac) or next LINE; |
---|
[238] | 2869 | if ( control_exist_mac($computer_db, $mac) == 0) { |
---|
[332] | 2870 | print "Error: physical MAC address already exists: $mac\n"; |
---|
[357] | 2871 | next LINE; |
---|
[221] | 2872 | } |
---|
| 2873 | |
---|
[357] | 2874 | control_syntax_ip($ip) or next LINE; |
---|
[238] | 2875 | if ( control_exist_ip($computer_db, $ip) == 0 ) { |
---|
[221] | 2876 | print "Error: IP address already exists: $ip\n"; |
---|
[357] | 2877 | next LINE; |
---|
[221] | 2878 | } |
---|
| 2879 | my $timestamp = time; |
---|
[345] | 2880 | push @{$computer_db->{$sector}}, { $mac => { |
---|
[221] | 2881 | 'hostname' => $hostname, |
---|
| 2882 | 'ip' => $ip, |
---|
| 2883 | 'address_type' => 'dhcp', |
---|
| 2884 | 'enabled' => 'yes', |
---|
| 2885 | 'create_time' => $timestamp, |
---|
| 2886 | 'modify_time' => $timestamp, |
---|
| 2887 | 'alias' => '', |
---|
| 2888 | }}; |
---|
| 2889 | } |
---|
| 2890 | } |
---|
| 2891 | } |
---|
| 2892 | |
---|
| 2893 | #------------------------------------------------------------------------------- |
---|
| 2894 | #Nom: load_data_pool |
---|
| 2895 | #Description: permet de charger le fichier YAML via les fichiers de conf 'pool' du dhcp. |
---|
| 2896 | |
---|
| 2897 | sub load_data_pool { |
---|
[345] | 2898 | my ($sector, $input_file) = @_; |
---|
[221] | 2899 | |
---|
| 2900 | my @T_mac; |
---|
| 2901 | |
---|
| 2902 | open (FILE, "<$input_file"); |
---|
| 2903 | my @buffer = <FILE>; |
---|
| 2904 | close(FILE); |
---|
| 2905 | |
---|
[238] | 2906 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 2907 | |
---|
[221] | 2908 | for my $ligne (@buffer) { |
---|
| 2909 | #-- |
---|
| 2910 | $ligne =~ s/#.*$//; |
---|
| 2911 | $ligne =~ s/\s+/ /; |
---|
| 2912 | $ligne =~ s/^\s+//; |
---|
| 2913 | $ligne =~ s/;//g; |
---|
| 2914 | $ligne =~ s/"//g; |
---|
| 2915 | next if $ligne eq ''; |
---|
| 2916 | |
---|
| 2917 | if (($ligne =~ /^subclass/)) { |
---|
| 2918 | my @T_split = split(/ / ,$ligne); |
---|
| 2919 | my $pool = $T_split[1]; |
---|
| 2920 | |
---|
| 2921 | @T_mac = split(/:/ , $T_split[2]); |
---|
| 2922 | my $mac = $T_mac[1].":".$T_mac[2].":".$T_mac[3].":".$T_mac[4].":".$T_mac[5].":".$T_mac[6]; |
---|
| 2923 | control_syntax_mac_address($mac) or next; |
---|
[238] | 2924 | if (control_exist_mac($computer_db, $mac) == 0) { |
---|
[332] | 2925 | print "Error: physical MAC address already exists: $mac\n"; |
---|
[221] | 2926 | next; |
---|
| 2927 | } |
---|
| 2928 | |
---|
| 2929 | #--- cette partie teste si le pool existe. |
---|
[238] | 2930 | if (not exists $computer_db->{'pool'}{$pool}) { |
---|
[332] | 2931 | print "Error: create pool with create_pool command before load database: $pool\n"; |
---|
[221] | 2932 | exit; |
---|
| 2933 | } |
---|
| 2934 | |
---|
[345] | 2935 | if ($computer_db->{'pool'}{'domain'} eq $sector) { |
---|
[221] | 2936 | my $timestamp = time; |
---|
[345] | 2937 | push @{$computer_db->{$sector}}, { $mac => { |
---|
[221] | 2938 | 'hostname' => $pool, |
---|
| 2939 | 'ip' => $pool, |
---|
| 2940 | 'address_type' => 'pool-dhcp', |
---|
| 2941 | 'enabled' => 'yes', |
---|
| 2942 | 'create_time' => $timestamp, |
---|
| 2943 | 'modify_time' => $timestamp, |
---|
| 2944 | }}; |
---|
| 2945 | } |
---|
| 2946 | else { |
---|
| 2947 | print "Ajout de la machine $mac [FAILED]\n"; |
---|
[345] | 2948 | print "Error: the pool doesn't exists: $pool, for the domain: $sector\n"; |
---|
[221] | 2949 | } |
---|
| 2950 | } |
---|
| 2951 | } |
---|
| 2952 | } |
---|
| 2953 | |
---|
| 2954 | #------------------------------------------------------------------------------- |
---|
| 2955 | |
---|
| 2956 | sub load_data_file { |
---|
[345] | 2957 | my ($sector, $input_file, $type_file) = @_; |
---|
[221] | 2958 | |
---|
[238] | 2959 | my $computer_db = ipamdb_load($COMPUTER_YAML); |
---|
| 2960 | |
---|
| 2961 | #$computer_db |
---|
[221] | 2962 | if ($type_file eq 'dhcp') { |
---|
[345] | 2963 | load_data_dhcp($sector, $input_file); |
---|
[221] | 2964 | } |
---|
| 2965 | |
---|
| 2966 | elsif ($type_file eq 'pool-dhcp') { |
---|
[345] | 2967 | load_data_pool($sector, $input_file); |
---|
[221] | 2968 | } |
---|
| 2969 | |
---|
[238] | 2970 | ipamdb_save("$COMPUTER_YAML", $computer_db); |
---|
[221] | 2971 | } |
---|
| 2972 | |
---|
| 2973 | #------------------------------------------------------------------------------- |
---|
| 2974 | |
---|
| 2975 | sub cmd_load_database { |
---|
| 2976 | local @ARGV = @_; |
---|
| 2977 | |
---|
| 2978 | my $help = get_cmd_name(); |
---|
[345] | 2979 | my ($sector, $input_file, $type_file); |
---|
[221] | 2980 | |
---|
| 2981 | GetOptions( |
---|
[346] | 2982 | 'sector|s|d=s' => \$sector, |
---|
[221] | 2983 | 'filename|f=s' => \$input_file, |
---|
| 2984 | 'kind|k=s' => \$type_file, |
---|
| 2985 | ); |
---|
| 2986 | |
---|
| 2987 | exit_on_error_option($help) |
---|
[345] | 2988 | if $sector eq '' |
---|
[221] | 2989 | or $input_file eq '' |
---|
| 2990 | or $type_file eq ''; |
---|
| 2991 | |
---|
[345] | 2992 | load_data_file($sector, $input_file, $type_file); |
---|
[221] | 2993 | } |
---|
| 2994 | |
---|
| 2995 | #------------------------------------------------------------------------------- |
---|
| 2996 | #Nom: backup_database |
---|
| 2997 | #Description: sauvegarde et réinitialise les fichiers d'enregistrements DHCP. |
---|
| 2998 | |
---|
| 2999 | sub backup_database { |
---|
| 3000 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime time; |
---|
| 3001 | $year += 1900; |
---|
| 3002 | $mon++; |
---|
| 3003 | my $date = sprintf '%04i-%02i-%02i-%02i-%02i-%02i', $year, $mon, $mday, $hour, $min, $sec; |
---|
| 3004 | |
---|
[332] | 3005 | copy($COMPUTER_YAML, "$FOLDER_BACKUP/$COMPUTER_BASENAME-$date.conf") or die "Error: database copy backup failed: $!\n"; |
---|
[221] | 3006 | } |
---|
| 3007 | |
---|
| 3008 | #------------------------------------------------------------------------------- |
---|
| 3009 | # HELP section |
---|
| 3010 | #------------------------------------------------------------------------------- |
---|
| 3011 | |
---|
| 3012 | #------------------------------------------------------------------------------- |
---|
| 3013 | #Nom: exit_on_error_option |
---|
| 3014 | #Description: messages d'aide des options pour les différentes commandes |
---|
| 3015 | |
---|
| 3016 | sub exit_on_error_option { |
---|
| 3017 | my ($command) = @_; |
---|
| 3018 | |
---|
| 3019 | if ($command eq 'add-dhcp') { |
---|
| 3020 | print "List of options for command: $command\n"; |
---|
[348] | 3021 | print " -s : sector attachment (mandatory). Example: -s legi-sector03\n"; |
---|
[221] | 3022 | print " -h : computer hostname (mandatory if option -i != 'pool'). Example: -h info8pc154\n"; |
---|
| 3023 | print " -m : physical MAC address (mandatory). Example: -m 0F:58:AB:2A\n"; |
---|
| 3024 | print " -i : internet IP address (mandatory). Possible value: classical IP address or the keyword 'pool'\n"; |
---|
| 3025 | print " -p : name of the DHCP pool to which the machine belongs (mandatory if option -i == 'pool')\n"; |
---|
| 3026 | print " -c : comment (mandatory). Example: 2014-04-07 DELL Laptop 6400 - Olivier Toto (INFO)\n"; |
---|
| 3027 | print "Example:\n"; |
---|
[348] | 3028 | print " ddt add-dhcp -h most1mc130 -s legi-sector03 -i 194.254.66.130 -m 00:17:F2:D3:2B:FF -c '2008-07-03 Mac Book Guillaume Balleyrac (MOST)\n"; |
---|
[221] | 3029 | } |
---|
| 3030 | |
---|
| 3031 | elsif ($command eq 'add-float') { |
---|
| 3032 | print "List of options for command: $command\n"; |
---|
[348] | 3033 | print " -s : sector attachment (mandatory)\n"; |
---|
[221] | 3034 | print " -p : name of the DHCP pool to which the machine belongs (mandatory)\n"; |
---|
| 3035 | print " -m : physical MAC address (mandatory)\n"; |
---|
| 3036 | print " -c : comment (mandatory). Example: 2014-04-07 DELL Laptop 6400 - Olivier Toto (INFO)\n"; |
---|
| 3037 | print "Example:\n"; |
---|
[348] | 3038 | print " ddt add-float -p pool-stagiaire -s legi-pool -i 192.168.10.1 -m 00:AB:1B:CC:AA:2F -c '2013-09-25 Dell OptiPlex 745 - Eric Goncalves (NRJ)\n"; |
---|
[221] | 3039 | } |
---|
| 3040 | |
---|
| 3041 | elsif ($command eq 'add-static') { |
---|
| 3042 | print "List of options for command: $command\n"; |
---|
[348] | 3043 | print " -s : sector attachment (mandatory)\n"; |
---|
[221] | 3044 | print " -i : internet IP address (mandatory)\n"; |
---|
| 3045 | print " -h : computer hostname (mandatory)\n"; |
---|
| 3046 | print " -m : physical MAC address (mandatory)\n"; |
---|
| 3047 | print " -c : comment (mandatory). Example: 2014-04-07 DELL Laptop 6400 - Olivier Toto (INFO)\n"; |
---|
| 3048 | print "Example:\n"; |
---|
[348] | 3049 | print " ddt add-static -h legipc1 -s legi-sector03 -i 192.168.10.1 -m 00:AB:1B:CC:AA:2F -c '2013-09-25 Dell OptiPlex 745 - Eric Goncalves (NRJ)\n"; |
---|
[221] | 3050 | } |
---|
| 3051 | |
---|
| 3052 | elsif ($command eq 'add-virtual') { |
---|
| 3053 | print "List of options for command: $command\n"; |
---|
[348] | 3054 | print " -s : sector attachment (mandatory)\n"; |
---|
[221] | 3055 | print " -i : internet IP address (mandatory)\n"; |
---|
| 3056 | print " -h : computer hostname (mandatory)\n"; |
---|
| 3057 | print " -c : comment (mandatory). Example: 2014-04-07 DELL Laptop 6400 - Olivier Toto (INFO)\n"; |
---|
| 3058 | print "Example:\n"; |
---|
[348] | 3059 | print " ddt add-virtual -h legipc1 -s legi-sector03 -i 192.168.10.1 -c '2013-09-25 Dell OptiPlex 745 - Eric Goncalves (NRJ)\n"; |
---|
[221] | 3060 | } |
---|
| 3061 | |
---|
| 3062 | elsif ($command eq 'add-alias') { |
---|
| 3063 | print "List of options for command: $command\n"; |
---|
[348] | 3064 | print " -s : sector attachment (mandatory)\n"; |
---|
[221] | 3065 | print " -h : computer hostname (mandatory)\n"; |
---|
| 3066 | print " -a : computer alias name (mandatory)\n"; |
---|
| 3067 | } |
---|
| 3068 | |
---|
[345] | 3069 | elsif ($command eq 'create-sector') { |
---|
[221] | 3070 | print "List of options for command: $command\n"; |
---|
[348] | 3071 | print " -s : new sector (mandatory)\n"; |
---|
[221] | 3072 | print " -e : DNS domain name extension( mandatory). Example legi.grenoble-inp.fr\n"; |
---|
| 3073 | print " -c : comment (mandatory). Example: 2016-08-22 VLAN legi-261 (INFO)\n"; |
---|
| 3074 | print "Examples:\n"; |
---|
[348] | 3075 | print " ddt create-sector -s legi-sector03 -e legi.grenoble-inp.fr -c '2016-08-22 VLAN legi-261 (INFO)'\n"; |
---|
[221] | 3076 | } |
---|
| 3077 | |
---|
| 3078 | elsif ($command eq 'create-pool') { |
---|
| 3079 | print "List of options for command: $command\n"; |
---|
| 3080 | print " -p : name of the DHCP pool. Example: pool-legi-priv\n"; |
---|
[348] | 3081 | print " -s : sector attachment for the pool. (sector attachment must exist in file $COMPUTER_BASENAME.conf). Example: legi-sector03\n"; |
---|
[221] | 3082 | print " -f : configuration filename on the DHCP server for the pool\n"; |
---|
| 3083 | print " -i : adresse(s) IP ou plage d'IP. Séparateur d'adresses IP: ','. Séparateur de plage '-'\n"; |
---|
| 3084 | print "Examples:\n"; |
---|
[348] | 3085 | print " ddt create-pool -p legi-pool1 -s legi-sector03 -f legi-pool-private -i 192.168.10.1,192.168.10.2,192.168.10.3\n"; |
---|
| 3086 | print " ddt create-pool -p legi-pool2 -s legi-sector03 -f legi-pool-public -i 192.168.10.1-192.168.10.4\n"; |
---|
[221] | 3087 | } |
---|
| 3088 | |
---|
| 3089 | elsif ($command eq 'create-pxe') { |
---|
| 3090 | print "List of options for command: $command\n"; |
---|
| 3091 | print " -b : name of the PXE/BOOTP configuration. Example: most\n"; |
---|
| 3092 | print " -n : internet IP address for the DHCP next-server.\n"; |
---|
| 3093 | print " -f : filename on TFTP server to load at boot\n"; |
---|
| 3094 | print " -c : comment (mandatory). Example: 2014-04-07 PXE Boot for CentOS (MOST)\n"; |
---|
| 3095 | } |
---|
| 3096 | |
---|
| 3097 | elsif ($command eq 'remove-pxe') { |
---|
| 3098 | print "List of options for command: $command\n"; |
---|
| 3099 | print " -b : name of the PXE/BOOTP configuration. Example: most\n"; |
---|
| 3100 | } |
---|
| 3101 | |
---|
| 3102 | elsif ($command eq 'enable-pxe') { |
---|
| 3103 | print "List of options for command: $command\n"; |
---|
| 3104 | print " -h : computer hostname (mandatory unless option -i)\n"; |
---|
| 3105 | print " -i : internet IP address (mandatory unless option -h)\n"; |
---|
[348] | 3106 | print " -s : sector attachment (mandatory if option -h)\n"; |
---|
[221] | 3107 | print " -b : name of the PXE/BOOTP configuration. Example: most\n"; |
---|
| 3108 | } |
---|
| 3109 | |
---|
| 3110 | elsif ($command eq 'disable-pxe') { |
---|
| 3111 | print "List of options for command: $command\n"; |
---|
| 3112 | print " -h : computer hostname (mandatory unless option -i)\n"; |
---|
| 3113 | print " -i : internet IP address (mandatory unless option -h)\n"; |
---|
[348] | 3114 | print " -s : sector attachment (mandatory if option -h)\n"; |
---|
[221] | 3115 | } |
---|
| 3116 | |
---|
| 3117 | elsif ($command eq 'create-tag') { |
---|
| 3118 | print "List of options for command: $command\n"; |
---|
| 3119 | print " -t : name of the TAG (mandatory). Example: restricted\n"; |
---|
| 3120 | print " -c : comment (mandatory). Example: 2014-04-07 tag restricted (INFO)\n"; |
---|
| 3121 | print "tag 'universal' is intrinsic\n"; |
---|
| 3122 | } |
---|
| 3123 | |
---|
| 3124 | elsif ($command eq 'remove-tag') { |
---|
| 3125 | print "List of options for command: $command\n"; |
---|
| 3126 | print " -b : name of the TAG. Example: restricted\n"; |
---|
| 3127 | } |
---|
| 3128 | |
---|
| 3129 | elsif ($command eq 'change-mac') { |
---|
| 3130 | print "List of options for command: $command\n"; |
---|
[348] | 3131 | print " -s : sector attachment (mandatory). Example: -s legi-sector03\n"; |
---|
[221] | 3132 | print " -h : computer hostname (mandatory unless option -i)\n"; |
---|
| 3133 | print " -i : internet IP address (mandatory unless option -h). Possible value: classical IP address or the keyword 'pool'\n"; |
---|
| 3134 | print " -m : physical MAC address (mandatory). Example: -m 0F:58:AB:2A:22:11\n"; |
---|
| 3135 | } |
---|
| 3136 | |
---|
| 3137 | elsif ($command eq 'change-ip') { |
---|
| 3138 | print "List of options for command: $command\n"; |
---|
[348] | 3139 | print " -s : sector attachment (mandatory). Example: -s legi-sector03\n"; |
---|
[221] | 3140 | print " -h : computer hostname (mandatory)\n"; |
---|
| 3141 | print " -i : new internet IP address (mandatory). Possible value: classical IP address\n"; |
---|
| 3142 | } |
---|
| 3143 | |
---|
| 3144 | elsif ($command eq 'change-host') { |
---|
| 3145 | print "List of options for command: $command\n"; |
---|
[348] | 3146 | print " -s : sector attachment (mandatory). Example: -s legi-sector03\n"; |
---|
[221] | 3147 | print " -i : internet IP address (mandatory). Possible value: classical IP address\n"; |
---|
| 3148 | print " -h : new computer hostname (mandatory)\n"; |
---|
| 3149 | print "It's not possible to change hostname for computer that belongs to a pool\n"; |
---|
| 3150 | } |
---|
| 3151 | |
---|
| 3152 | elsif ($command eq 'change-comment') { |
---|
| 3153 | print "List of options for command: $command\n"; |
---|
[348] | 3154 | print " -s : sector attachment (mandatory). Example: -s legi-sector03\n"; |
---|
[221] | 3155 | print " -m : physical MAC address (mandatory). Example: -m 0F:58:AB:2A:22:11\n"; |
---|
| 3156 | print " -c : new comment (mandatory)\n"; |
---|
| 3157 | } |
---|
| 3158 | |
---|
[345] | 3159 | elsif ($command eq 'change-sector') { |
---|
[221] | 3160 | print "List of options for command: $command\n"; |
---|
[348] | 3161 | print " -s : new sector attachment (mandatory). Example: -s legi-sector03\n"; |
---|
[221] | 3162 | print " -m : physical MAC address (mandatory). Example: -m 0F:58:AB:2A:22:11\n"; |
---|
| 3163 | print " -i : internet IP address (mandatory)\n"; |
---|
| 3164 | } |
---|
| 3165 | |
---|
| 3166 | elsif ($command eq 'change-tag') { |
---|
| 3167 | print "List of options for command: $command\n"; |
---|
| 3168 | print " -h : computer hostname (mandatory unless option -i or -m)\n"; |
---|
[348] | 3169 | print " -s : sector attachment (mandatory). Example: -s legi-sector03\n"; |
---|
[221] | 3170 | print " -i : internet IP address (mandatory unless option -h or -m)\n"; |
---|
| 3171 | print " -m : physical MAC address (mandatory unless option -h or -i, priority). Example: -m 0F:58:AB:2A:22:11\n"; |
---|
| 3172 | print " -t : list of tags separated by comma (mandatory). Example: -t internal,windows\n"; |
---|
| 3173 | } |
---|
| 3174 | |
---|
| 3175 | elsif ($command eq 'load-database') { |
---|
| 3176 | print "List of options for command: $command\n"; |
---|
[348] | 3177 | print " -s : sector attachment\n"; |
---|
[221] | 3178 | print " -f : input file in DHCP format\n"; |
---|
| 3179 | print " -k : possible cases (kind): dhcp, pool-dhcp, fix-address\n"; |
---|
| 3180 | } |
---|
| 3181 | |
---|
| 3182 | elsif ($command eq 'enable-pc') { |
---|
| 3183 | print "List of options for command: $command\n"; |
---|
| 3184 | print " -h : computer hostname (mandatory unless option -i)\n"; |
---|
| 3185 | print " -i : internet IP address (mandatory unless option -h)\n"; |
---|
[348] | 3186 | print " -s : sector attachment (mandatory if option -h)\n"; |
---|
[221] | 3187 | print "Examples:\n"; |
---|
[348] | 3188 | print " ddt enable-pc -i 192.168.10.1\n"; |
---|
| 3189 | print " ddt enable-pc -s legi-sector03 -h kevinpc\n"; |
---|
[221] | 3190 | } |
---|
| 3191 | |
---|
| 3192 | elsif ($command eq 'enable-float') { |
---|
| 3193 | print "List of options for command: $command\n"; |
---|
| 3194 | print " -m : physical MAC address (mandatory)\n"; |
---|
| 3195 | print " -p : name of the DHCP pool (mandatory)\n"; |
---|
| 3196 | } |
---|
| 3197 | |
---|
| 3198 | elsif ($command eq 'disable-float') { |
---|
| 3199 | print "List of options for command: $command\n"; |
---|
| 3200 | print " -m : physical MAC address (mandatory)\n"; |
---|
| 3201 | print " -p : name of the DHCP pool (mandatory)\n"; |
---|
| 3202 | } |
---|
| 3203 | |
---|
| 3204 | elsif ($command eq 'disable-pc') { |
---|
| 3205 | print "List of options for command: $command\n"; |
---|
| 3206 | print " -h : computer hostname (mandatory unless option -i)\n"; |
---|
| 3207 | print " -i : internet IP address (mandatory unless option -h)\n"; |
---|
[348] | 3208 | print " -s : sector attachment (mandatory if option -h)\n"; |
---|
[221] | 3209 | print "Examples:\n"; |
---|
[348] | 3210 | print " ddt disable-pc -i 192.168.10.1\n"; |
---|
| 3211 | print " ddt disable-pc -s legi-sector03 -h kevinpc\n"; |
---|
[221] | 3212 | } |
---|
| 3213 | |
---|
| 3214 | elsif ($command eq 'del-pc') { |
---|
| 3215 | print "List of options for command: $command\n"; |
---|
[348] | 3216 | print " -s : sector attachment (mandatory)\n"; |
---|
[221] | 3217 | print " -h : computer hostname (mandatory unless option -i)\n"; |
---|
| 3218 | print " -i : internet IP address (mandatory unless option -h)\n"; |
---|
| 3219 | } |
---|
| 3220 | |
---|
| 3221 | elsif ($command eq 'del-float') { |
---|
| 3222 | print "List of options for command: $command\n"; |
---|
| 3223 | print " -m : physical MAC address (mandatory)l\n"; |
---|
| 3224 | print " -p : name of the DHCP pool\n"; |
---|
| 3225 | } |
---|
| 3226 | |
---|
| 3227 | elsif ($command eq 'search-mac') { |
---|
| 3228 | print "List of options for command: $command\n"; |
---|
| 3229 | print " -m : physical MAC address (mandatory). Example: -m 0F:58:AB:2A:22:11\n"; |
---|
| 3230 | } |
---|
| 3231 | |
---|
[353] | 3232 | elsif ($command eq 'sector-add-ip') { |
---|
| 3233 | print "List of options for command: $command\n"; |
---|
| 3234 | print " -s : sector attachment (mandatory)\n"; |
---|
| 3235 | print " -i : internet IP range address in CIDR notation (mandatory)\n"; |
---|
| 3236 | } |
---|
| 3237 | |
---|
[221] | 3238 | else { |
---|
| 3239 | print "No help for command: $command\n"; |
---|
| 3240 | } |
---|
| 3241 | exit; |
---|
| 3242 | } |
---|
| 3243 | |
---|
| 3244 | #------------------------------------------------------------------------------- |
---|
| 3245 | |
---|
| 3246 | sub cmd_version { |
---|
| 3247 | |
---|
| 3248 | print <<'END'; |
---|
[224] | 3249 | ddt - management of computer names and IP addresses |
---|
[303] | 3250 | Copyright (C) 2006-2018, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
| 3251 | Main author Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr> |
---|
[221] | 3252 | License GNU GPL version 2 or later and Perl equivalent |
---|
| 3253 | END |
---|
| 3254 | |
---|
[303] | 3255 | print "Database Version 1\n"; |
---|
[221] | 3256 | print "Version $VERSION\n\n"; |
---|
[227] | 3257 | print ' $Id: ddt 362 2018-09-14 20:13:58Z g7moreau $'."\n"; |
---|
[221] | 3258 | return; |
---|
| 3259 | } |
---|
| 3260 | |
---|
| 3261 | #------------------------------------------------------------------------------- |
---|
| 3262 | #Nom: usage |
---|
| 3263 | #Description: message d'aide sur les commandes du script |
---|
| 3264 | |
---|
| 3265 | sub cmd_help { |
---|
| 3266 | print <<END; |
---|
[224] | 3267 | ddt - management of computer names and IP addresses |
---|
[221] | 3268 | |
---|
[347] | 3269 | ddt add-alias [--hostname|-h hostname] [--sector|-s|-d sector] [--alias|-a alias] |
---|
| 3270 | ddt add-dhcp [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] [--comment|-c comment] |
---|
| 3271 | ddt add-float [--pool|-p pool] [--sector|-s|-d sector] [--mac|-m mac] [--comment|-c comment] |
---|
| 3272 | ddt add-static [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] [--comment|-c comment] |
---|
| 3273 | ddt add-virtual [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--comment|-c comment] |
---|
| 3274 | ddt change-comment [--sector|-s|-d sector] [--mac|-m mac] [--comment|-c comment] |
---|
| 3275 | ddt change-sector [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] |
---|
| 3276 | ddt change-host [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
| 3277 | ddt change-ip [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
| 3278 | ddt change-mac [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] |
---|
| 3279 | ddt change-tag [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] [--tag|-t tag] |
---|
[342] | 3280 | ddt check-dns [--direct] [--reverse] |
---|
[347] | 3281 | ddt create-sector [--sector|-s|-d sector] [--dns-extension|-e dns_extension] [--comment|-c comment] |
---|
| 3282 | ddt create-pool [--pool|-p pool] [--sector|-s|-d sector] [--file-pool|-f file_pool] [--ipaddress-pool|-i ipaddress_pool] |
---|
[224] | 3283 | ddt create-pxe [--bootp|-b pxe_config] [--next-server|-n next_server] [--filename|-f filename] [--comment|-c comment] |
---|
| 3284 | ddt create-tag [--tag|-t tag] [--comment|-c comment] |
---|
| 3285 | ddt del-float [--pool|-p pool] [--mac|-m mac] |
---|
[347] | 3286 | ddt del-pc [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
| 3287 | ddt disable-pc [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
[224] | 3288 | ddt disable-float [--pool|-p pool] [--mac|-m mac] |
---|
[347] | 3289 | ddt disable-pxe [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
[224] | 3290 | ddt enable-float [--pool|-p pool] [--mac|-m mac] |
---|
[347] | 3291 | ddt enable-pc [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
| 3292 | ddt enable-pxe [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--bootp|-b pxe_config] |
---|
[224] | 3293 | ddt gen-dhcp-file |
---|
[337] | 3294 | ddt gen-dns-file [--verbose] |
---|
[224] | 3295 | ddt help |
---|
[347] | 3296 | ddt load-database [--sector|-s|-d sector] [--filename|-f filename] [--kind|-k kind] |
---|
[224] | 3297 | ddt remove-pxe [--bootp|-b pxe_config] |
---|
| 3298 | ddt remove-tag [--tag|-t tag] |
---|
| 3299 | ddt search-mac [--mac|-m mac] |
---|
[353] | 3300 | ddt sector-add-ip [--sector|-s|-d sector] [--ip-range|-i ip_cidr] |
---|
[345] | 3301 | ddt show-sector [--no-header|-H] |
---|
[224] | 3302 | ddt show |
---|
| 3303 | ddt show-pool [--no-header|-H] |
---|
| 3304 | ddt show-pxe [--no-header|-H] |
---|
| 3305 | ddt show-tag [--no-header|-H] |
---|
| 3306 | ddt version |
---|
[221] | 3307 | |
---|
| 3308 | COMMANDS |
---|
| 3309 | |
---|
[353] | 3310 | * add-alias : add an alias for a computer (like CNAME for the DNS) |
---|
| 3311 | * add-dhcp : add a computer with a fix DHCP IP or in a DHCP pool |
---|
| 3312 | * add-float : add a computer with an IP in a DHCP pool |
---|
| 3313 | * add-static : add a computer with a static IP |
---|
| 3314 | * add-virtual : add a virtual computer with a static IP but a virtual MAC (useful to declare float computer in DNS) |
---|
| 3315 | * change-comment : change the computer comment |
---|
| 3316 | * change-sector : change the sector attachment for a computer |
---|
| 3317 | * change-host : change the computer hostname |
---|
| 3318 | * change-ip : change the computer IP address |
---|
| 3319 | * change-mac : change the computer physical MAC address |
---|
| 3320 | * change-tag : change the list of TAGs associated to a computer |
---|
| 3321 | * check-dns : check the DNS table for base IPs |
---|
| 3322 | * create-sector : create a new sector |
---|
| 3323 | * create-pool : create a new pool for DHCP records |
---|
| 3324 | * create-pxe : create a new PXE/BOOTP configuration |
---|
| 3325 | * create-tag : create a new TAG |
---|
| 3326 | * del-float : remove a computer from a DHCP pool |
---|
| 3327 | * del-pc : remove a computer (DHCP or static IP) from the YAML database |
---|
| 3328 | * disable-pc : disable a computer (DHCP and/or DNS) (but keep it in the database) |
---|
| 3329 | * disable-float : disable a computer from a DHCP pool (but keep it in the database) |
---|
| 3330 | * disable-pxe : remove PXE/BOOTP configuration on a computer |
---|
| 3331 | * enable-float : enable a previous disable computer (DHCP and/or DNS) |
---|
| 3332 | * enable-pc : enable a previous disable computer (DHCP and/or DNS) |
---|
| 3333 | * enable-pxe : enable PXE/BOOTP configuration on a computer |
---|
| 3334 | * gen-dhcp-file : generate DHCP files for the isc DHCP server |
---|
| 3335 | * gen-dns-file : generate DNS files for the bind domain server |
---|
| 3336 | * help : this help |
---|
| 3337 | * load-database : load the YAML database (be careful) |
---|
| 3338 | * remove-pxe : remove a PXE/BOOTP configuration |
---|
| 3339 | * remove-tag : remove a TAG |
---|
| 3340 | * search-mac : search physical MAC address computer |
---|
| 3341 | * ddt sector-add-ip : add IP range check on a sector |
---|
| 3342 | * show-sector : list all sector group of computer |
---|
| 3343 | * show : list all computers |
---|
| 3344 | * show-pool : list all pool |
---|
| 3345 | * show-pxe : list PXE/BOOTP configuration |
---|
| 3346 | * show-tag : list all TAGs |
---|
| 3347 | * version : return program version |
---|
[221] | 3348 | END |
---|
| 3349 | return; |
---|
| 3350 | } |
---|
| 3351 | |
---|
| 3352 | ################################################################ |
---|
| 3353 | # documentation |
---|
| 3354 | ################################################################ |
---|
| 3355 | |
---|
| 3356 | __END__ |
---|
| 3357 | |
---|
| 3358 | =head1 NAME |
---|
| 3359 | |
---|
[224] | 3360 | ddt - management of computer names and IP addresses |
---|
[221] | 3361 | |
---|
[228] | 3362 | |
---|
[221] | 3363 | =head1 USAGE |
---|
| 3364 | |
---|
[347] | 3365 | ddt add-alias [--hostname|-h hostname] [--sector|-s|-d sector] [--alias|-a alias] |
---|
| 3366 | ddt add-dhcp [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] [--comment|-c comment] |
---|
| 3367 | ddt add-float [--pool|-p pool] [--sector|-s|-d sector] [--mac|-m mac] [--comment|-c comment] |
---|
| 3368 | ddt add-static [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] [--comment|-c comment] |
---|
| 3369 | ddt add-virtual [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--comment|-c comment] |
---|
| 3370 | ddt change-comment [--sector|-s|-d sector] [--mac|-m mac] [--comment|-c comment] |
---|
| 3371 | ddt change-sector [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] |
---|
| 3372 | ddt change-host [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
| 3373 | ddt change-ip [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
| 3374 | ddt change-mac [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] |
---|
| 3375 | ddt change-tag [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] [--tag|-t tag] |
---|
[342] | 3376 | ddt check-dns [--direct] [--reverse] |
---|
[347] | 3377 | ddt create-sector [--sector|-s|-d sector] [--dns-extension|-e dns_extension] [--comment|-c comment] |
---|
| 3378 | ddt create-pool [--pool|-p pool] [--sector|-s|-d sector] [--file-pool|-f file_pool] [--ipaddress-pool|-i ipaddress_pool] |
---|
[224] | 3379 | ddt create-pxe [--bootp|-b pxe_config] [--next-server|-n next_server] [--filename|-f filename] [--comment|-c comment] |
---|
| 3380 | ddt create-tag [--tag|-t tag] [--comment|-c comment] |
---|
| 3381 | ddt del-float [--pool|-p pool] [--mac|-m mac] |
---|
[347] | 3382 | ddt del-pc [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
| 3383 | ddt disable-pc [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
[224] | 3384 | ddt disable-float [--pool|-p pool] [--mac|-m mac] |
---|
[347] | 3385 | ddt disable-pxe [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
[224] | 3386 | ddt enable-float [--pool|-p pool] [--mac|-m mac] |
---|
[347] | 3387 | ddt enable-pc [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
| 3388 | ddt enable-pxe [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--bootp|-b pxe_config] |
---|
[224] | 3389 | ddt gen-dhcp-file |
---|
[337] | 3390 | ddt gen-dns-file [--verbose] |
---|
[224] | 3391 | ddt help |
---|
[347] | 3392 | ddt load-database [--sector|-s|-d sector] [--filename|-f filename] [--kind|-k kind] |
---|
[224] | 3393 | ddt remove-pxe [--bootp|-b pxe_config] |
---|
| 3394 | ddt remove-tag [--tag|-t tag] |
---|
| 3395 | ddt search-mac [--mac|-m mac] |
---|
[353] | 3396 | ddt sector-add-ip [--sector|-s|-d sector] [--ip-range|-i ip_cidr] |
---|
[345] | 3397 | ddt show-sector [--no-header|-H] |
---|
[224] | 3398 | ddt show |
---|
| 3399 | ddt show-pool [--no-header|-H] |
---|
| 3400 | ddt show-pxe [--no-header|-H] |
---|
| 3401 | ddt show-tag [--no-header|-H] |
---|
| 3402 | ddt version |
---|
[221] | 3403 | |
---|
[228] | 3404 | |
---|
| 3405 | =head1 DESCRIPTION |
---|
| 3406 | |
---|
| 3407 | DDT is an acronym for DHCP-DNS-Tools. |
---|
| 3408 | The previous command name was not C<ddt> but just C<dhcp-dns-tools>... |
---|
| 3409 | In practise, DDT is an IP Address Management (IPAM) service. |
---|
| 3410 | It has been used in the LEGI laboratory for over 10 years. |
---|
| 3411 | |
---|
| 3412 | The tool is quite effective and tries to keep things simple |
---|
| 3413 | but easily configurable for your site like a swiss army knife. |
---|
| 3414 | Everything is saved in a YAML database |
---|
| 3415 | and entries could be added, deleted, or modified by the command line. |
---|
| 3416 | |
---|
| 3417 | |
---|
[221] | 3418 | =head1 COMMANDS |
---|
| 3419 | |
---|
[235] | 3420 | =head2 add-alias |
---|
[221] | 3421 | |
---|
[347] | 3422 | ddt add-alias [--hostname|-h hostname] [--sector|-s|-d sector] [--alias|-a alias] |
---|
[235] | 3423 | |
---|
| 3424 | =head2 add-dhcp |
---|
| 3425 | |
---|
[347] | 3426 | ddt add-dhcp [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] [--comment|-c comment] |
---|
[235] | 3427 | |
---|
| 3428 | =head2 add-float |
---|
| 3429 | |
---|
[347] | 3430 | ddt add-float [--pool|-p pool] [--sector|-s|-d sector] [--mac|-m mac] [--comment|-c comment] |
---|
[235] | 3431 | |
---|
| 3432 | =head2 add-static |
---|
| 3433 | |
---|
[347] | 3434 | ddt add-static [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] [--comment|-c comment] |
---|
[235] | 3435 | |
---|
| 3436 | =head2 add-virtual |
---|
| 3437 | |
---|
[347] | 3438 | ddt add-virtual [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--comment|-c comment] |
---|
[235] | 3439 | |
---|
| 3440 | =head2 change-comment |
---|
| 3441 | |
---|
[347] | 3442 | ddt change-comment [--sector|-s|-d sector] [--mac|-m mac] [--comment|-c comment] |
---|
[235] | 3443 | |
---|
[345] | 3444 | =head2 change-sector |
---|
[235] | 3445 | |
---|
[347] | 3446 | ddt change-sector [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] |
---|
[235] | 3447 | |
---|
| 3448 | =head2 change-host |
---|
| 3449 | |
---|
[347] | 3450 | ddt change-host [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
[235] | 3451 | |
---|
| 3452 | =head2 change-ip |
---|
| 3453 | |
---|
[347] | 3454 | ddt change-ip [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
[235] | 3455 | |
---|
| 3456 | =head2 change-mac |
---|
| 3457 | |
---|
[347] | 3458 | ddt change-mac [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] |
---|
[235] | 3459 | |
---|
| 3460 | =head2 change-tag |
---|
| 3461 | |
---|
[347] | 3462 | ddt change-tag [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--mac|-m mac] [--tag|-t tag] |
---|
[235] | 3463 | |
---|
| 3464 | =head2 check-dns |
---|
| 3465 | |
---|
[342] | 3466 | ddt check-dns [--direct] [--reverse] |
---|
[235] | 3467 | |
---|
[345] | 3468 | =head2 create-sector |
---|
[235] | 3469 | |
---|
[347] | 3470 | ddt create-sector [--sector|-s|-d sector] [--dns-extension|-e dns_extension] [--comment|-c comment] |
---|
[235] | 3471 | |
---|
| 3472 | =head2 create-pool |
---|
| 3473 | |
---|
[347] | 3474 | ddt create-pool [--pool|-p pool] [--sector|-s|-d sector] [--file-pool|-f file_pool] [--ipaddress-pool|-i ipaddress_pool] |
---|
[235] | 3475 | |
---|
| 3476 | =head2 create-pxe |
---|
| 3477 | |
---|
| 3478 | ddt create-pxe [--bootp|-b pxe_config] [--next-server|-n next_server] [--filename|-f filename] [--comment|-c comment] |
---|
| 3479 | |
---|
| 3480 | =head2 create-tag |
---|
| 3481 | |
---|
| 3482 | ddt create-tag [--tag|-t tag] [--comment|-c comment] |
---|
| 3483 | |
---|
| 3484 | =head2 del-float |
---|
| 3485 | |
---|
| 3486 | ddt del-float [--pool|-p pool] [--mac|-m mac] |
---|
| 3487 | |
---|
| 3488 | =head2 del-pc |
---|
| 3489 | |
---|
[347] | 3490 | ddt del-pc [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
[235] | 3491 | |
---|
| 3492 | =head2 disable-pc |
---|
| 3493 | |
---|
[347] | 3494 | ddt disable-pc [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
[235] | 3495 | |
---|
| 3496 | =head2 disable-float |
---|
| 3497 | |
---|
| 3498 | ddt disable-float [--pool|-p pool] [--mac|-m mac] |
---|
| 3499 | |
---|
| 3500 | =head2 disable-pxe |
---|
| 3501 | |
---|
[347] | 3502 | ddt disable-pxe [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
[235] | 3503 | |
---|
| 3504 | =head2 enable-float |
---|
| 3505 | |
---|
| 3506 | ddt enable-float [--pool|-p pool] [--mac|-m mac] |
---|
| 3507 | |
---|
| 3508 | =head2 enable-pc |
---|
| 3509 | |
---|
[347] | 3510 | ddt enable-pc [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] |
---|
[235] | 3511 | |
---|
| 3512 | =head2 enable-pxe |
---|
| 3513 | |
---|
[347] | 3514 | ddt enable-pxe [--hostname|-h hostname] [--sector|-s|-d sector] [--ip|-i ip] [--bootp|-b pxe_config] |
---|
[235] | 3515 | |
---|
| 3516 | =head2 gen-dhcp-file |
---|
| 3517 | |
---|
| 3518 | ddt gen-dhcp-file |
---|
| 3519 | |
---|
| 3520 | =head2 gen-dns-file |
---|
| 3521 | |
---|
[337] | 3522 | ddt gen-dns-file [--verbose] |
---|
[235] | 3523 | |
---|
| 3524 | =head2 help |
---|
| 3525 | |
---|
| 3526 | ddt help |
---|
| 3527 | |
---|
| 3528 | =head2 load-database |
---|
| 3529 | |
---|
[347] | 3530 | ddt load-database [--sector|-s|-d sector] [--filename|-f filename] [--kind|-k kind] |
---|
[235] | 3531 | |
---|
| 3532 | =head2 remove-pxe |
---|
| 3533 | |
---|
| 3534 | ddt remove-pxe [--bootp|-b pxe_config] |
---|
| 3535 | |
---|
| 3536 | =head2 remove-tag |
---|
| 3537 | |
---|
| 3538 | ddt remove-tag [--tag|-t tag] |
---|
| 3539 | |
---|
| 3540 | =head2 search-mac |
---|
| 3541 | |
---|
| 3542 | ddt search-mac [--mac|-m mac] |
---|
| 3543 | |
---|
[353] | 3544 | =head2 sector-add-ip |
---|
| 3545 | |
---|
| 3546 | ddt sector-add-ip [--sector|-s|-d sector] [--ip-range|-i ip_cidr] |
---|
| 3547 | |
---|
[345] | 3548 | =head2 show-sector |
---|
[235] | 3549 | |
---|
[345] | 3550 | ddt show-sector [--no-header|-H] |
---|
[235] | 3551 | |
---|
| 3552 | =head2 show |
---|
| 3553 | |
---|
| 3554 | ddt show |
---|
| 3555 | |
---|
| 3556 | =head2 show-pool |
---|
| 3557 | |
---|
| 3558 | ddt show-pool [--no-header|-H] |
---|
| 3559 | |
---|
| 3560 | =head2 show-pxe |
---|
| 3561 | |
---|
| 3562 | ddt show-pxe [--no-header|-H] |
---|
| 3563 | |
---|
| 3564 | =head2 show-tag |
---|
| 3565 | |
---|
| 3566 | ddt show-tag [--no-header|-H] |
---|
| 3567 | |
---|
| 3568 | =head2 version |
---|
| 3569 | |
---|
| 3570 | ddt version |
---|
| 3571 | |
---|
| 3572 | |
---|
[221] | 3573 | =head1 AUTHORS |
---|
| 3574 | |
---|
[303] | 3575 | Written by Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>, Kevin Reverchon, Olivier De-Marchi - Grenoble - France |
---|
[221] | 3576 | |
---|
| 3577 | |
---|
| 3578 | =head1 LICENSE AND COPYRIGHT |
---|
| 3579 | |
---|
[303] | 3580 | License GNU GPL version 2 or later and Perl equivalent |
---|
[221] | 3581 | |
---|
[303] | 3582 | Copyright (C) 2006-2018, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|