|
Automatic Computer Naming via DNS |
|
|
Written by Sam Agnew
|
|
Wednesday, 09 November 2005 |
|
This is a script we employ on our Radmind-managed machines as well as our unmanaged staff machines.
I know there are other solutions for naming computers based upon a text file on a web server etc. but since we already have static IPs and DNS I thought it would be easier to use that. Hence the solution I came up with for us here at Weill Cornell Medical College in Qatar.
Instructions:
- Copy cornellnamer.plist to /Library/LaunchDaemons
- Create /Library/AdminItems and copy cornellNamer.sh to this directory
- Edit the dnsbase variable as appropriate for your domain
The script assumes that you have static IPs and a DNS that names each computer uniquely (in our case the name matches the asset tag for the machine)
cornellNamer.sh:
#! /bin/sh
#
# Cornell Computer Namer
# Modifications by
# Sam Agnew
# Weill Cornell Medical College in Qatar
# January 22, 2004
#
# Based upon
# Panther Post-Image Setup SPRING 2004
# ComputerName
# January 9, 2004
#
# Now launched via launchd
# Now using scutil
# Now using qatar-med.cornell.edu
# Sam Agnew
# July 31, 2005
# Minor cleanup for release November 9, 2005
PATH=/bin:/sbin/:/usr/bin/:/usr/sbin
test=$(ifconfig -a inet 2>/dev/null | sed -n -e '/127.0.0.1/d' -e '/0.0.0.0/d' -e '/inet/p' | wc -l)
# Max wait for network in seconds / 2
limit=60
# Set domain name base here
dnsbase=".qatar-med.cornell.edu"
# Set Initial Name and try to make it unique
TEMPNAME="Waiting-for-network${RANDOM}"
scutil --set ComputerName $TEMPNAME
scutil --set LocalHostName $TEMPNAME
# Wait for network
X=0
while [ $test != 1 ]
do
echo Network not currently up
test=$(ifconfig -a inet 2>/dev/null | sed -n -e '/127.0.0.1/d' -e '/0.0.0.0/d' -e '/inet/p' | wc -l)
X=$((X+1))
if [ $X -ge $limit ]; then
break
fi
sleep 2
done
HWADDRESS=`ifconfig en0 | grep ether | awk '{print $2}'`
IPADDRESS=`ifconfig en0 | grep inet\ | awk '{print $2}'`
COMPUTERNAME=$(host 207.162.244.165 | awk '{ print $5 }' | sed s/${dnsbase}.//)
###### IF COMPUTERNAME IS BLANK, SET NAME TO HWADDRESS, OTHERWISE SET TO VALUE CALCULATED FROM DNS LOOKUP ######
if [ "$COMPUTERNAME" = "no" ]; then
NONAME="`echo $HWADDRESS`-DHCP-Error"
scutil --set ComputerName $NONAME
scutil --set LocalHostName $NONAME
elif [ "$COMPUTERNAME" = "" ]; then
NONAME="`echo $HWADDRESS`-Network-Connection-Failure"
scutil --set ComputerName $NONAME
scutil --set LocalHostName $NONAME
elif [ "$COMPUTERNAME" = "3(NXDOMAIN)" ]; then
NONAME="`echo $HWADDRESS`-No-DNS-Record-For-Host"
scutil --set ComputerName $NONAME
scutil --set LocalHostName $NONAME
else
scutil --set ComputerName $COMPUTERNAME
scutil --set LocalHostName $COMPUTERNAME
fi
if [ $(who|grep console|wc -l) == 0 ]; then
if [ $(ps wwaux|grep WaitingForLoginWindow|grep -v grep|wc -l) == 0 ]; then
killall loginwindow
fi
fi
killall syslogd
cornellnamer.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>edu.cornell.qatar-med.cornellcomputernamer</string>
<key>ServiceDescription</key>
<string>Names computer based upon DNS resolution</string>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/Library/AdminItems/cornellNamer.sh</string>
</array>
</dict>
</plist>
|
|
Last Updated ( Tuesday, 15 November 2005 )
|