#!/bin/bash
ipmi_ip=$(ipmitool lan print |grep "IP Address  " |awk -F ": " '{print$2}')
#password=`curl -H "Content-Type:application/json" -X POST --data  "{\"data\":  [\"${ipmi_ip}\"],\"type\": \"IPMI\"}" http://udcm.ucloudadmin.com/api/ipmi/v1/getpwd/ | jq "." | grep 'IPMIPassword' | awk '{print $NF}' | sed  's/^//g' | sed 's/^,$//g'`
SN=$(dmidecode -s system-serial-number |grep -v ^#  | sed s/[[:space:]]//g)
passwd=$1
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 y|./socflash_x64 if=ROME2D32GM-2T_1.36.05.ima
}

function IPMISetting() {
        echo "*** Config IPMI related settings ***"
        if [ $(ipmitool lan print |grep "IP Address Source" |awk -F ": " '{print$2}' |awk -F " " '{print$1}') = "Static" ];then
                echo "IPMI has been set completely"
        elif [ $(ipmitool lan print |grep "Default Gateway IP" |awk -F ": " '{print$2}') = "0.0.0.0" ];then
                ipmitool lan set 1 ipsrc static
                ipmitool lan set 1 ipaddr $ipmi_ip
                ipmitool lan set 1 netmask $netmask
                ipmitool lan set 1 defgw ipaddr $gateway
                #ipmitool user set password 2 $passwd
        else
                ipmitool lan set 1 ipsrc static
                ipmitool lan set 1 ipaddr $ipmi_ip
                #ipmitool user set password 2 $passwd
        fi
}

BMCUpdating
IPMISetting

