Spacer http://macenterprise.org MacResource.org - Mac OS X enterprise deployment project Spacer
Site Map Contact Us Top Background
 
Search
 
 
updateByHostPrefs 4.1 E-mail
Written by MacEnterprise   
Wednesday, 23 March 2005
Leon Towns-von Stauber has provided this perl script login hook for Mac OS X that renames files in a user's Library/Preferences/ByHost/ folder with the MAC address of the host being logged into.

It's useful when initially setting up a user's account, as you can create the user template home directory with default preference files containing a placeholder in the filename, and they will dynamically be renamed according to the host(s) used by a user.

The latest version of his script can be downloaded at http://www.occam.com/tools/

#!/usr/bin/perl -w
#
# updateByHostPrefs - Update ByHost preference filenames with local MAC address.
#
# Copyright (c) 2002-2003 Occam's Razor. All rights reserved.
#
# See the LICENSE file distributed with this code for restrictions on its use
# and further distribution.
# Original distribution available at .
#
# $Id: updateByHostPrefs.pl,v 4.1 2003/08/14 09:03:47 leonvs Exp $
#
# TODO
#
use strict;
use Sys::Syslog qw(:DEFAULT setlogsock);


# Set up some global variables used for logging.
my $DEBUG = 0;
my $LOG_FAC = "user";
my $progname = $0; $progname =~ s/.*\///;


my $username = shift @ARGV or report("ERR", "Need a username argument.");
@ARGV and report("WARN", "Too many arguments.");
my (undef, undef, $uid, undef, undef, undef, undef, $homeDir, undef) =
getpwnam $username or
report("ERR", "$username is not a valid username.");
my $byHostDir = "$homeDir/Library/Preferences/ByHost";
report("DEBUG", "byHostDir is $byHostDir.");


$> == 0 or report("ERR", "Trying to run as UID $>, must be run as root.");
# Switch user, to accomodate shared homedirs where root is mapped to nobody.
$> = $uid;


# Get "primary" MAC address listed by ifconfig.
my $macAddr=`/sbin/ifconfig en0 | /usr/bin/grep ether | /usr/bin/cut -d" " -f2`;
$macAddr =~ s/://g;
chomp $macAddr;
report("DEBUG", "macAddr is $macAddr.");


report("INFO", "Updating for $username with MAC address of $macAddr.");


opendir BYHOSTDIR, $byHostDir or report("ERR", "Can't open $byHostDir.");
my @prefFiles = grep /\.plist$/, readdir BYHOSTDIR;
closedir BYHOSTDIR;


foreach my $file (@prefFiles) {
my $newFile = $file;
$newFile =~ s/^(.*)\.[^.]*\.plist$/$1.$macAddr.plist/;
$file = "$byHostDir/$file";
$newFile = "$byHostDir/$newFile";
unless (-e $newFile) {
report("DEBUG", "Changing $file to $newFile.");
rename $file, $newFile or
report("WARN", "Couldn't change $file to $newFile.");
}
}


sub report {
my ($severity, $message) = @_;
my $output = "$severity: $message\n";


setlogsock "unix";
openlog $progname, "cons, pid", $LOG_FAC;
if ($severity eq "DEBUG") { syslog "debug", "$output" if $DEBUG; }
elsif ($severity eq "INFO") { syslog "info", "$output"; }
elsif ($severity eq "WARN") { syslog "warning", "$output"; }
elsif ($severity eq "ERR") { syslog "err", "$output"; exit 0; }
else { syslog "err", "Undefined severity $output"; }
closelog;
return 1;
}

Last Updated ( Wednesday, 23 March 2005 )