Wednesday, May 5, 2010

Managing Users With Puppet from the Command Line

While in the process of deploying Puppet, I had the sudden need to manage some users' passwords. Since I already had the software installed, I thought ralsh would be the easiest way to handle the job. I could have scripted a bunch of ssh/awk/sed/etc commands, but that just didn't seem very robust. Sure, it'd have worked fine back in 1999, but this is Two Thousand Freaking Ten.

Here's the script I wrote.

WARNING: You'll need to disable history or otherwise remove the shell history file after relogging.

#!/usr/bin/env bash
function manage_user()
{
user=$1
password=$2
shift; shift;
out=`ralsh user $user password='$password' ensure=present`
echo $out | grep 'notice.*changed password'
return $?
}
user=$1
password=$2
if [ -z $user ]
then
echo "What user?"
exit 1
fi
if [ -z $password ]
then
echo "What encrypted password?"
exit 1
fi
id $user >& /dev/null
case "$?" in
0)
manage_user $user $password
if [ $? -eq 0 ]
then
echo "Changed password for $user"
exit 0
else
echo "Didn't change password for $user"
exit 1
fi
;;
1) echo "User doesn't exist!"; exit 1 ;;
*) echo "There was a problem verifying user's existence!"; exit 1 ;;
esac
exit 0

Easily force PXE boot in Linux without IPMI

Sometimes I need to PXE boot a box for reinstall and don't have easy access to the BMC. Sometimes I don't want to try to race the BIOS prompt for PXE booting. What can I say, I have a short attention span, er, other things to do while the BIOS posts.

One option I could use is ipmitool, but I like to just overwrite the MBR with dd.

Here's the command I use:

dd if=/dev/zero of=/dev/sda bs=512 count=1