Files
pxe_boot_alma/work/KickStart/cfg/ifFix.pl
T
andersenandClaude Fable 5 6d398b826a refactor: move tree under homelab/, add alma10 hardening and kickstart docs
- restructure boot/kickstarts/scripts/docs into homelab/
- docs: alma9->10 breaking changes, CIS hardening, kickstart changes
- add find_cron_entries.sh helper and codex running memory notes

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-06 22:35:07 -04:00

189 lines
6.1 KiB
Perl

#!/usr/bin/perl
#
# Copyright InfoStructure Solutions Inc 2011
# All rights reserved. No part of this doucment may be reproduced in any
# form without the written permission of the copyright holder.
#
# $Id: ifFix.pl,v 1.1 2011/11/23 16:24:59 wia Exp $
#
use Getopt::Long;
my $dmi = "dmidecode -t 41";
my $upath = "/sys/class/net";
my $udev = "udevadm info --query=property --path=$upath";
my $rfile = "/etc/udev/rules.d/70-persistent-net.rules";
my $cfile = "/etc/sysconfig/network-scripts/ifcfg-";
die "Usage: $0 [-d] <-w>\n" if ( !GetOptions("d:1", "w", "h") || $opt_h );
sub debug (@) { warn shift || $_, "\n" if $opt_d; }
if ( -e $rfile) {
if ( open(RULES, $rfile) ) {
my @rules = <RULES>;
close RULES;
if ( open(DMI, "$dmi |") ) {
if ( opendir(UDIR, $upath) ) {
my ($macs, $ifs, $onb);
my $idx = 0;
foreach my $if ( grep(/^eth/, readdir(UDIR)) ) {
my $ifcfg = "$cfile$if";
if ( -e $ifcfg ) {
$interfaces->{$if} = $ifcfg;
debug "Found interface config '$ifcfg'";
} else {
warn "Config for interface $if does not exist\n";
}
}
closedir UDIR;
my $y;
while ( <DMI> ) {
if (/Type:\s+(\w+)/) {
$y = ($1 =~ /Ethernet/) ? 1 : 0;
} elsif ($y && /Bus Address:\s+(\S+)/) {
my $if = "eth".$idx++;
if ( exists $interfaces->{$if} ) {
debug "Found onboard eth \@ bus '$1' and setting to $if";
$onb->{$1} = $if;
$y = undef;
} else {
warn "Found onboard eth \@ bus '$1' but there was no interface $if\n";
}
}
}
close DMI;
foreach my $if ( keys %{$interfaces} ) {
if ( open(UDEV, "$udev/$if |") ) {
debug "Properties for interface $if:";
my ($path, $bus, $mac);
while ( <UDEV> ) {
if ( /^DEVPATH=(\S+)/ ) {
$path = $1;
} elsif ( /^NET_MATCHID=(\S+)/ ) {
$bus = $1;
} elsif ( /^MATCHADDR=(\S+)/ ) {
$mac = $1;
}
}
if ( $mac && $bus ) {
$ifs->{$if} = ( exists $onb->{$bus} ) ? $onb->{$bus} : "eth".$idx++;
($macs->{$ifs->{$if}} = $mac) =~ tr/a-f/A-F/;
debug " bus = $bus, mac = $mac, path = $path -> new if = $ifs->{$if}";
} else {
warn "Could not find mac($mac) and/or bus($bus) for interface $if\n";
}
close UDEV;
} else {
warn "Could not get properties for '$if'\n";
}
}
if ( $opt_w ) {
if ( open(RULES, ">$rfile") ) {
debug "Openned '$rfile for write'";
select RULES;
} else {
warn "Could not open rules file '$rfile' for writing\n";
}
} else {
print "\ncat <<EOF >$rfile\n";
}
foreach my $line ( @rules ) {
chomp $line;
if ( $line !~ /^\s*(:?#.*)?$/ ) {
my $newLine;
foreach my $par ( split(/\s*,\s*/, $line ) ) {
if ( $par =~ /^NAME="([^"]+)"/ ) {
if ( exists $ifs->{$1} ) {
if ( $ifs->{$1} eq $1 ) {
$newLine = undef;
last;
} else {
$par = qq|NAME="$ifs->{$1}"|;
}
}
}
$newLine = ($newLine) ? join(', ', $newLine, $par) : $par;
}
if ( $newLine ) {
print "# $line\n";
print "$newLine\n"
} else {
print "$line\n";
}
} else {
print "$line\n";
}
}
if ( $opt_w ) {
close RULES;
select STDOUT;
} else {
print "EOF\n" unless $opt_w;
}
foreach my $if ( keys %{$interfaces} ) {
if ( open(RI, $interfaces->{$if} ) ) {
@cfg = <RI>;
close RI;
} else {
warn "Could not read ifcfg '$interfaces->{$if}\n";
}
if ( $opt_w ) {
if ( open(WI, ">$interfaces->{$if}") ) {
debug "Openned '$interfaces->{$if} for write'";
select WI;
} else {
warn "Could not open ifcfg '$interfaces->{$if}' for writing\n";
}
} else {
print "\ncat <<EOF >$interfaces->{$if}\n";
}
foreach my $line ( @cfg ) {
if ( $line =~ /HWADDR="?([^\s"]+)/ ) {
debug "Check $1 =? $macs->{$if}\n";
if ( $1 eq $macs->{$if} ) {
print $line;
} else {
print "# $line";
print qq|HWADDR="$macs->{$if}"\n|;
}
} else {
print $line;
}
}
if ( $opt_w ) {
close WI;
select STDOUT;
} else {
print "EOF\n" unless $opt_w;
}
}
} else {
warn "Could not read '$upath'\n";
}
} else {
warn "Could not run '$dmi'\n";
}
close RULES;
} else {
warn "Could not open '$rules'\n";
}
} else {
warn "No rules file '$rules'\n";
}