Skip to content

Instantly share code, notes, and snippets.

@dalang
Created July 18, 2013 11:47
Show Gist options
  • Save dalang/6028695 to your computer and use it in GitHub Desktop.
Save dalang/6028695 to your computer and use it in GitHub Desktop.
bash script to get data from csv file and run command with the parsed info
#!/bin/bash
#=====================================================
#== USAGE: HW_bmc_conf.sh [csvFilename] <SerialNumber>
#== Return:
#== 0 -- normal
#== 1 -- Args error
#== 2 -- imana configuration file is not exist
#== 3 -- The Server with given serialnumber is NOT in csv File
#=====================================================
currentPath=`pwd`/
pushd "`dirname "$0"`" >> /dev/null
USAGE="--------------------------------------------\nUSAGE: HW_bmc_conf.sh [csvFilename] <SerialNumber>"
default_csvFile=svrinfo.csv
# Check arguments
if [ $# -gt 2 ]; then
echo "Error ID 1: Too much args, the maximum args number is 2."
echo -e $USAGE
exit 1
elif [ $# -eq 2 ]; then
csvFile=$1
if ! [[ "$csvFile" =~ ^.+\.csv$ ]];then
echo "the 1st arg should be a filename end with .csv"
echo -e $USAGE
exit 1
fi
serialnumber=$2
elif [ $# -eq 1 ]; then
csvFile=default_csvFile
serialnumber=$1
else
echo "Please send at least 1 argument"
echo -e $USAGE
exit 1
fi
if ! [[ "$serialnumber" =~ ^[a-zA-Z0-9]+$ ]];then
echo "serialnumber could only contain upper or lower case letters and numbers."
echo -e $USAGE
exit 1
fi
# Check if csv file is existing
function get_key_index()
{
local _xrsh_tmp
local _xrsh_cnt=0
local _xrsh_array=(`echo "$1"`)
for _xrsh_tmp in ${_xrsh_array[*]}; do
if [[ "$_xrsh_tmp" = "$2" ]]; then
echo $_xrsh_cnt
return
fi
_xrsh_cnt=$(( $_xrsh_cnt + 1 ))
done
echo "-1"
}
if ! [[ "$csvFile" =~ / ]]; then
csvFile=$currentPath/$csvFile
elif ! [[ "$csvFile" =~ ^/ ]]; then
csvFile=$currentPath/$csvFile
fi
targetFile=$csvFile
echo "target file >>> "$targetFile
if ! [ -e $targetFile ]; then
echo "$csvFile could not be found."
exit 2
fi
# filter out server info in csv file with serial number
_header_columns=`head -1 $csvFile`
OLD_IFS=$IFS
IFS=","
_header_columns=($_header_columns)
IFS=$OLD_IFS
# loop through header columns
#for _header in ${_header_columns[@]}
#do
#echo `get_key_index "${_header_columns[*]}" $_header`
#done
svrConf=( `grep $serialnumber $targetFile` )
if [ ! $svrConf ]; then
echo "Error ID: 3 The SN: $serialnumber is not in $targetFile"
exit 3
fi
OLD_IFS=$IFS
IFS=","
svrConf=($svrConf)
IFS=$OLD_IFS
#================================================================
#index=`get_key_index "${_header_columns[*]}" "ILO_CONF"`
#echo $index
#echo ${svrConf[$index]}
#if [ $index -lt 0 ] || [ "${svrConf[$index]}" = "FALSE" ] || [ "${svrConf[$index]}" = "false" ] \
#|| [ "${svrConf[$index]}" = "false" ]
#then
#echo "Not Allowed to Configure ILO, EXIT!"
#exit 0
#fi
#================================================================
# get IP configuration
declare -a netItems=('ILO_LOCATE' 'ILO_IPADDR' 'ILO_NETMASK' 'ILO_GATEWAY' 'ILO_DNSPRI' 'ILO_DNSSEC');
declare -a netSettings
COUNTER=0
for item in "${netItems[@]}"
do
index=`get_key_index "${_header_columns[*]}" $item`
if [ $index -lt 0 ] || [ -z "${svrConf[$index]}" ]; then
echo $item" is empty, EXIT"
exit 0
fi
#echo $item"<==>"${svrConf[$index]}
netSettings[$COUNTER]=${svrConf[$index]}
COUNTER=$COUNTER+1
done
# =========Start iMana Setting==========
modprobe ipmi_msghandler
modprobe ipmi_devintf
modprobe ipmi_si
if [ -z `which ipmitool` ]; then
echo 'ipmitool command is not available'
else
# ipmitool LAN Configuration
ipmitool lan set 1 ipsrc static
ipmitool lan set 1 ipaddr ${netSettings[1]}
ipmitool lan set 1 netmask ${netSettings[2]}
ipmitool lan set 1 defgw ipaddr ${netSettings[3]}
#ipmitool lan set 1 defgw macaddr 00:0e:0c:aa:8e:13
#ipmitool lan set 1 arp respond on
#ipmitool lan set 1 auth ADMIN MD5
#ipmitool lan set 1 access on
ipmitool lan print 1
# ipmitool User Configuration
ipmitool user set name 2 root
ipmitool user set password 2 razor
ipmitool channel setaccess 1 2 link=on ipmi=on callin=on privilege=4
ipmitool user enable 2
fi
#==========End iMana Setting==============
popd >> /dev/null
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment