首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将bash脚本输出重定向到不包括菜单的日志文件

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

Stack Overflow用户
提问于 2020-06-21 01:34:34
回答 1查看 130关注 0票数 0

首先感谢你的帮助真的很感谢,我有一个简单的bash脚本,他正在读取服务器列表,命令列表和回滚命令列表,脚本运行良好,

但我的问题是日志,所以我希望将控制台上的输出记录到日志文件中。问题是,当我打开日志文件时,我可以看到包含日志的菜单,以及从日志文件中排除菜单的任何帮助,

使用以下命令导出日志im:

代码语言:javascript
复制
exec > >(tee -a ${log_file} )
exec 2> >(tee -a ${log_file} >&2)

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

代码语言:javascript
复制
**************************************************************
              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.

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

代码语言:javascript
复制
[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.

非常感谢你的帮助

他是我的代码:

代码语言:javascript
复制
#!/bin/bash
clear
#Echo with colors*****************
normal=`echo "\033[m"`
menu=`echo "\033[36m"` #Blue
number=`echo "\033[33m"` #yellow
bgred=`echo "\033[41m"`
fgred=`echo "\033[31m"`
PASSED=$'\e[32mPASSED\e[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 '%*s\n'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
            printf "\n"
            printf  "Starting executing commands for : $server \n" 
            printf '%*s\n'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
            printf "\n"
            #Commands BEGIN.========================================================
            #'ssh $server "hostname" < /dev/null;'
            printf "\n"
                while read command; do
                    printf '[ %s ] Command executed : %s\n' "$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 "\033[m"`
menu=`echo "\033[36m"` #Blue
number=`echo "\033[33m"` #yellow
bgred=`echo "\033[41m"`
fgred=`echo "\033[31m"`
PASSED=$'\e[32mPASSED\e[0m'
UP=$'\e[32mUP\e[0m'
DOWN=$'\e[31mDOWN\e[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 "\033[m"`
    menu=`echo "\033[36m"` #Blue
    number=`echo "\033[33m"` #yellow
    bgred=`echo "\033[41m"`
    fgred=`echo "\033[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 "\033[01;31m"` # bold red
    normal=`echo "\033[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 : %s\n ' "$UP" "$output "
                            else
                        printf '\n'
                        printf '[ %s ] Server : %s\n ' "$DOWN" "$output "
                        fi
                done
            show_menu;
        ;;
        2) clear;
            option_picked "Option 2 : Display the config.";
                while read server; do
            #Title
            printf '%*s\n'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
            printf "\n"
            printf  "Starting executing commands for : $server \n" 
            printf '%*s\n'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
            printf "\n"
            #Commands BEGIN.========================================================
            #'ssh $server "hostname" < /dev/null;'
            printf "\n"
                    while read command; do
                        printf '[ %s ] Command executed : %s\n' "$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 '%*s\n'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
            printf "\n"
            printf  "Starting executing commands for : $server \n" 
            printf '%*s\n'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
            printf "\n"
            #Commands BEGIN.========================================================
            #'ssh $server "hostname" < /dev/null;'
            printf "\n"
                    while read command; do
                        printf '[ %s ] Command executed : %s\n' "$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 '%*s\n'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
            printf "\n"
            printf  "Starting executing commands for : $server \n" 
            printf '%*s\n'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
            printf "\n"
            #Commands BEGIN.========================================================
            #'ssh $server "hostname" < /dev/null;'
            printf "\n"
                    while read command; do
                        printf '[ %s ] Command executed : %s\n' "$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 '%*s\n'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
            printf "\n"
            printf  "Starting executing commands for : $server \n" 
            printf '%*s\n'  "${COLUMNS:-$(tput cols)}" '' | tr ' ' _
            printf "\n"
            #Commands BEGIN.========================================================
            #'ssh $server "hostname" < /dev/null;'
            printf "\n"
                    while read command; do
                        printf '[ %s ] Command executed : %s\n' "$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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-21 01:52:05

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

代码语言:javascript
复制
...

  # 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62489418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档