標籤:
#!/bin/bash# 示範使用 shell 指令碼建立 HTML 報告的樣本指令碼# Web directoryWEB_DIR=/var/www/html# A little CSS and table layout to make the report look a little nicerecho "<HTML><HEAD><style>.titulo{font-size: 1em; color: white; background:#0863CE; padding: 0.1em 0.2em;}table{border-collapse:collapse;}table, td, th{border:1px solid black;}html{text-align:center;}</style><meta http-equiv=‘Content-Type‘ content=‘text/html; charset=UTF-8‘ /></HEAD><BODY align=center>" > $WEB_DIR/report.html# View hostname and insert it at the top of the html bodyHOST=$(hostname)echo "Filesystem usage for host <strong>$HOST</strong><br>Last updated: <strong>$(date)</strong><br><br><table border=‘1‘><tr><th class=‘titulo‘> HOSTNAME</td><th class=‘titulo‘>IP</td></tr>" >> $WEB_DIR/report.html#擷取IPifconfig|grep "inet addr:"|while read line; doecho "<tr><td align=‘center‘>" >> $WEB_DIR/report.htmlecho $HOST >> $WEB_DIR/report.htmlecho "</td><td align=‘center‘>" >> $WEB_DIR/report.htmlecho $line | awk -F ‘:‘ ‘{print $2}‘| awk -F‘ ‘ ‘{print $1}‘ >> $WEB_DIR/report.htmlecho "</td></tr>" >> $WEB_DIR/report.htmldoneecho "</table><br><br><table border=‘1‘><tr><th class=‘titulo‘> Machine Info</td><th class=‘titulo‘>Value</td></tr>" >> $WEB_DIR/report.html#擷取機器SN號dmidecode -t 1|egrep -e "Product|Serial"|while read line; doecho "<tr><td align=‘center‘>" >> $WEB_DIR/report.htmlecho $line | awk -F ‘:‘ ‘{print $1}‘ >> $WEB_DIR/report.htmlecho "</td><td align=‘center‘>" >> $WEB_DIR/report.htmlecho $line | awk -F ‘:‘ ‘{print $2}‘ >> $WEB_DIR/report.htmlecho "</td></tr>" >> $WEB_DIR/report.htmldoneecho "</table><br><br><table border=‘1‘><tr><th class=‘titulo‘>Filesystem</td><th class=‘titulo‘>Size</td><th class=‘titulo‘>Use %</td></tr>" >> $WEB_DIR/report.html# 擷取硬碟使用方式df -Ph | grep -vi 檔案系統|while read line; doecho "<tr><td align=‘center‘>" >> $WEB_DIR/report.htmlecho $line | awk ‘{print $1}‘ >> $WEB_DIR/report.htmlecho "</td><td align=‘center‘>" >> $WEB_DIR/report.htmlecho $line | awk ‘{print $2}‘ >> $WEB_DIR/report.htmlecho "</td><td align=‘center‘>" >> $WEB_DIR/report.htmlecho $line | awk ‘{print $5}‘ >> $WEB_DIR/report.htmlecho "</td></tr>" >> $WEB_DIR/report.htmldoneecho "</table></BODY></HTML>" >> $WEB_DIR/report.html
SHELL 產生HTML