您好我有一个我正在创建工作的脚本,并被要求添加一个功能以实现可伸缩性。我目前在每次请求后暂停,但我不知道如何添加跳过暂停功能。我留下了我的代码,以显示我正在做的提供更多信息的基本功能。在顶部我主要有函数,在底部是函数动作和暂停。
#!/bin/bash
# Basic Linux host info script
# Matthew Morcaldi 2015
# TODO: Getops & HP Support
# Config ------------------------------------------------
outfile="linux_info.txt"
#--------------------------------------------------------
#### uncomment (set -x) for debug
# set -x
checkroot() {
if [ $UID -ne 0 ] ; then
echo "User has insufficient privilege."
exit 4
fi
}
header() {
echo '' | tee -a $outfile
echo '----------------------' | tee -a $outfile
echo "[*] $title" | tee -a $outfile
echo '----------------------' | tee -a $outfile
}
function pause(){
read -p "$*"
}
display_os() {
title="System"
header $title
dmidecode -t 1 | grep -i 'serial' | sed 's/^ *//' | tee $outfile
cat /etc/*-release | tee -a $outfile
dmidecode -t system -q | egrep -i 'Manufacturer: |Product|UUID' | \
sed 's/^ *//' | tee -a $outfile
ipmitool bmc info | egrep -i 'Firmware revision' | tee -a $outfile
dmidecode -t bios -q | egrep -i 'version|vendor' | \
sed 's/^ *//' | tee -a $outfile
}
display_network_oob() {
title="Drac-Info"
header $title
ipmitool lan print | egrep -i 'IP Address|MAC Address|Default Gateway IP|Subnet Mask' | tee -a $outfile
}
display_networking() {
title="Networking"
header $title
ifconfig -a | tee -a $outfile
}
display_bonding() {
if [ -f /proc/net/bonding/bond0 ] ; then
title="Bonding"
header $title
cat /proc/net/bonding/bond0 | sed 's/^ *//' | tee -a $outfile
fi
}
display_switch_info() {
if [ -f /usr/sbin/lldpctl ] ; then
title="Switch-Port-Information"
header $title
lldpctl | egrep '(Interface|VLAN|PortDescr|SysName)' | \
sed 's/^ *//' | tee -a $outfile
else
echo "Switch file LLDPCTL does not exist skipping"
fi
}
show_disks_dell() {
if [ -f /opt/MegaRAID/MegaCli/MegaCli64 ] ; then
title="Dell-Raid"
header $title
/opt/MegaRAID/MegaCli/MegaCli64 -PDList -aAll | \
egrep -i 'count|^Device Id: |firmware state' | \
grep -v 'Count: 0' | \
perl -p -e 's/Firmware state: (.*)$/Firmware state: $1\n/' | tee -a $outfile
else
echo "skipping Dell disk check"
fi
}
show_battery_dell() {
if [ -f /opt/MegaRAID/MegaCli/MegaCli64 ]; then
title="Dell-Battery"
header $title
/opt/MegaRAID/MegaCli/MegaCli64 -AdpBbuCmd -GetBbuStatus -a0 | \
egrep -i 'isSOHGood|Charger Status|Capacity|Relative|Charging' | \
sed 's/^ *//' | tee -a $outfile
else
echo "skipping Dell battery check"
fi
}
show_disks_hp() {
if [ -f /usr/sbin/hpacucli ]; then
title="HP-Raid"
header $title
hpacucli ctrl all show config detail | \
sed 's/^ *//' | tee -a $outfile
else
echo "skipping HP disk check"
fi
}
show_battery_hp() {
if [ -f /usr/sbin/hpacucli ]; then
title="HP-Battery"
header $title
hpacucli ctrl all show status | \
sed 's/^ *//' | tee -a $outfile
else
echo "skipping HP battery check"
fi
}
display_dimms() {
title="Memory"
header $title
cat /proc/meminfo|grep MemTotal >> $outfile
free -g | tee -a $outfile
}
sel_list() {
title="SEL"
header $title
ipmitool sel elist | tee -a $outfile
}
# Where the magic happens -------------------------------------
# TODO: Add Optarg Support
# http://wiki.bash-hackers.org/howto/getopts_tutorial
# comment out below to skip
pause 'Press [Enter] key to continue. ..'
service ipmi start
pause 'Press [Enter] key to continue. ..'
display_os
pause 'Press [Enter] key to continue. ..'
display_network_oob
pause 'Press [Enter] key to continue. ..'
display_networking
pause 'Press [Enter] key to continue. ..'
display_bonding
pause 'Press [Enter] key to continue. ..'
display_dimms
pause 'Press [Enter] key to continue. ..'
display_switch_info
pause 'Press [Enter] key to continue. ..'
sel_list
pause 'Press [Enter] key to continue. ..'
show_disks_hp
pause 'Press [Enter] key to continue. ..'
show_battery_hp
pause 'Press [Enter] key to continue. ..'
show_disks_dell
pause 'Press [Enter] key to continue. ..'
show_battery_dell
pause 'Press [Enter] key to continue. ..'
service ipmi stop
cat << "EOF"
_ _,---._
,-',' `-.___
/-;' `._
/\/ ._ _,'o \
( /\ _,--'\,','"`. )
|\ ,'o \' //\
| \ / ,--'""`-.
: \_ _/ ,-' `-._
\ `--' / )
`. \`._ ,' ________,','
.--` ,' ,--` __\___,;'
\`.,-- ,' ,`_)--' /`.,'
\( ; | | ) (`-/
`--'| |) |-/
| | | | |
| | |,.,-. | |_
| `./ / )---` )
_| / ,', ,-'
,'|_( /-<._,' |--,
| `--'---. \/ \
| / \ /\ \
,-^---._ | \ / \ \
,-' \----' \/ \--`.
/ \ \ \
EOF
答案 0 :(得分:1)
将您的pause
函数更改为返回0(true
)以跳过,或1(false
)继续,并将其重命名为更具描述性的内容:
function shouldSkip {
read -p "$*" line
test "$line" = 'n' -o "$line" = 's'
return $?
}
使用:
if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "service ipmi start". ..'
then
service ipmi start
fi
答案 1 :(得分:0)
特别感谢Danny Dagla。发布最终脚本供全球使用。
#!/bin/bash
# Linux host info script [DONUTS]
# Matthew Morcaldi 2015
# Version 0.1.2 last update (1/19/2015)
# w00t be in r00t
# Config ------------------------------------------------
outfile="linux_info.txt"
#--------------------------------------------------------
#### uncomment (set -x) for debug
# set -x
checkroot() {
if [ $UID -ne 0 ] ; then
echo "User has insufficient privilege."
exit 4
fi
}
function shouldSkip {
read -p "$*" line
test "$line" = 'n' -o "$line" = 's'
return $?
}
header() {
echo '' | tee -a $outfile
echo '----------------------' | tee -a $outfile
echo "[*] $title" | tee -a $outfile
echo '----------------------' | tee -a $outfile
}
function pause(){
read -p "$*"
}
display_os() {
title="System"
header $title
dmidecode -t 1 | grep -i 'serial' | sed 's/^ *//' | tee $outfile
cat /etc/*-release | tee -a $outfile
dmidecode -t system -q | egrep -i 'Manufacturer: |Product|UUID' | \
sed 's/^ *//' | tee -a $outfile
/usr/bin/ipmitool bmc info | egrep -i 'Firmware revision' | tee -a $outfile
dmidecode -t bios -q | egrep -i 'version|vendor' | \
sed 's/^ *//' | tee -a $outfile
}
display_network_oob() {
title="Drac-Info"
header $title
ipmitool lan print | egrep -i 'IP Address|MAC Address|Default Gateway IP|Subnet Mask' | tee -a $outfile
}
display_networking() {
title="Networking"
header $title
ifconfig -a | tee -a $outfile
}
display_bonding() {
if [ -f /proc/net/bonding/bond0 ] ; then
title="Bonding"
header $title
cat /proc/net/bonding/bond0 | sed 's/^ *//' | tee -a $outfile
fi
}
display_switch_info() {
if [ -f /usr/sbin/lldpctl ] ; then
title="Switch-Port-Information"
header $title
lldpctl | egrep '(Interface|VLAN|PortDescr|SysName)' | \
sed 's/^ *//' | tee -a $outfile
else
echo "Switch file LLDPCTL does not exist skipping"
fi
}
show_disks_dell() {
if [ -f /opt/MegaRAID/MegaCli/MegaCli64 ] ; then
title="Dell-Raid"
header $title
/opt/MegaRAID/MegaCli/MegaCli64 -PDList -aAll | \
egrep -i 'count|^Device Id: |firmware state' | \
grep -v 'Count: 0' | \
perl -p -e 's/Firmware state: (.*)$/Firmware state: $1\n/' | tee -a $outfile
/opt/MegaRAID/MegaCli/MegaCli64 -LDInfo -Lall -aALL | \
egrep -i 'virtual|cache|state|raid level|name|size|number' | tee -a $outfile
else
echo "skipping Dell disk check"
fi
}
show_battery_dell() {
if [ -f /opt/MegaRAID/MegaCli/MegaCli64 ]; then
title="Dell-Battery"
header $title
/opt/MegaRAID/MegaCli/MegaCli64 -AdpBbuCmd -GetBbuStatus -a0 | \
egrep -i 'isSOHGood|Charger Status|Capacity|Relative|Charging' | \
sed 's/^ *//' | tee -a $outfile
else
echo "skipping Dell battery check"
fi
}
show_disks_hp() {
if [ -f /usr/sbin/hpacucli ]; then
title="HP-Raid"
header $title
hpacucli ctrl all show config detail | \
sed 's/^ *//' | tee -a $outfile
else
echo "skipping HP disk check"
fi
}
show_battery_hp() {
if [ -f /usr/sbin/hpacucli ]; then
title="HP-Battery"
header $title
hpacucli ctrl all show status | \
sed 's/^ *//' | tee -a $outfile
else
echo "skipping HP battery check"
fi
}
display_dimms() {
title="Memory"
header $title
cat /proc/meminfo|grep MemTotal | tee -a $outfile
free -g | tee -a $outfile
dmidecode -t Memory Device -q |egrep -i 'Size:|Type: D|Locator: D' | tee -a $outfile
}
sel_list() {
title="SEL"
header $title
ipmitool sel elist | tee -a $outfile
}
# Where the magic happens -------------------------------------
# TODO: Add Optarg Support
# http://wiki.bash-hackers.org/howto/getopts_tutorial
checkroot
pause 'Press [Enter] key to continue into DONUTS Version: 0.1.1. ..'
service ipmi start
if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Display OS". '
then
display_os
fi
if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Display Network OOB". '
then
display_network_oob
fi
if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Display Network". '
then
display_networking
fi
if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Display Bonding". '
then
display_bonding
fi
if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Display Switch Port Information". '
then
display_switch_info
fi
if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Display Memory". '
then
display_dimms
fi
if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "System Event List". '
then
sel_list
fi
if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "HP RAID". '
then
show_disks_hp
fi
if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "HP RAID Battery". '
then
show_battery_hp
fi
if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Dell RAID". '
then
show_disks_dell
fi
if ! shouldSkip 'Press [Enter] key to continue, [n] or [s] to skip "Dell RAID Battery". '
then
show_battery_dell
fi
pause 'Press [Enter] key to continue. ..'
service ipmi stop
cat << "EOF"
_ _,---._
,-',' `-.___
/-;' `._
/\/ ._ _,'o \
( /\ _,--'\,','"`. )
|\ ,'o \' //\
| \ / ,--'""`-.
: \_ _/ ,-' `-._
\ `--' / )
`. \`._ ,' ________,','
.--` ,' ,--` __\___,;'
\`.,-- ,' ,`_)--' /`.,'
\( ; | | ) (`-/
`--'| |) |-/ DONUTS Version: 0.1.1
| | | | |
| | |,.,-. | |_
| `./ / )---` )
_| / ,', ,-'
,'|_( /-<._,' |--,
| `--'---. \/ \
| / \ /\ \
,-^---._ | \ / \ \
,-' \----' \/ \--`.
/ \ \ \
EOF