51ak.blogs

BigBrother服务器端管理脚本_Bash

一段脚本
用于完成增删改BB客户机时对服务端的修改工作
可以中英文切换

#! /bin/bash
#This File can add/modify/del the bbhost
#Some paths may need to be modify
#And Some errors may occur
#By Wokofo 2010-08-19
#LANG=en_US.UTF8
#LANG=zh_CN.GB18030 去掉此注释,界面显示中文

dir_bbhost='/home/bbuser/bb/etc/bb-hosts'
dir_bbhtml='/home/bbuser/bb/bb/html/'
dir_bblogs='/home/bbuser/bbvar/logs/'
dir_bbhist='/home/bbuser/bbvar/hist/'
dir_filebak="/home/zhangwenjing/bbfilebak/"
dir_hostbak="/home/zhangwenjing/bbhostbak/"
dir_filebak_html=${dir_filebak}html/
dir_filebak_logs=${dir_filebak}logs/
dir_filebak_hist=${dir_filebak}hist/


if [ $LANG == "zh_CN.GB18030" ]; then
#命令解释--中文
str_main1="欢迎使用BB管理脚本"
str_main2="请选择一条命令"
str_main3="1.增加一台BB主机"
str_main4="2.修改一台BB的主机名"
str_main5="3.修改一台BB的主机名_新"
str_main6="4.删除一台BB主机"
str_main7="0.退出\n"
str_main8=" 请输入您的选择:"
str_main9="错误的输入,请重试!"
str_main0="程序不能继续运行,请确认!"
str_init0="初始化参数完成!"
str_init1="初始化参数失败"
str_add0=" 当前选择:添加一台新BB主机"
str_add1=" 请输入主机名:"
str_add2=" 请输入主机ip:"
str_add3="你输入的主机名是"
str_add4=" 主机ip地址是"
str_add5="条记录已存在,无法添加,请确认!"
str_add6="HOST文件修改成功"
str_add7="重启BB服务"
str_add8="添加完成!"
str_add9="HOST文件修改失败,请手工检查"	
str_modi0="修改一台BB主机的别名"
str_modi1=" 请输入原主机名:"
str_modi2=" 请输入新主机名:"
str_modi3="你输入的原主机名是"
str_modi4=" 新主机名是"
str_modi5="原主机名不存在,请确认"
str_modi6="新主机名已存在,请确认"
str_modi7="HOST文件修改成功"
str_modi8="停止BB服务"
str_modi9="文件移动成功"
str_modi10="文件改名失败"
str_modi11="启动BB服务"
str_modi12="修改完成!"
str_modi13="HOST文件修改失败,请手工检查"
str_del0="删除一台BB主机"
str_del1=" 请输入主机名:"
str_del2="你输入的原主机名是
str_del3="主机名不存在,请确认"
str_del4="HOST文件修改成功"
str_del5="停止BB服务"
str_del6="文件删除成功"
str_del7="文件删除失败"
str_del8="启动BB服务"
str_del9="修改完成!"
str_del10="HOST文件修改失败,请手工检查"
str_newmodi0="修改一台BB主机的别名_新"
else

