#!/bin/bash
################################################################################
#                                                                              #
#              HPM.2 Get Dynamic Credentials Test Script  v0.02                #
#                 Tested with ipmitool version 1.8.11 and .12                  #
#                                                                              #
#                   first parameter: IPMB Address                              #
#    all following parameters: command that should be executed via RMCP+       #
#                                                                              #
################################################################################

# Session Timeout for the remote session in seconds (currently is 60 seconds)
gdc_timeout_b0="0x32"    #LSB
gdc_timeout_b1="0x00"
gdc_timeout_b2="0x00"
gdc_timeout_b3="0x00"    #MSB



################################################################################
#                                                                              #
#                     Nothing to change below this line                        #
#                                                                              #
################################################################################

function decode_ipmitoolret_name()
{
	decode_0=$(printf \\x"${coded:10:2}")
	decode_1=$(printf \\x"${coded:13:2}")
	decode_2=$(printf \\x"${coded:16:2}")
	decode_3=$(printf \\x"${coded:19:2}")
	decode_4=$(printf \\x"${coded:22:2}")
	decode_5=$(printf \\x"${coded:25:2}")
	decode_6=$(printf \\x"${coded:28:2}")
	decode_7=$(printf \\x"${coded:31:2}")

	decoded=$decode_0$decode_1$decode_2$decode_3$decode_4$decode_5$decode_6$decode_7
}

function decode_ipmitoolret_pwm()
{
	decode_0=$(printf \\x"${coded:10:2}")
	decode_1=$(printf \\x"${coded:13:2}")
	decode_2=$(printf \\x"${coded:16:2}")
	decode_3=$(printf \\x"${coded:19:2}")
	decode_4=$(printf \\x"${coded:22:2}")

	decoded_pwm=$decode_0$decode_1$decode_2$decode_3$decode_4
}

function decode_ipmitoolret_pwl()
{
	decode_0=$(printf \\x"${coded:10:2}")
	decode_1=$(printf \\x"${coded:13:2}")
	decode_2=$(printf \\x"${coded:16:2}")
	decode_3=$(printf \\x"${coded:19:2}")
	decode_4=$(printf \\x"${coded:22:2}")
	decode_5=$(printf \\x"${coded:25:2}")
	decode_6=$(printf \\x"${coded:28:2}")
	decode_7=$(printf \\x"${coded:31:2}")
	decode_8=$(printf \\x"${coded:34:2}")
	decode_9=$(printf \\x"${coded:37:2}")

	decoded_pwl=$decode_0$decode_1$decode_2$decode_3$decode_4$decode_5$decode_6$decode_7$decode_8$decode_9
}

function get_remote_ip
{
	ipmitool -t $remote_blade_addr lan print 1 | egrep "(IP Address) *: *[0-9]+.[0-9]+.[0-9]+.[0-9]+" | egrep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+" | tr -d '\n'
}

remote_blade_addr=$1 # IPMB-0 Address of the remote blade
shift
remote_command="mc info" # Command that should be executed via RMCP



echo "Getting remote IP-Address"
rem_ip=$(get_remote_ip)
echo "Remote IP Address: $rem_ip"


echo "Requesting new dynamic IPMI 2.0 session with Cipher Suite ID 2 for IPMI LAN channel 1"
it_cmd_os="ipmitool -t $remote_blade_addr raw 0x2c 0x3f 0x00 0x00 0x00 0x01 0x02 0x04 0x01 $gdc_timeout_b0 $gdc_timeout_b1 $gdc_timeout_b2 $gdc_timeout_b3 0x00 0x00 0x00 0x00"
echo "Requesting new dynamic IPMI 1.5 session with Authentication Type 4h = straight password / key for IPMI LAN channel 1"
#it_cmd_os="ipmitool -m 0x80 -t $remote_blade_addr raw 0x2c 0x3f 0x00 0x00 0x00 0x00 0x04 0x04 0x01 $gdc_timeout_b0 $gdc_timeout_b1 $gdc_timeout_b2 $gdc_timeout_b3 0x00 0x00 0x00 0x00"
#echo $it_cmd_os
it_os="$($it_cmd_os)"
if [ "$?" -ne 0 ]
then
 echo "Failed to get Dynamic session"
 exit 1
fi
#echo $it_os
SESSION=${it_os:4:2}
echo "SESSION-HANDLE: $SESSION"

echo "Requesting Username and Password"

#Username Most significant bytes
it_cmd_unm="ipmitool -t $remote_blade_addr raw 0x2c 0x3f 0x00 0x$SESSION 0x03"
#echo $it_cmd_unm
it_unm="$($it_cmd_unm)"

if [ "$?" -ne 0 ]
then
 echo "Failed to get most significant bytes of the username"
 exit 1
fi
#echo $it_unm
coded=$it_unm
decode_ipmitoolret_name
username=$decoded


#Username Least significant bytes
it_cmd_unl="ipmitool -t $remote_blade_addr raw 0x2c 0x3f 0x00 0x$SESSION 0x02"
#echo $it_cmd_unl
it_unl="$($it_cmd_unl)"

if [ "$?" -ne 0 ]
then
 echo "Failed to get least significant bytes of the username"
 exit 1
fi
#echo $it_unl
coded=$it_unl
decode_ipmitoolret_name
username=$decoded$username

#Password Most significant bytes
it_cmd_pwm="ipmitool -t $remote_blade_addr raw 0x2c 0x3f 0x00 0x$SESSION 0x05"
#echo $it_cmd_pwm
it_pwm="$($it_cmd_pwm)"

if [ "$?" -ne 0 ]
then
 echo "Failed to get most significant bytes of the password"
 exit 1
fi
#echo $it_pwm
coded=$it_pwm
decode_ipmitoolret_pwm
password=$decoded_pwm


#Password Least significant bytes
it_cmd_pwl="ipmitool -t $remote_blade_addr raw 0x2c 0x3f 0x00 0x$SESSION 0x04"
#echo $it_cmd_pwl
it_pwl="$($it_cmd_pwl)"

if [ "$?" -ne 0 ]
then
 echo "Failed to get least significant bytes of the password"
 exit 1
fi
#echo $it_pwl
coded=$it_pwl
decode_ipmitoolret_pwl
password=$decoded_pwl$password

echo "Username: $username   Password: $password"

echo ""
echo "Executing commands via LAN"

echo ""
echo "remote_command: $remote_command"
echo ipmitool -I lanplus -H $rem_ip -U $username -P $password $remote_command
ipmitool -I lanplus -H $rem_ip -U $username -P $password $remote_command




