Bash Code optimization For ssh login on the remote device

Posted on

Problem

I have below code which works fine as its written in bash, i am just posting it here to see if this can be better optimised as per the BASH way. I am have an intermediate understanding in bash so, looking for any expert advise.

Thanks for the reveiw and comments in advanced .

BASH CODE:

#!/bin/bash
#######################################################################
# Section-1 , this script logs in to the Devices and pulls all the Data
#######################################################################
# timestamp to be attached to the log file
TIMESTAMP=$(date "+%m%d%Y-%H%M%S")

# logfile to collect all the Firmware Version of C7000 components
LOGFILE_1="/home/myuser/firmware_version-${TIMESTAMP}.log"
LOGFILE_2="/home/myuser/fmv_list-${TIMESTAMP}.log"

# read is a built-in command of the Bash shell. It reads a line of text from standard input.
# -r option used for the "raw input", -s option used for Print the string prompt,
# while option -s tells do not echo keystrokes when read is taking input from the terminal.
# So, altogether it reads password interactively and save it to the environment
read -rsp $'Please Enter password below:n' SSHPASS
export SSHPASS

for host in $(cat enc_list);
do
        echo "========= $host =========";
        sshpass -e timeout -t 20 ssh -o "StrictHostKeyChecking no" $host  -l tscad  show firmware summary ;
done |  tee -a "${LOGFILE_1}"

# at last clear the exported variable containing the password
unset SSHPASS

######################################################################################
# Section-2, This will just grep the desired data from the log file as produced from
# Section-1, log file
######################################################################################
data_req="`ls -l /home/myuser/firmware_version-* |awk '{print $NF}'| tail -1`"
cat "${data_req}" | egrep '=|1   BladeSystem|HP VC'  | awk '{$1=$1};1' | tee -a "${LOGFILE_2}"

Data Pulled by Script as outlined in section-1 in the code.

========= enc1001 =========

HPE BladeSystem Onboard Administrator
(C) Copyright 2006-2018 Hewlett Packard Enterprise Development LP

OA-9457A55F4C75 [SCRIPT MODE]> show firmware summary


Enclosure Firmware Summary

Blade Offline Firmware Discovery:       Disabled

Onboard Administrator Firmware Information
Bay Model                                              Firmware Version
--- -------------------------------------------------- ----------------
1   BladeSystem c7000 DDR2 Onboard Administrator with KVM 4.85
2   OA Absent

Enclosure Component Firmware Information
Device  Name                                          Location Version    NewVersion
-----------------------------------------------------------------------------------
TRAY  | BladeSystem c7000 Onboard Administrator Tray  | -     | 1.7      | 1.7
FAN   | Active Cool 200 Fan                           | 1     | 2.9.4    | 2.9.4
FAN   | Active Cool 200 Fan                           | 2     | 2.9.4    | 2.9.4
FAN   | Active Cool 200 Fan                           | 3     | 2.9.4    | 2.9.4
FAN   | Active Cool 200 Fan                           | 4     | 2.9.4    | 2.9.4
FAN   | Active Cool 200 Fan                           | 5     | 2.9.4    | 2.9.4
FAN   | Active Cool 200 Fan                           | 6     | 2.9.4    | 2.9.4
FAN   | Active Cool 200 Fan                           | 7     | 2.9.4    | 2.9.4
FAN   | Active Cool 200 Fan                           | 8     | 2.9.4    | 2.9.4
FAN   | Active Cool 200 Fan                           | 9     | 2.9.4    | 2.9.4
FAN   | Active Cool 200 Fan                           | 10    | 2.9.4    | 2.9.4
BLD   | HP Location Discovery Services                | -     | 2.1.3    | 2.1.3


Device Firmware Information

Device Bay:     1
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 09/12/2016
iLO4                                 2.50 Sep 23 2016
Power Management Controller          1.0.9

Device Bay:     2
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 09/12/2016
iLO4                                 2.50 Sep 23 2016
Power Management Controller          1.0.9

