繁体   English   中英

Bash 脚本问题为学校项目生成 IP 错误报告

[英]Bash script problem generating IP error report for school project

我的脚本有问题,由于启动脚本时指定的 IP 列表,我无法生成所有被 ping 的 IP 的 HTML 报告。 有人可以帮我吗,非常感谢您的帮助,这对于学校项目的顺利进行至关重要。

#!/bin/bash
# start of html

# end of html

htmlsite=$(echo "<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
</head>
<body>
    <style>
        body{
            margin: auto;
        }
        table{
            border: solid 1px black;
            margin: auto;
        }
        td{
            border: solid 1px black;
            margin: auto;
            text-align: center;
        }
        th{
            border: solid 1px black;
            margin: auto;
            text-align: center;
        }
    </style>
    <table>
        <tr>
            <th>INFO SERVEUR</th>
        </tr>
        <tr>
            <th>IP</th>
            <th>STATUS</th>
        </tr>
        <tr>
            <td>[ 8.8.8.8 ]</td>
            <td>[ OK ]</td>
        </tr>
    </table>
</body>
</html>")

# color for all "echo" and "printf"
reset="\033[0;m"
BIGreen="\e[1;92m"
BIRed="\e[1;91m"
BICyan="\e[1;96m"
BIPurple="\e[1;95m"
BIWhite="\e[1;97m"
BIYellow="\e[1;93m"

# argument
args="2"

# the date
dateresult="$(date)"

# start of ping 
if [ $# -ne $args ] # if a user does not enter both value
then
    clear
    printf "=========================================================================================================\n"
    printf "\n$BIYellow/!\ $BIRed An error has been encountered  $BIYellow/!\ \n"
    printf $BICyan"\nYou will be enter two values, like that :\n"
    printf $BIGreen"\n=> Name of the file with the IPs to be tested\n"
    printf $BIGreen"=> Name for the site generation"
    printf "\n=> Exemple :\n"
    printf $BIWhite"\n=> ./pinger.sh list.txt site_a_generer\n\n"
    printf $reset"=========================================================================================================\n\n"
else # if the file does not exist the user will be notified
    clear
    if [ ! -e $1 ] 
    then
        printf $BIRed"\n░██╗░░░░░░░██╗░█████╗░██████╗░███╗░░██╗██╗███╗░░██╗░██████╗░\n"
        printf "░██║░░██╗░░██║██╔══██╗██╔══██╗████╗░██║██║████╗░██║██╔════╝░\n"
        printf "░╚██╗████╗██╔╝███████║██████╔╝██╔██╗██║██║██╔██╗██║██║░░██╗░\n"
        printf "░░████╔═████║░██╔══██║██╔══██╗██║╚████║██║██║╚████║██║░░╚██╗\n"
        printf "░░╚██╔╝░╚██╔╝░██║░░██║██║░░██║██║░╚███║██║██║░╚███║╚██████╔╝\n"
        printf "░░░╚═╝░░░╚═╝░░╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚══╝╚═╝╚═╝░░╚══╝░╚═════╝░\n"
        printf "\nError the $BIWhite/| $BIRed"$1" $BIWhite|\ "$BIRed"does not exist\n\n"
        
    else # the start of the ping
    clear
    printf $BIPurple"\n\n"
    printf "██████╗░████████╗░██████╗  ░██████╗██╗░█████╗░\n"
    sleep 0.1
    printf "██╔══██╗╚══██╔══╝██╔════╝  ██╔════╝██║██╔══██╗\n"
    sleep 0.1
    printf "██████╦╝░░░██║░░░╚█████╗░  ╚█████╗░██║██║░░██║\n"
    sleep 0.1
    printf "██╔══██╗░░░██║░░░░╚═══██╗  ░╚═══██╗██║██║░░██║\n"
    sleep 0.1
    printf "██████╦╝░░░██║░░░██████╔╝  ██████╔╝██║╚█████╔╝\n"
    sleep 0.1
    printf "╚═════╝░░░░╚═╝░░░╚═════╝░  ╚═════╝░╚═╝░╚════╝░\n\n"
    sleep 0.1
    printf $BIWhite"=> ======================= ENT MAROILLES ========================\n\n"
    printf $BIWhite"=> Date : $dateresult\n\n"

        
        for ping in $(cat $1) # take the information from the text file to put in "ping"
        do    
        ping=$(echo ${ping})
        IP="$ping"
            oct1="$(echo $IP | cut -d"." -f1)" # first byte IPs
            oct2="$(echo $IP | cut -d"." -f2)" # second byte IPs
            oct3="$(echo $IP | cut -d"." -f3)" # third byte IPs
            oct4="$(echo $IP | cut -d"." -f4)" # fourth byte IPs
                    if [ -z $oct1 ] || [ $oct1 -lt 0 ] || [ $oct1 -gt 255 ]  # if first byte is less than 0 and if is greater than 255
                    then
                        echo -e "=> "$BIRed"$ping : "$BIYellow"[ UNKNOWN IP ]"$reset""
                    else
                        if [ -z $oct2 ] || [ $oct2 -lt 0 ] || [ $oct2 -gt 255 ] # if second byte is less than 0 and if is greater than 255
                        then
                            echo -e "=> "$BIRed"$ping : "$BIYellow"[ UNKNOWN IP ]"$reset""
                        else
                            if [ -z $oct3 ] || [ $oct3 -lt 0 ] || [ $oct3 -gt 255 ]  # if thrid byte is less than 0 and if is greater than 255
                            then
                                echo -e "=> "$BIRed"$ping : "$BIYellow"[ UNKNOWN IP ]"$reset"" 
                            else 
                                if [ -z $oct4 ] || [ $oct4 -lt 0 ] || [ $oct4 -gt 255 ]  # if fourth byte is less than 0 and if is greater than 255
                                then
                                    echo -e "=> "$BIRed"$ping : "$BIYellow"[ UNKNOWN IP ]"$reset"" # print unknown IPs
                                        
                                else
                                      
                                resultatping=$(ping -w 1 -c 1 $ping)
                                    if [ $? -eq 0 ] 
                                    then
                                        printf "=> $BIWhite$ping : "$BIGreen"[ OK ]\n"$reset""
                                    else
                                        printf "=> $BIWhite$ping : "$BIRed"[ NOT OK ]\n"$reset""
                                    fi
                                fi
                            fi
                        fi
                    fi                 
                                   
    done    
        
printf $BIWhite"\n=> ============================ END =============================\n"

# create DOCTYPE HTML


# SENDMAIL but is not working 
    mailcreate=$(echo ""$1""$resultatping"" >> mail.txt)
    printf "\n=> Write your Email => "    
    read email
    emailsend=$(sendmail $email < mail.txt)
    printf "=> Sending information at "$email"\n"

       
fi
    
fi

您的 html 有(至少)引用问题。 所有 " 将被 shell 删除。要保留 '"' 像这样逃脱它 \" 或用 ' 包围它

如果您只是在终端上打印 html,您将看不到任何“我认为(未经测试)。

如需更多调试,请给我们更多信息。

使用 here doc 获取字符串而不处理替换。

htmlsite=$(cat - <<EOF
<html lang="en">
...
</html>
EOF
)

我从您的评论中看到您的 sendmail 无法正常工作。 该命令本身似乎很好,并且可以在我的机器上运行。 确保您可以使用该命令手动发送消息:如果不能,您需要对 sendmail 设置进行故障排除。 此外,您的resultatping变量获得 ping 命令的 output,但结果被发送到文件mail.txt而没有 HTML:变量htmlsite从未使用过。 最后恕我直言,调用变量ping并将其用作 ping 命令的参数可能不是一个好主意......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM