Script Prototypes

From Nuclear Physics Group Documentation Pages
Revision as of 14:35, 25 July 2007 by Steve (talk | contribs) (→‎setres)
Jump to navigationJump to search

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. Or something along those lines.