Device Bay:     3
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 05/21/2018
iLO4                                 2.61 Jul 27 2018
Power Management Controller          1.0.9

Device Bay:     4
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 09/12/2016
iLO4                                 2.50 Sep 23 2016
Power Management Controller          1.0.9

Device Bay:     5
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 09/12/2016
iLO4                                 2.50 Sep 23 2016
Power Management Controller          1.0.9

Device Bay:     6
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 09/12/2016
iLO4                                 2.50 Sep 23 2016
Power Management Controller          1.0.9

Device Bay:     7
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 09/12/2016
iLO4                                 2.50 Sep 23 2016
Power Management Controller          1.0.9

Device Bay:     8
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 10/25/2017
iLO4                                 2.70 May 07 2019
Power Management Controller          1.0.9

Device Bay:     9
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 09/12/2016
iLO4                                 2.70 May 07 2019
Power Management Controller          1.0.9

Device Bay:     10
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 10/25/2017
iLO4                                 2.55 Aug 16 2017
Power Management Controller          1.0.9

Device Bay:     11
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 10/25/2017
iLO4                                 2.55 Aug 16 2017
Power Management Controller          1.0.9

Device Bay:     12
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 05/05/2016
iLO4                                 2.40 Dec 02 2015
Power Management Controller          1.0.9

Device Bay:     13
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 12/28/2015
iLO4                                 2.40 Dec 02 2015
Power Management Controller          1.0.9

Device Bay:     14
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 05/05/2016
iLO4                                 2.40 Dec 02 2015
Power Management Controller          1.0.9

Device Bay:     15
Discovered:     No

Firmware Component                   Current Version      Firmware ISO Version
------------------------------------ -------------------- ---------------------
System ROM                           I36 09/12/2016
iLO4                                 2.70 May 07 2019
Power Management Controller          1.0.9

Interconnect Firmware Information

Bay Device Model               Firmware Version
--- -------------------------- ----------------
  1 HP VC Flex-10/10D Module   4.50
  2 HP VC Flex-10/10D Module   4.50

The Overall output of the code is as follows:

========= enc1001 =========
1 BladeSystem c7000 DDR2 Onboard Administrator with KVM 4.85
1 HP VC Flex-10/10D Module 4.50
2 HP VC Flex-10/10D Module 4.50

Thanks

Solution

  • there is no need for final semicolon ;

loop

for host in $(cat enc_list);
  • $(cat ) can be writen as $(< ), latter form is builtin and will not fork a cat command.

data

data_req="`ls -l /home/myuser/firmware_version-* |awk '{print $NF}'| tail -1`"
  • no need for quote
  • back tick ( ` ) is deprecated use $( ) construct.
  • you use ls -l then awk to filter filename ( $NF ), just use ls | tail -1
  • sorting by ls won’t work when year change.
  • sorting ls is frowned upon (well if you build all the sorted files without space or newline in their name, it might be OK)
  • if you still want ls sorting use either ls -t or ls -rt to filter by date (newest first, oldest first)
  • use ls to skip any aliased ls (when piped ls will put one file per line, this can be forced by ls -1, column display can be forced with ls -C )
  • you use ${LOGFILE_1} above, then use a parsed ls to retrieve the file, why not use ${LOGFILE_1} again ?

parsing

cat "${data_req}" | egrep '=|1   BladeSystem|HP VC'  | awk '{$1=$1};1' | tee -a "${LOGFILE_2}"
  • grep can read file, this is a useless use of cat.
  • awk '{$1=$1};1' will do nothing

the line can be written as

egrep '=|1[ ]+BladeSystem|HP VC'  "${LOGFILE_1}"  | tee -a "${LOGFILE_2}"

  • I am pretty sure you can use public/private keys with HP enclosure.
  • those enclosure might give you XML answer, it might be worth the effort to analyse and parse it using XML tools ( xmlstartlet/xsltproc/xmllint ), not awk/sed/grep

Leave a Reply

Your email address will not be published. Required fields are marked *