我的腳本有問題,由于腳本啟動時指定的 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
uj5u.com熱心網友回復:
您(至少)在 html 上有參考問題。所有 " 將被外殼洗掉。要保持 '"' 像這樣轉義它 \" 或用 ' 包圍它
如果您只是在終端上列印您的 html,您將看不到任何“我認為(未測驗)。
如需更多除錯,請給我們更多資訊。
uj5u.com熱心網友回復:
使用 here doc 獲取字串而不處理替換。
htmlsite=$(cat - <<EOF
<html lang="en">
...
</html>
EOF
)
uj5u.com熱心網友回復:
我從您的評論中看到您的 sendmail 無法正常作業。該命令本身似乎很好,并且可以在我的機器上運行。確保您可以使用該命令手動發送訊息:如果不是,您需要對您的 sendmail 設定進行故障排除。此外,您的resultatping變數會獲取 ping 命令的輸出,但結果將發送到mail.txt沒有 HTML的檔案:htmlsite從未使用過該變數。最后恕我直言,呼叫變數ping并將其用作 ping 命令的引數可能不是一個好主意......
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/373440.html
下一篇:將行中的資料轉換為列