str_main2='Please select a command' 
str_main1='Welcome to the BB management script' 
str_main3='1. Add a BB host ' 
str_main4='2. Modify a hosts name of BB ' 
str_main5='3. Modify a hosts name of BB_NEW' 
str_main6='4. Remove a BB host ' 
str_main7='0. exit \n ' 
str_main8='Please enter your choice:' 
str_main9='Wrong input, please try again!' 
str_main0='program can not continue to run, make sure your input! ' 
str_init0='initialization parameters to complete!' 
str_init1='initialization parameter failed' 
str_add0='current choice: Add a new BB host' 
str_add1='Please enter the host name:' 
str_add2='Please enter the host ip:' 
str_add3='the host name is' 
str_add4='host ip address' 
str_add5='Record already exists, cann not add, make sure!' 
str_add6='HOST File edited successfully' 
str_add7='restart the BB services' 
str_add8='Add completed!' 
str_add9='HOST file modification fails, manually check' 
str_modi0='modify a host alias BB' 
str_modi1='Please enter the original host name:' 
str_modi2='Please enter a new host name:' 
str_modi3='You entered the original host name is' 
str_modi4='new host name is' 
str_modi5='original host name does not exist, make sure' 
str_modi6='new host name already exists, make sure' 
str_modi7='HOST File edited successfully' 
str_modi8='Stop BB service' 
str_modi9='file mobile success' 
str_modi10='file rename failed' 
str_modi11='start the BB service' 
str_modi12='editing!' 
str_modi13='HOST file modification fails, manually check' 
str_del0='delete a BB host' 
str_del1='Please enter the host name:' 
str_del2='your host name is the original input' 
str_del3='host name does not exist, make sure' 
str_del4='HOST File edited successfully' 
str_del5='Stop BB service' 
str_del6='file deleted successfully' 
str_del7='file deletion failed' 
str_del8='start the BB service' 
str_del9='editing!' 
str_del10='HOST file modification fails, manually check' 
str_newmodi0='BB modify a host alias NEW' 
fi




f_init()
{
if [ -d $dir_bbhtml ] && [ -d $dir_bblogs ] && [ -d $dir_bbhist ] && [ -d $dir_filebak ] && [  -d $dir_hostbak ] &&  [ -f $dir_bbhost ]
then
	if [ ! -d "${dir_filebak_html}" ] ; then	
		mkdir ${dir_filebak_html}
	fi
	if [ ! -d "${dir_filebak_logs}" ] ; then	
		mkdir ${dir_filebak_logs}
	fi
	if [ ! -d "${dir_filebak_hist}" ] ; then	
		mkdir ${dir_filebak_hist}
	fi
	echo ${str_init0}
	return 0
else
	echo ${str_init1}
	return 1
fi
}

f_add()
{
 echo -n ${str_add0}
if [ -z $hostname ]; then
	echo -n  ${str_add1}
	read hostname
fi
if [ -z $hostip ]; then
	echo -n ${str_add2}
	read hostip
fi
echo ${str_add3}[${hostname}]${str_add4}[${hostip}]
result=`cat  /home/bbuser/bb/etc/bb-hosts  |egrep -n  "\(${hostip}|${hostname}\)[[:space:]]" |wc -l `
if [ $result -ne 0 ];then
	echo $result${str_add5}
	cat  /home/bbuser/bb/etc/bb-hosts  |egrep -n  "\(${hostip}|${hostname}\)[[:space:]]"
	return 1;
fi
newhostname=${dir_hostbak}bb-hosts`date +%Y%m%d_%H%M`
cp -f $dir_bbhost $newhostname
echo -e  "$hostip\t$hostname # BBNET" >>${newhostname}
cp -f $dir_bbhost ${newhostname}.bak
sudo cp -f ${newhostname} $dir_bbhost
if [ $? -eq 0 ]; then
	echo ${str_add6}
	echo ${str_add7}
	sudo /etc/init.d/BigBrother stop
	sudo /etc/init.d/BigBrother start
	echo ${str_add8}
else
	echo ${str_add9}
fi
}



