web-shell编程之实时监控IP存活
#!/bin/bash
# ping函数
myping(){
ping -c 2 -i 0.3 -W 1 $1 &>/dev/null
if [ $? -eq 0 ];then
echo "$1 is up" >> 1.txt
else
echo "$1 is down" >> 1.txt
fi
}
for i in {1,10,30,40,51,55,{66..70},100,101}
do
myping 10.168.1.$i &
done
sleep 5
#结果重定向排序
cat 1.txt|sort -t "." -k4n,4 -o 1.txt
cat -n 1.txt > 2.txt
#定义网站渲染html文本,需要nginx支撑
HTML='/usr/share/nginx/html/index.html'
#echo -e "\033[32mProcess,start,Please wait...\033[1m"
#echo "---------------------------------------------"
cat >$HTML<<EOF
<html>
<meta http-equiv="refresh" content="60">
<body>
<h1>主机监控</h1>
<hr color="green">
<table border="1" width="350" cellpadding="0" cellspaceing="0">
<tr align="center"><td>ID</td><td>IP</td><td>存活状态</td></tr>
EOF
#将之前的2.txt文件取值赋予对应变量
while read line
do
ID=`echo $line | awk '{print $1}'`
IP=`echo $line | awk '{print $2}'`
STATE=`echo $line | awk '{print $4}'`
# echo $IP $STATE
# sleep 0.1
echo "<tr><td>$ID</td><td>$IP</td><td><a target="blank" href=$STATE>$STATE</a></td></tr>" >>$HTML
done < 2.txt
#html结尾
cat >>$HTML<<EOF
</table>
</body>
</html>
EOF
#清空临时数据
rm -rf 1.txt
#echo "done"
转载请注明-MrZ-个人博客
THE END
0
二维码
海报
web-shell编程之实时监控IP存活
#!/bin/bash
# ping函数
myping(){
ping -c 2 -i 0.3 -W 1 $1 &>/dev/null
if [ $? -eq 0 ];then
echo "$1 is up" >> 1.txt
else
echo "$1 is down" >> 1.txt
fi
}
fo……
共有 0 条评论