Script to change WWPNS in LPAR profile

This is a script to interactively change WWPNs on adapters in an IBM POWER/Power systems LPAR profile. This is useful if you have to re-create adapters or even whole profiles as the HMC GUI does not allow you to set or change WWPNs, and the HMC command line is not exactly elegant.

# Copyright AIXperts Consultancy ltd, Henrik Morsing - November 2022

echo
echo "You are about to write new WWPNs to the adapters for this profile. Please backup the profile first"
echo

# Prompt for hmc and host

printf "Please enter HMC to connect to: "; read hmc
printf "Please enter host to change: "; read host
echo

# Find LPAR and system names
while read sys
do
   #echo "Checking system "${sys}
   lpar="$(echo $(ssh -n ${hmc} lssyscfg -r lpar -m ${sys} -F name) | tr ' ' '\n' | grep ${host})"
   if [[ ${lpar} != "" ]]
   then
      #echo "Break-out"
      export system=${sys}
      break
   fi
done <<< "$(ssh ${hmc} 'lssyscfg -r sys -F name')"

# Find profile name
profile_name="$(echo $(ssh ${hmc} lssyscfg -r prof -m ${system} --filter \"lpar_names=${lpar}\" -F name) | cut -f1 -d' ')"

# Find WWPNs
typeset -A value        # For WWPNs
typeset -i f=0          # Counter for adapter
typeset -i field=1      # Counter for adapter
value[${f}]="kjfhkes"   # Inital, non-empty value

# Define some arrays to read output values into
typeset -A adapter_id
typeset -A rlpar_id
typeset -A rlpar_name
typeset -A rslot_no

# Let's fetch the output from the HMC
cmd_out="$(ssh ${hmc} lssyscfg -m ${system} -r prof --filter \"lpar_names=${lpar},profile_names=${profile_name}\" -F virtual_fc_adapters)"

# Loop to read every adapter output
while [[ -n ${value[${f}]} ]]
do
   f+=1
   # Read values for each adapter in turn into the arrays
   echo ${cmd_out} | tr -d '"' | cut -d, -f${field},$(( ${field}+1 )) | tr '/' ' '| read adapter_id[${f}] client rlpar_id[${f}] rlpar_name[${f}] rslot_no[${f}] value[${f}] req

   field+=2
done

# Before starting this loop, start building up a command to submit
cmd="ssh ${hmc} chsyscfg -r prof -m ${system} --force -i \"lpar_name=${lpar}, name=${profile_name}, \\\"virtual_fc_adapters="

f=1     # Adapter counter

while [[ -n ${value[${f}]} ]]
do
   printf "Enter new WWPN or hit <ENTER> to keep WWPN ["${value[${f}]}"]: "; read wwpns_input

   if [[ "${wwpns_input}" == "" ]]
   then

      cmd_build=${cmd}\\\"\\\"${adapter_id[${f}]}/client/${rlpar_id[${f}]}/${rlpar_name[${f}]}/${rslot_no[${f}]}/${value[${f}]}/0\\\"\\\",
      cmd=${cmd_build}

      # Increment f
      f+=1

   elif [[ "${wwpns_input}" =~ [0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f],[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f] ]]
   then
      value[${f}]=${wwpns_input}

      cmd_build=${cmd}\\\"\\\"${adapter_id[${f}]}/client/${rlpar_id[${f}]}/${rlpar_name[${f}]}/${rslot_no[${f}]}/${value[${f}]}/0\\\"\\\",
      cmd=${cmd_build}

      # Increment f
      f+=1

   else
      echo "Invalid entry, please try again"
   fi
done

# Now, tidy-up end of command
cmd_end=$(echo ${cmd_build} | sed 's/,$//')     # Remove trailing comma
cmd_complete=${cmd_end}\\\"\"\                  # Finish off quites

echo
echo "Running: "${cmd_complete}

${cmd_complete}

exit 0

This entry was posted in AIX, HMC, IBM POWER, Scripting and tagged , , , , , , . Bookmark the permalink.

Leave a Reply