将 bash 脚本输出重定向到日志文件,不包括菜单



>首先感谢您的帮助,我有一个简单的bash脚本,他正在阅读服务器列表,命令列表和回滚命令列表,脚本工作正常,

但我的问题与日志有关,因此我想将控制台上的输出记录到日志文件中。 问题是当我打开日志文件时,我可以看到带有日志的菜单,任何帮助从日志文件中排除菜单,

使用 导出日志 IM

exec > >(tee -a ${log_file} )
exec 2> >(tee -a ${log_file} >&2)

执行脚本(代码如下(后,我看到的是:

**************************************************************
test script
**************************************************************
** 1) Test ip connectivity for all the servers.             **
** 2) Display the default config before apply the hardening.** 
** 3) Executing the hardening changes.                      **
** 4) Display the new values after the hardening applied.   **
** 5) Rollback to the default config.                       **
**************************************************************
Please enter a menu option and enter or x to exit. 3
Option 3 : Executing config.

当我检查日志时,我可以看到一个很好的日志,但菜单:(

[root@host1 newscript]# 
[root@host1 newscript]# 
[root@host1 newscript]# cat Hardening_log.txt 
**************************************************************
test script
**************************************************************
** 1) Test ip connectivity for all the servers.             **
** 2) Display the default config before apply the hardening.** 
** 3) Executing the hardening changes.                      **
** 4) Display the new values after the hardening applied.   **
** 5) Rollback to the default config.                       **
**************************************************************
Please enter a menu option and enter or x to exit. 
Option 1 : Test ip connectivity for all the servers.
[ UP ] Server : 192.168.0.182 

[ UP ] Server : 192.168.0.183 

**************************************************************
test script
**************************************************************
** 1) Test ip connectivity for all the servers.             **
** 2) Display the default config before apply the hardening.** 
** 3) Executing the hardening changes.                      **
** 4) Display the new values after the hardening applied.   **
** 5) Rollback to the default config.                       **
**************************************************************
Please enter a menu option and enter or x to exit. 
Option 2 : Display the config.
___________________________________________________________________________________________________________________________________________________________________________
Starting executing commands for : 192.168.0.182 
___________________________________________________________________________________________________________________________________________________________________________

[ PASSED ] Command executed : cat /etc/ssh/sshd_config | grep MaxAuthTries
Printing commands result : #MaxAuthTries 6
[ PASSED ] Command executed : chage -l sofiane | grep "Account expires"
Printing commands result : Account expires                                              : Dec 31, 2022
___________________________________________________________________________________________________________________________________________________________________________
Starting executing commands for : 192.168.0.183 
___________________________________________________________________________________________________________________________________________________________________________

[ PASSED ] Command executed : cat /etc/ssh/sshd_config | grep MaxAuthTries
Printing commands result : #MaxAuthTries 6
[ PASSED ] Command executed : chage -l sofiane | grep "Account expires"
Printing commands result : Account expires                                              : Dec 31, 2022

**************************************************************
test script
**************************************************************
** 1) Test ip connectivity for all the servers.             **
** 2) Display the default config before apply the hardening.** 
** 3) Executing the hardening changes.                      **
** 4) Display the new values after the hardening applied.   **
** 5) Rollback to the default config.                       **
**************************************************************
Please enter a menu option and enter or x to exit. 
Option 3 : Executing config.
___________________________________________________________________________________________________________________________________________________________________________
Starting executing commands for : 192.168.0.182 
___________________________________________________________________________________________________________________________________________________________________________

[ PASSED ] Command executed : sed -i '/^#MaxAuthTries/s/6/2/' /etc/ssh/sshd_config
[ PASSED ] Command executed : /usr/bin/chage -E 2021-12-31 sofiane
___________________________________________________________________________________________________________________________________________________________________________
Starting executing commands for : 192.168.0.183 
___________________________________________________________________________________________________________________________________________________________________________

[ PASSED ] Command executed : sed -i '/^#MaxAuthTries/s/6/2/' /etc/ssh/sshd_config
[ PASSED ] Command executed : /usr/bin/chage -E 2021-12-31 sofiane

**************************************************************
test script
**************************************************************
** 1) Test ip connectivity for all the servers.             **
** 2) Display the default config before apply the hardening.** 
** 3) Executing the hardening changes.                      **
** 4) Display the new values after the hardening applied.   **
** 5) Rollback to the default config.                       **
**************************************************************
Please enter a menu option and enter or x to exit.

谢谢你如此糊涂的帮助

他是我的代码:

#!/bin/bash
clear
#Echo with colors*****************
normal=`echo "33[m"`
menu=`echo "33[36m"` #Blue
number=`echo "33[33m"` #yellow
bgred=`echo "33[41m"`
fgred=`echo "33[31m"`
PASSED=$'e[32mPASSEDe[0m'
#*********************************
servers="servers.txt"
commands="commands.txt"
log_file="log.txt"
now=$(date +"%F_%H-%M")


exec > >(tee -a ${log_file} )
exec 2> >(tee -a ${log_file} >&2)
while read server; do
#Title
printf '%*sn'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
printf "n"
printf  "Starting executing commands for : $server n" 
printf '%*sn'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
printf "n"
#Commands BEGIN.========================================================
#'ssh $server "hostname" < /dev/null;'
printf "n"
while read command; do
printf '[ %s ] Command executed : %sn' "$PASSED" "$command"
printf "Printing commands result : " & ssh $server $command  < /dev/null;
printf "n"
done < $commands
done < $servers
mv log.txt $now"-log.txt"
#Commands END.==========================================================


