Wednesday, June 17, 2009

Mac OS X - Advanced Computer(Re)Name via MySQL (Extended)

No comments (condensed) version:
http://codesnippets.joyent.com/posts/show/2155



#!/bin/bash
# Script: mysql_computername.sh
# Version: 1.0
# Description: Rename computers dynamically on system startup.
# Variables are pulled from a MySQL database for
# easier management.
#
# Developed by: Mark Carver © 2009
# Email: mark_carver@jsd.k12.ak.us
# Website: http://oldgreenhorn.blogspot.com

export PATH=${PATH}:/usr/local/mysql/bin

# MySQL Configuration
mysql_host='localhost' # Domain, IP
mysql_user='ReadOnlyUser' # Preferably use a user that only has read only access as the password
mysql_password='YoUrPaSsWoRd' # is visible in this script.
mysql_db='database_name'
mysql_connection="mysql -s -N -w --connect_timeout=5 -h ${mysql_host} -u ${mysql_user} --password=${mysql_password} ${mysql_db}"

# Test for connection to host. Computer needs to start network connections on startup, otherwise this script would often fail.
# Time is calculated in minutes by multiplying the sleep (number of seconds it waits before trying again) and while loops
# (number of loops it will complete before failing) then dividing by 60 (seconds).
# 10 seconds x 30 loops = 300 seconds / 60 seconds = 5 minutes
i=0
while [ $i -lt 30 ]
do
if eval "ping -c 1 -q ${mysql_host}"; then
internet='connected'
break
else
sleep 10
let i+=1
fi
done

if [ "${internet}" == 'connected' ]; then

# Test for connection to MySQL database. Read above for instructions on changing timout settings.
m=0
while [ $m -lt 30 ]
do
if eval "${mysql_connection} -e \"SHOW DATABASES\""; then
mysql_status='connected'
break
else
sleep 10
let m+=1
fi
done

if [ "${mysql_status}" == 'connected' ]; then

# Pull variables from the MySQL server

# ${template}: Naming scheme that allows you to dynamically change how the computer name is displayed.
# When storing the value in the MySQL database, you must use single quotes ( ' ) around
# any metacharacters ( | & ; ( ) < > space tab) that is used in your naming scheme.
# Not doing so will break the script. -------------------See below for more details.
template=`${mysql_connection} -e "SELECT value FROM settings WHERE attribute='template'"`

# ${mac}: Pulls the MAC address of this computer and searches for it on the MySQL database.
# It then parses out the ":" and changes the case from lower to UPPER. 00:1f:f3:d0:a5:60 > 001FF3D0A560
# You may need to modify this line to suit how you MAC addresses are stored in your database.
# To keep the colons (:) in, remove the following: | tr -d ':'
# To keep the case lower, remove the following: | tr 'a-z' 'A-Z'
mac=`ifconfig en0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | tr -d ':' | tr 'a-z' 'A-Z'`
# DEBUG: Uncomment the line below and replace the MAC address with one that is currently in your
# MySQL database. This will allow you to run this script locally to smooth out any settings.
# Make sure you comment it back or remove the line before you deploy, otherwise you'll get a
# nasty surpise ;)
mac="001FF3D0A560"

# ${exists}: Checks the MySQL database for a record that matches the MAC address
exists=`${mysql_connection} -e "SELECT ethernet FROM computers WHERE ethernet='${mac}'"`

if [ "${exists}" != '' ]; then

############################################### Custom Variables ###############################################
# Here is where you will create your own variables depending on what you wish to display for the computer name. #

fas=`${mysql_connection} -e "SELECT fas FROM computers WHERE ethernet='${mac}'"`
last_name=`${mysql_connection} -e "SELECT last_name FROM users WHERE fas='${fas}'"`
first_name=`${mysql_connection} -e "SELECT first_name FROM users WHERE fas='${fas}'"`
school=`${mysql_connection} -e "SELECT school FROM computers WHERE fas='${fas}'"`
team=`${mysql_connection} -e "SELECT team FROM users WHERE fas='${fas}'"`
cart=`${mysql_connection} -e "SELECT cart FROM computers WHERE fas='${fas}'"`
slot=`${mysql_connection} -e "SELECT slot FROM computers WHERE fas='${fas}'"`
if [ "${cart}" != "" ] && [ "${slot}" != "" ]
then
fas="${cart}${slot}-${fas}"
fi

if [ "${last_name}" != "" ] || [ "${first_name}" != "" ]; then
lastname_first="- ${last_name}, ${first_name}"
else
lastname_first=""
fi

# #
############################################## End Custom Variables #############################################

# Takes your MySQL template string and evaluates it to include the variables. Example of the value that's stored:
# ${school} ${team} ${lastname_first} '('${fas}')'
# Any metacharacters will need to be put in single quotation. You must also put the EXACT variable for this to work.
# Otherwise it will just spit out garbage.
computer_name=`eval echo ${template}`

# Takes the compiled computer name and condenses any extra spaces for variables that may have been blank.
# Then formats the computer name to an appropriate LocalHostName with lower case, no metacharacters, and only one hyphen :)
computer_name=`echo ${computer_name} | tr -s " "`
local_hostname=`echo ${computer_name} | tr -s "[:punct:]" "-" | tr -c -s '\12\60-\172' "-" | tr 'A-Z' 'a-z' | sed 's/-$//'`

# And finally, we can safely set the computer name and local host name!
scutil --set ComputerName "${computer_name}"
scutil --set LocalHostName "${local_hostname}"
exit 0
else
logger -s "The MAC address: ${mac} does not exist in the database. Will try again next startup/login."
exit 0
fi
else
logger -s "MySQL connection timeout reached. Failed connecting to MySQL database: ${mysql_db} at ${mysql_host}. Will try again next startup/login."
fi
else
logger -s "Host connection timeout reached. Failed connecting to host: ${mysql_host}. Will try again next startup/login."
fi

No comments: