简体   繁体   中英

Bash script problem generating IP error report for school project

I have a problem with my script, I cannot generate an HTML report of all the IPs which are pinged thanks to the list of IPs which are specified when the script is launched. Could someone help me please, thank you very much for your help which will be essential for the smooth running of the school project.

#!/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

You have (at least) quoting problem on your html. All " will be remove by the shell. To keep a '"' escape it like this \" or suround it by '

If you just print your html on a terminal you will not see any " I think (not tested).

For more debuging give us more information please.

Use a here doc to get a string without processing substitutions.

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

I see from your comment that your sendmail is not working. The command seems to be fine per se and it works on my machine. Make sure that you can manually send messages out with that command: if not you need to troubleshoot your sendmail setup. Also your resultatping variable gets the output of your ping command but the result is sent to the file mail.txt without HTML: the variable htmlsite is never used. Finally IMHO it's probably not a great idea to call a variable ping and use it as an argument for the ping command...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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