blob: 444ab236ede179fd4d2c9a632997d8f3a610013d [file] [log] [blame]
#! /bin/sh
if [ "$#" -lt 2 ]
then
echo "Not enough parameters!"
echo "Usage: ./release_computer <machine_name> <user_name>"
echo " example: ./release_computer vcla1-7 guest"
exit 0
fi
name=$1
len=`expr length $name`
pos=`expr index "$name" -`
num=${name:$pos:$len}
num=`expr $num + 20`
wanip=152.1.14.$num
if [ $2 == "root" ]
then
echo -e "\nERROR: CANNOT DELETE USER 'root'!!!"
echo -e "Nothing can be done! Exit.\n"
exit 1
fi
echo -e "\nWorking...."
# check if <machine_name> is valid
if [ -z "`nodels | grep -w $1`" ]
then
echo -e "\nERROR : '$1' - there is no such machine!"
echo -e "Nothing can be done! Exit.\n"
exit 1
else
echo "Check: Machine '$1' exists. Good!"
fi
# check if machine is ON
if [ "`rpower $1 stat | grep -w on`" ]
then
echo "Check: Machine '$1' is ON. Good!"
else
if [ "`rpower $1 stat | grep -w off`" ]
then
echo -e "\nERROR: Machine '$1' is OFF!"
else
echo -e "\nERROR: Cannot determine whether '$1' is ON or OFF!"
echo "ERROR INFO: (result of 'rpower $1 stat'): `rpower $1 stat`"
fi
echo -e "Nothing can be done! Exit.\n"
exit 1
fi
# check if machine IS on local network
if [ "`pping $1 | grep -w noping`" ]
then
echo -e "\nERROR: Machine '$1' is NOT on local network!"
echo "ERROR INFO: (result of 'pping $1'): `pping $1`"
echo -e "Nothing can be done! Exit.\n"
exit 1
else
echo "Check: Machine '$1' IS on local network. Good!"
fi
# check if machine is available
# i.e. NOT in the middle of something (like creating image, re-loading OS, etc.)
case $( nodeset $1 stat | gawk 'split($0, a, " ") {print a[2]}' ) in
install )
echo -e "\nERROR: Machine '$1' is in the moddle of re-loading OS!"
echo "ERROR INFO: (result of 'nodeset $1 stat'): `nodeset $1 stat`"
echo "Please try again later."
echo -e "Exit. Nothing was done!\n"
exit 1
;;
image )
echo -e "\nERROR: Machine '$1' is in the moddle of creating image of OS!"
echo "ERROR INFO: (result of 'nodeset $1 stat'): `nodeset $1 stat`"
echo "Please try again later."
echo -e "Exit. Nothing was done!\n"
exit 1
;;
boot )
echo "Check: Machine '$1' is available. Good!" ;;
* )
echo -e "\nERROR: Machine '$1' is in UKNOWN state!"
echo "ERROR INFO: (result of 'nodeset $1 stat'): `nodeset $1 stat`"
echo -e "Nothing can be done! Exit.\n"
exit 1
;;
esac
# check if machine is accessible
if [ -z "`psh $1 uname -r`" ]
then
echo -e "\nERROR: Machine '$1' is NOT accessible!"
echo "ERROR INFO: possibly SSH deamon is NOT running on '$1'"
echo -e "Nothing can be done! Exit.\n"
exit 1
else
echo "Check: Machine '$1' is accessible. Good!"
fi
# determine what OS
os=`ssh $1 uname -s`
if [ $os == "CYGWIN_NT-5.1" ]
then
os="WinXP"
fi
if [ $os == "CYGWIN_NT-5.2" ]
then
os="Win2003"
fi
ver=`ssh $1 uname -r`
case $os in
Linux )
echo "Check: Machine '$1' is Linux machine ($ver). Good!" ;;
WinXP )
echo "Check: Machine '$1' is WindowsXP machine. Ok." ;;
Win2003 )
echo "Check: Machine '$1' is Windows2003 machine. Ok." ;;
* )
echo "ERROR: Cannot determine OS of '$1'!"
echo "ERROR INFO: (result of 'ssh $1 uname -s'): `ssh $1 uname -s`"
exit 1
;;
esac
#if [ $os == "Linux" ]
#then
# echo -e "\nThis script temporarily works only with Windows machines. Sorry!"
# echo -e "Exit. Nothing was done!\n"
# exit 0
#fi
# check if <user_name> actually exists on this machine
if [[ $os == "Win2003" || $os == "WinXP" ]]
then
if [ -z "`ssh $1 ls | grep -w list_users.vbs`" ]
then
echo -e "\nERROR: missing script: 'list_users.vbs'"
echo "ERROR: cannot continue! Exit."
exit 1
fi
fi
user_already_exists="yes"
if [[ $os == "Win2003" || $os == "WinXP" ]]
then
# Windows
if [ -z "`ssh $1 cscript.exe //Nologo list_users.vbs | grep -w $2`" ]
then
user_already_exists="no"
fi
else
# Linux
if [ -z "`ssh $1 cat /etc/passwd | grep -w $2`" ]
then
user_already_exists="no"
fi
fi
# delete user if exists
if [ $user_already_exists == "no" ]
then
echo -e "\nWARNING: user '$2' does NOT exists on $1 !"
echo "Do you want just disable WAN network interface? (y or n)"
read ans
if [ $ans != "y" ]
then
echo -e "Exit. Nothing was done!\n"
exit 0
fi
else
if [[ $os == "Win2003" || $os == "WinXP" ]]
then
# Windows
ssh $1 cscript.exe //Nologo del_user.vbs $2 $3
else
# Linux
ssh $1 "userdel $2" > /dev/null
fi
fi
# check if <user_name> DOES NOT exists NOW on this machine
user_now_exists="no"
if [[ $os == "Win2003" || $os == "WinXP" ]]
then
# Windows
if [ -n "`ssh $1 cscript.exe //Nologo list_users.vbs | grep -w $2`" ]
then
user_now_exists="yes"
fi
else
# Linux
if [ -n "`ssh $1 cat /etc/passwd | grep -w $2`" ]
then
user_now_exists="yes"
fi
fi
if [ $user_now_exists == "no" ]
then
echo "Check: User '$2' does NOT exist now. Good!"
else
echo -e "\nERROR: Could NOT delete user '$2'!"
# echo "ERROR INFO: check 'del_user.vbs' script."
echo -e "\n Cannot continue! Exit.\n"
exit 1
fi
# Disable WAN interface
#echo -n "Disabling WAN interface..."
if [[ $os == "Win2003" || $os == "WinXP" ]]
then
# Windows
if [ -z "`ssh $1 ls | grep -w disWAN.vbs`" ]
then
echo -e "\nERROR: Missing script: 'disWAN.vbs'"
echo "ERROR: Cannot continue! Exit."
exit 1
fi
ssh $1 cscript.exe //Nologo disWAN.vbs
else
# Linux
if [ -z "`ssh $1 ifconfig | grep -w $wanip`" ]
then
echo "WAN is already disabled!"
else
touch ifcfg-eth1.tmp
for ln in `ssh $1 cat /etc/sysconfig/network-scripts/ifcfg-eth1`
do
if [ -n "`echo $ln | grep -w ONBOOT`" ]
then
echo "ONBOOT=no" >> ifcfg-eth1.tmp
else
echo $ln >> ifcfg-eth1.tmp
fi
done
scp -q ifcfg-eth1.tmp $1:/etc/sysconfig/network-scripts/ifcfg-eth1 > /dev/null
psh $1 "service network restart" > /dev/null
rm -f ifcfg-eth1.tmp
fi
fi
#echo ""
# check if WAN was actually disabled
if [[ $os == "Win2003" || $os == "WinXP" ]]
then
if [ -n "`ssh $1 ipconfig | grep -w $wanip`" ]
then
echo -e "\nERROR: Can still ping WAN interface ($wanip)!"
echo -e "ERROR: Cannot continue! Exit.\n"
exit 1
fi
fi
if [ $os == "Linux" ]
then
if [ -n "`ssh $1 ifconfig | grep -w $wanip`" ]
then
echo -e "\nERROR: Can still ping WAN interface ($wanip)!"
echo -e "ERROR: Cannot continue! Exit.\n"
exit 1
fi
fi
echo -e "Check: WAN interface is disabled now. Good!\n"
if [ -z $ans ]
then
echo -e "\n User '$2' was DELETED on machine '$1'."
fi
echo " Machine '$1' is OFF-LINE now."
echo " Thank you!"
echo ""