Script Prototypes
setres
Bash function for remotely setting /etc/resolv.conf to the proper values:
function setres { home=~ master="$home/bin/master_resolv.conf" scp $master root@$1:/etc/resolv.conf }
Would be better if the function checked to see if the file didn't exist, and if not, created it. Or maybe it would be better to have the "master" file on a specific machine. Actually, even better would be a program that maps settings to files.
Here's a shot at it. Doesnt' check to see if it exists or not, but handles the copying and can play with as many different types of config files as we want. Just add to the hash table as necessary. Need to be able to login as root on destination machine from source machine.
#!/usr/bin/perl -w #Matthew Minuti --last updated 7/20/07 # This is a program to push master config files to specific NPG machines. # If no or one arguments given, print usage and exit. if(not $ARGV[0] or not $ARGV[1]){ print "\n Usage: replaceconfig.pl config host\n where config is a settings file and host is the host to check\n E.g. replaceconfig.pl resolv.conf improv to act on improv's /etc/resolv.conf\n\n"; exit; } # Defining scp for copying my $scp="/usr/bin/scp"; # Set up a hash table so that we know where each config file master is, and # where the copied file should end up. my %settings = ( "resolv.conf" => "/etc/resolv.conf /etc/resolv.conf.npgmaster", "nsswitch.conf" => "/etc/nsswitch.conf /etc/nsswitch.conf.npgmaster", ); # See if we know about the passed config argument, and if not, whine and exit. if(not $settings{$ARGV[0]}){ print "\nSorry, $ARGV[0] isn't defined in the settings list. Bye!\n"; exit; } # Find out what config file we're concerned with and format the locations # appropriately. my ($writeloc, $sourceloc) = split " ", $settings{$ARGV[0]}; # Set up another variable to avoid perl interpreting the .'s in filenames # as concatenation $scpargs = "$sourceloc $ARGV[1]:$writeloc"; # Send a copy of the master where it belongs! `$scp $scpargs`;
I've decided that using a hash table isn't as good a way to do it as I previously thought. Doing it that way requires that both the source code and the files be modified when a configuration is added, and complicates the code. Instead, master files should be kept in a known location (maybe /etc/npg-master or something). The program will take a filename and host, and try to open and copy "master-filename" to the same location on host. Something along these lines:
#!/usr/bin/perl -w use strict; #Steve McCoy # Program for pushing master config files to machines sub masterloc($); sub badloc($); $ARGV[0] or die "need a filename\n"; $ARGV[1] or die "need a hostname\n"; my $master = masterloc($ARGV[0]); my $bad = badloc($ARGV[0]); my $scp = "/usr/bin/scp"; my $args = "$master $ARGV[1]:$bad"; `$scp $args`; sub masterloc($) { my $ml = "/etc/npg-master/$_[0]"; -e $ml or die "no master file \"$ml\"\n"; return $ml; } sub badloc($) { return "/etc/$_[0]"; }