#!/bin/bash
clear
#Colors*****************
normal=`echo "33[m"`
menu=`echo "33[36m"` #Blue
number=`echo "33[33m"` #yellow
bgred=`echo "33[41m"`
fgred=`echo "33[31m"`
PASSED=$'e[32mPASSEDe[0m'
UP=$'e[32mUPe[0m'
DOWN=$'e[31mDOWNe[m'
#*********************************
#Files needed*****************
servers="servers.txt"
commands="execute.txt"
default_config="check_default.txt"
rollback="roll_back.txt"
log_file="log.txt"
now=$(date +"%F_%H-%M")
#*****************************
#To log the output*****************
log_file=Hardening_log.txt
exec > >(tee -a ${log_file} )
exec 2> >(tee -a ${log_file} >&2)
#*****************************
show_menu(){
normal=`echo "33[m"`
menu=`echo "33[36m"` #Blue
number=`echo "33[33m"` #yellow
bgred=`echo "33[41m"`
fgred=`echo "33[31m"`
printf "n${menu}**************************************************************${normal}n"
printf "              test script"
printf "n${menu}**************************************************************${normal}n"
printf "${menu}**${number} 1)${menu} Test ip connectivity for all the servers.             **${normal}n"
printf "${menu}**${number} 2)${menu} Display the default config before apply the hardening.** ${normal}n"
printf "${menu}**${number} 3)${menu} Executing the hardening changes.                      **${normal}n"
printf "${menu}**${number} 4)${menu} Display the new values after the hardening applied.   **${normal}n"
printf "${menu}**${number} 5)${menu} Rollback to the default config.                       **${normal}n"
printf "${menu}**************************************************************${normal}n"
printf "Please enter a menu option and enter or ${fgred}x to exit. ${normal}"
read opt
}
option_picked(){
msgcolor=`echo "33[01;31m"` # bold red
normal=`echo "33[00;00m"` # normal white
message=${@:-"${normal}Error: No message passed"}
printf "${msgcolor}${message}${normal}n"
}
clear
show_menu
while [ $opt != '' ]
do
if [ $opt = '' ]; then
exit;
else
case $opt in
1) clear;
option_picked "Option 1 : Test ip connectivity for all the servers.";
cat servers.txt |  while read output
do
ping -c 2 "$output" > /dev/null
if [ $? -eq 0 ]; then
printf 'n'
printf '[ %s ] Server : %sn ' "$UP" "$output "
else
printf 'n'
printf '[ %s ] Server : %sn ' "$DOWN" "$output "
fi
done
show_menu;
;;
2) clear;
option_picked "Option 2 : Display the config.";
while read server; do
#Title
printf '%*sn'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
printf "n"
printf  "Starting executing commands for : $server n" 
printf '%*sn'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
printf "n"
#Commands BEGIN.========================================================
#'ssh $server "hostname" < /dev/null;'
printf "n"
while read command; do
printf '[ %s ] Command executed : %sn' "$PASSED" "$command"
printf "Printing commands result : " & ssh $server $command  < /dev/null;
printf "n"
done < $default_config
done < $servers
show_menu;
;;
3) clear;
option_picked "Option 3 : Executing config.";
while read server; do
#Title
printf '%*sn'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
printf "n"
printf  "Starting executing commands for : $server n" 
printf '%*sn'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
printf "n"
#Commands BEGIN.========================================================
#'ssh $server "hostname" < /dev/null;'
printf "n"
while read command; do
printf '[ %s ] Command executed : %sn' "$PASSED" "$command"
#printf "Printing commands result : " & ssh $server $command  < /dev/null;
ssh $server $command  < /dev/null;
printf "n"
done < $commands
done < $servers
show_menu;
;;
4) clear;
option_picked "Option 4 : Display the new config after the hardening applied.";
while read server; do
#Title
printf '%*sn'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
printf "n"
printf  "Starting executing commands for : $server n" 
printf '%*sn'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
printf "n"
#Commands BEGIN.========================================================
#'ssh $server "hostname" < /dev/null;'
printf "n"
while read command; do
printf '[ %s ] Command executed : %sn' "$PASSED" "$command"
printf "Printing commands result : " & ssh $server $command  < /dev/null;
printf "n"
done < $default_config
done < $servers
show_menu;
;;
5) clear;
option_picked "Option 5 : Rollback to the default config.";
while read server; do
#Title
printf '%*sn'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
printf "n"
printf  "Starting executing commands for : $server n" 
printf '%*sn'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
printf "n"
#Commands BEGIN.========================================================
#'ssh $server "hostname" < /dev/null;'
printf "n"
while read command; do
printf '[ %s ] Command executed : %sn' "$PASSED" "$command"
#printf "Printing commands result : " &
ssh $server $command  < /dev/null;
printf "n"
done < $rollback
done < $servers
show_menu;
;;
x)exit;
;;
n)exit;
;;
*)clear;
option_picked "Pick an option from the menu";
show_menu;
;;
esac
fi
done

可以将初始标准输出复制到不同的文件句柄(例如,3(,并将菜单发送到该句柄。设置后(exec 3>&1(,将>&3添加到输出不应转到日志文件的任何命令中。

...
# Keep handle to original stdout on fd 3
exec 3>&1
# capture stdout/stderr to log file
exec > >(tee -a ${log_file} )
exec 2> >(tee -a ${log_file} >&2)
...
function show_menu {
}
...
# Output the clear seq and the menu to terminal only
clear >&3
show_menu >&3
# Rest of the code
while [ $opt != '' ] 
...
done

最新更新