我有下面的代码,它的工作原理很好,就像用bash编写的一样,我只是在这里发布它,看看是否可以按照BASH方式更好地优化它。我对bash有一个中间的理解,所以,寻找任何专家的建议。
感谢您的评论和评论。
#!/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 按代码中第1节所概述的脚本提取的数据.========= 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代码的总体输出如下:========= 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谢谢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}"按代码中第1节所概述的脚本提取的K15数据.K26A7K18代码的总体输出如下:K29A10
谢谢
发布于 2020-07-01 19:50:40
;for host in $(cat enc_list);$(cat )可以写为$(< ),后者是内置的,不会发送cat命令。data_req="`ls -l /home/myuser/firmware_version-* |awk '{print $NF}'| tail -1`") is deprecated use$( )构造)。ls -l然后awk来过滤文件名( $NF ),只需使用ls | tail -1ls进行排序将不起作用。ls是不允许的(好吧,如果您构建所有排序的文件,没有空格或换行符的名字,它可能是可以的)ls排序,请使用ls -t或ls -rt按日期进行筛选(最新的第一次,最早的第一次)\ls跳过任何别名的ls (当管道ls每行放置一个文件时,这可以由ls -1强制执行,列显示可以强制使用ls -C )${LOGFILE_1},然后使用解析的ls检索文件,为什么不再次使用${LOGFILE_1}呢?cat "${data_req}" | egrep '=|1 BladeSystem|HP VC' | awk '{$1=$1};1' | tee -a "${LOGFILE_2}"grep可以读取文件,这是一个无用的猫使用。awk '{$1=$1};1'什么也做不了这一行可以写成
egrep '=|1[ ]+BladeSystem|HP VC' "${LOGFILE_1}" | tee -a "${LOGFILE_2}"https://codereview.stackexchange.com/questions/244835
复制相似问题