Script Prototypes

From Nuclear Physics Group Documentation Pages
Revision as of 17:46, 20 July 2007 by Minuti (talk | contribs)
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`;