f_modify()
{
 echo -n ${str_modi0}
if [ -z ${hostname} ]; then
	echo -n  ${str_modi1}
	read hostname
fi
if [ -z ${hostip} ]; then
	echo -n ${str_modi2}
	read hostip
fi
echo ${str_modi3}[${hostname}] ${str_modi4}[${hostip}]
result=`cat  /home/bbuser/bb/etc/bb-hosts  |egrep -n  "[[:space:]]${hostname}[[:space:]]" |wc -l `
if [ $result -eq 0 ];then
	echo ${str_modi5}
	return 1;
fi
result=`cat  /home/bbuser/bb/etc/bb-hosts  |egrep -n  "[[:space:]]${hostip}[[:space:]]" |wc -l `
if [ $result -ne 0 ];then
	echo ${str_modi6}
	return 1;
fi
newhostname=${dir_hostbak}bb-hosts`date +%Y%m%d_%H%M`
cp -f ${dir_bbhost} ${newhostname}.bak
sed "s/\t${hostname} /\t${hostip} /" ${dir_bbhost} >${newhostname}
sudo cp -f ${newhostname} ${dir_bbhost}
if [ $? -eq 0 ]; then
	echo ${str_modi7}	
	echo ${str_modi8}
	sudo /etc/init.d/BigBrother stop	
	for loop in disk cpu mem msgs netprotl pagefile procs svcs 
	do
	if [ -f "${dir_bbhtml}${hostname}.${loop}.html" ]; then	
		sudo mv "${dir_bbhtml}${hostname}.${loop}.html" "${dir_bbhtml}${hostip}.${loop}.html"  
		if [ $? -eq 0 ]; then 
			echo "${loop}${str_modi9}"
		else
			echo "${loop}${str_modi10}"
		fi
	fi
	done

	echo ${str_modi11}
	sudo /etc/init.d/BigBrother start
	echo ${str_modi12}
else
	echo ${str_modi13}	
fi
}




f_del()
{
echo -n ${str_del0}
if [ -z $hostname ]; then
	echo -n  ${str_del1}
	read hostname
fi
echo ${str_del2}[$hostname]
result=`cat  /home/bbuser/bb/etc/bb-hosts  |egrep -n  "[[:space:]]$hostname[[:space:]]" |wc -l `
if [ $result -eq 0 ];then
	echo ${str_del3}
fi
newhostname=${dir_hostbak}bb-hosts`date +%Y%m%d_%H%M`
cp -f $dir_bbhost ${newhostname}.bak
sed "/\t${hostname} /"d $dir_bbhost >${newhostname}
sudo cp -f ${newhostname} $dir_bbhost
if [ $? -eq 0 ]; then
	echo ${str_del4}
	echo ${str_del5}
	sudo /etc/init.d/BigBrother stop
cd ${dir_bb_html}
sudo find -name "${hostname}.*" -exec rm {} \;
cd ${dir_bblogs}
sudo find -name "${hostname}.*" -exec rm {} \;
cd ${dir_bbhist}
sudo find -name "${hostname}.*" -exec rm {} \;


#	for loop in disk cpu mem msgs netprotl pagefile procs svcs 
#	do
#	if [ -f "${dir_bbhtml}${hostname}.${loop}.html" ]; then	
#		sudo mv "${dir_bbhtml}${hostname}.${loop}.html" "${dir_filebak_html}${hostname}.${loop}.html"  
#		if [ $? -eq 0 ]; then 
#			echo "${dir_bbhtml}${loop}${str_del6}"
#		else
#			echo "${dir_bbhtml}${loop}${str_del7}"
#		fi
#	fi
#	if [ -f "${dir_bblogs}${hostname}.${loop}.html" ]; then	
#		sudo mv "${dir_bblogs}${hostname}.${loop}.html" "${dir_filebak_logs}${hostname}.${loop}.html"  
#		if [ $? -eq 0 ]; then 
#			echo "${dir_bblogs}${loop}${str_del6}"
#		else
#			echo "${dir_bblogs}${loop}${str_del7}"
#		fi
#	fi
#	if [ -f "${dir_bbhist}${hostname}.${loop}.html" ]; then	
#		sudo mv "${dir_bbhist}${hostname}.${loop}.html" "${dir_filebak_hist}${hostname}.${loop}.html"  
#		if [ $? -eq 0 ]; then 
#			echo "${dir_bbhist}${loop}${str_del6}"
#		else
#			echo "${dir_bbhist}${loop}${str_del7}"
#		fi
#	fi
#	done
        
        sudo find /home/bbuser/ -name "${hostname}.*.html" |xargs -r rm -rf
	echo ${str_del8}
	sudo /etc/init.d/BigBrother start
	echo ${str_del09}
else
	echo ${str_del10}
fi
}

