2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-09-21 16:39:30 +00:00
Files
xcat-core/xCAT-server/share/xcat/tools/dhcpop
T
2026-04-23 19:19:29 -03:00

149 lines
4.3 KiB
Perl
Executable File

#!/usr/bin/perl -w
#use Data::Dumper;
use strict;
use warnings;
BEGIN
{
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
}
use lib "$::XCATROOT/lib/perl";
use Getopt::Long;
use Fcntl ':flock';
use xCAT::DHCP::Backend;
sub usage{
print "Usage: dhcphelper -h \n";
print "\n";
print " dhcphelper -r|--rm -m|--mac <mac address> [ -a|--ip <ip address>] [ -n|--name <node name>]\n";
print " delete the dhcp lease of specified <mac>,<ip address> and <node name>\n";
print "\n";
return;
}
my $help;
my $rmop;
my $mac;
my $ip;
my $hostname;
GetOptions ("m|mac=s" => \$mac, # numeric
"a|ip=s" => \$ip, # string
"n|name=s" => \$hostname, # string
"r|rm" => \$rmop, # flag
"h|help" => \$help) # flag
or &usage;
if($help){
&usage;
exit 0;
}elsif($rmop){
my $backend = xCAT::DHCP::Backend->new_backend();
if (ref($backend) ne 'HASH' && $backend->name eq 'kea') {
if ($mac && $mac !~ /:/) {
$mac = lc($mac);
$mac =~ s/(\w{2})/$1:/g;
$mac =~ s/:$//;
}
mkdir "/tmp/xcat" unless -d "/tmp/xcat";
open(my $dhcplockfd, ">", "/tmp/xcat/dhcplock") or die "Unable to lock DHCP state: $!"; ## no critic (InputOutput::RequireBriefOpen)
flock($dhcplockfd, LOCK_EX);
my $config4 = $backend->load_dhcp4_config();
if ($config4->{error}) {
print "Error: $config4->{error}\n";
exit 1;
}
my $config6;
my $have_dhcp6 = -e $backend->dhcp6_config_file();
if ($have_dhcp6) {
$config6 = $backend->load_dhcp6_config();
if ($config6->{error}) {
print "Error: $config6->{error}\n";
exit 1;
}
}
my @matches;
push @matches, { hostname => $hostname } if $hostname;
push @matches, { 'hw-address' => lc($mac) } if $mac;
push @matches, { 'ip-address' => $ip } if $ip;
foreach my $match (@matches) {
$backend->delete_reservations($config4, $match);
$backend->delete_reservations($config6, $match) if $config6;
}
my $result = $backend->write_dhcp4_json($backend->encode_config($config4));
if ($result->{error}) {
print "Error: $result->{error}\n";
exit 1;
}
if ($config6) {
my $result6 = $backend->write_dhcp6_json($backend->encode_config($config6));
if ($result6->{error}) {
print "Error: $result6->{error}\n";
exit 1;
}
}
my $restart = $backend->restart_services(ipv6 => $config6 ? 1 : 0);
if ($restart->{error}) {
print "Error: $restart->{error}\n";
exit 1;
}
exit 0;
}
my $out=qx(tabdump -w key==omapi -w username==xcat_key passwd |tail -n1|awk -F, '{print \$2","\$3}');
$out =~ s/("|\n)//g;
my ($id,$passwd)=split(',',$out);
if(-z "$id" || -z "$passwd" ){
print "Error: no 'omapi' entry defined in passwd table!";
exit 1;
}
my $omshell_commands = "key " . $id . " \"" . $passwd . "\"\n";
$omshell_commands .= "connect\n";
if($hostname){
$omshell_commands .= "new host\n";
$omshell_commands .= "set name = \"$hostname\"\n"; #Find and destroy conflict name
$omshell_commands .= "open\n";
$omshell_commands .= "remove\n";
$omshell_commands .= "close\n";
}
if ($mac)
{
$omshell_commands .= "new host\n";
$omshell_commands .= "set hardware-address = " . $mac . "\n"; #find and destroy mac conflict
$omshell_commands .= "open\n";
$omshell_commands .= "remove\n";
$omshell_commands .= "close\n";
}
if($ip){
$omshell_commands .= "new host\n";
$omshell_commands .= "set ip-address = $ip\n"; #find and destroy ip conflict
$omshell_commands .= "open\n";
$omshell_commands .= "remove\n";
$omshell_commands .= "close\n";
}
my $omshell;
open($omshell, '|-', '/bin/sh', '-c', '/usr/bin/omshell >/dev/null')
or die "Unable to start omshell: $!";
print $omshell $omshell_commands;
close($omshell);
}else{
&usage;
exit 1;
}
#print "$mac-$ip-$hostname\n"
exit 0;