Scripting /etc/fstab changes
David Tonhofer
d.tonhofer at m-plify.com
Tue Dec 5 22:08:26 UTC 2006
Danda, Richard SFC wrote:
> Greetings all,
>
> I have a NetApp FAS270 filer that I have migrated NFS data directories too.
> I need to make changes to all my RHEL machines to point to the new device
> for mounts. Is there an easy way to script the change?
>
>
YOU WANT US TO DO YOUR HOMEWORK?!
Try this perl script, it's probably unelegant as hell but I found it in
my "hacks" directory.
No guarantees!!! Try it first manually, then set reallyWrite to 1 to
replace your fstab.
Fix the regular expressions for your case.
----------8<-------------------------8<-------------------------8<------------------
#!/usr/bin/perl
# Program that can be used to replace stuff in some file
$file = $ARGV[0];
if (!defined $file) {
die "You have to pass the name of the file to modify on the
commandline\n";
}
if (!(-f $file && -r $file && -w $file)) {
die "The file '$file' is not accessible or not writeable\n";
}
open(FILE,"<$file") or die "Could not open '$file' for reading: $!\n";
@buffer = (); # Takes up the file lines that we will write eventually
while ($line = <FILE>) {
chomp($line);
if ($line =~ /^hostA\:(.*)$/) {
push(@buffer,"hostB:$1");
next;
}
else {
# just keep the line as is
push(@buffer,$line);
next;
}
if ($line ne $buffer[@buffer-1]) {
print STDERR "Line changed\n";
print STDERR "From: $line\n";
print STDERR "To: $buffer[@buffer-1]\n";
}
}
close(FILE) or die "Could not close '$file': $!\n";
# Now we will write, replacing the existing file (if reallyWrite is set
to 1)
$reallyWrite = 0;
if ($reallyWrite) {
open(FILE,">$file") or die "Could not open '$file' for writing: $!\n";
foreach $line (@buffer) {
print FILE "$line\n";
}
close(FILE) or die "Could not close '$file': $!\n";
}
else {
foreach $line (@buffer) {
print "$line\n";
}
}
More information about the redhat-list
mailing list