#!/bin/bash
CPWD=$(cd `dirname $0`;pwd)
which ipmitool >/dev/null 2>&1
if [ $? -ne 0 ];then
        yum install *ipmi* *IPMI* -y
fi
ipmi_ip=$(ipmitool lan print |grep "IP Address  " |awk -F ": " '{print$2}')
passwd="ucloud.cn"
#netmask=$(ipmitool lan print |grep "Subnet Mask" |awk -F ": " '{print$2}')
#gateway=$(ipmitool lan print |grep "Default Gateway IP" |awk -F ": " '{print$2}')
function BMCUpdating() {
	echo "*** BMC updating ***"
	chmod +x Yafuflash
	hpm_file=$(ls *.hpm)
	echo YY|./Yafuflash -pc -cd $hpm_file
		ipmitool mc reset cold
		sleep 180
}

function UserSetting() {
	ipmitool user list 1 |grep root
	if [ $? = 0 ];then
		echo "Root user is existed..."
	else
		sleep 3
		echo "*** Config User related settings ***"
		#//Create new user named "root"
		ipmitool user set name 2 root
		#//Enabled the new user
		ipmitool user enable 2
		#//Set new password to root
		ipmitool user set password 2 $passwd
		#//Set the administrator priv to root
		ipmitool user priv 2 4 1
		#//Set the highest access priv to root
		ipmitool channel setaccess 1 2 ipmi=on
		echo "Modified KVM using privilege"
		echo "Check the machine type"
		M_type=`ipmitool fru |grep "Product Name" |awk -F ":" '{print$2}' |awk NR==1 |awk -F " " '{print$1}'`
		echo "Machine Type: $M_type"
		#if [ $M_type = NF5280R6 ];then
		#	if [ $(ipmitool raw 0x32 0xa4 0x02 |awk -F " " '{print$1}') = 07 ];then
		#	echo "The KVM privilege has been set for root user"
		#	else
        	#	ipmitool raw 0x32 0xA3 0x02 0x03 0x00 0x00 0x00   #附予kvm权限
		#	fi
		#fi
	fi
}

BMCUpdating
UserSetting