f_newmodi()
{
 echo -n ${str_newmodi0}
if [ -z $hostname ]; then
	echo -n  ${str_modi1}
	read hostname
fi

if [ -z $hostip ]; then
	echo -n ${str_modi2}
	read hostip
fi
echo "${str_modi3}[$hostname] ${str_modi4}[$hostip]"
result=`cat  /home/bbuser/bb/etc/bb-hosts  |egrep -n  "[[:space:]]$hostname[[:space:]]" |wc -l `
if [ $result -eq 0 ];then
	echo ${str_modi5}
	return 1;
fi
result=`cat  /home/bbuser/bb/etc/bb-hosts  |egrep -n  "[[:space:]]$hostip[[:space:]]" |wc -l `
if [ $result -ne 0 ];then
	echo ${str_modi6}
	return 1;
fi
newhostname=${dir_hostbak}bb-hosts`date +%Y%m%d_%H%M`
cp -f $dir_bbhost ${newhostname}.bak
sed "s/\t${hostname} /\t${hostip} /" $dir_bbhost >${newhostname}
sudo cp -f ${newhostname} $dir_bbhost
if [ $? -eq 0 ]; then
	echo ${str_del4}
	echo ${str_del5}
	sudo /etc/init.d/BigBrother stop
	for loop in disk cpu mem msgs netprotl pagefile procs svcs 
	do
	if [ -f "${dir_bbhtml}${hostname}.${loop}.html" ]; then	
		sudo mv "${dir_bbhtml}${hostname}.${loop}.html" "${dir_filebak_html}${hostname}.${loop}.html"  
		if [ $? -eq 0 ]; then 
			echo "${dir_bbhtml}${loop}${str_del6}"
		else
			echo "${dir_bbhtml}${loop}${str_del7}"
		fi
	fi
	if [ -f "${dir_bblogs}${hostname}.${loop}.html" ]; then	
		sudo mv "${dir_bblogs}${hostname}.${loop}.html" "${dir_filebak_logs}${hostname}.${loop}.html"  
		if [ $? -eq 0 ]; then 
			echo "${dir_bblogs}${loop}${str_del6}"
		else
			echo "${dir_bblogs}${loop}${str_del7}"
		fi
	fi
	if [ -f "${dir_bbhist}${hostname}.${loop}.html" ]; then	
		sudo mv "${dir_bbhist}${hostname}.${loop}.html" "${dir_filebak_hist}${hostname}.${loop}.html"  
		if [ $? -eq 0 ]; then 
			echo "${dir_bbhist}${loop}${str_del6}"
		else
			echo "${dir_bbhist}${loop}${str_del7}"
		fi
	fi
	done

	echo ${str_del8}
	sudo /etc/init.d/BigBrother start
	echo ${str_del09}
else
	echo ${str_del10}
fi
}




main()
{
f_init
if [ $? -ne 0 ] ;then
	echo ${str_main0}
	exit
fi
if [ -z $action ]
 then
	echo ${str_main1}
	echo ${str_main2}
	echo ${str_main3}
	echo ${str_main4}
	echo ${str_main5}
	echo ${str_main6}
	echo  -e ${str_main7}
	echo -n  ${str_main8}
read action
fi
case $action in
	1) f_add;;
	add) f_add;;
	2) f_modify;;
	modify) f_modify;;
	3) f_newmodi;;
	newmodi) f_newmodi;;
	4) f_del;;
	delete) f_del;;
	0) exit;;
	*) echo ${str_main9} ;;
esac
}
action=$1
hostname=$2
hostip=$3
main


posted on 2011-06-03 15:59  51ak  阅读(522)  评论(0编辑  收藏  举报

导航