shell利用mysql表项的icmp检测

时间:2024-01-09 19:46:20

作者:邓聪聪

  利用mysql的表项记录IP地址和对应状态

+----+-----------------+--------+--------+
| id | ip_host | desc | status |
+----+-----------------+--------+--------+
| | 8.8.8.8 | 电信 | |
| | 114.114.114.114 | 联通 | |
| | 114.114.114.117 | 测试 | |
| | 2.2.2.2 | 电信 | |
| | 222.222.222.1 | 电信 | |
| | 223.3.2.2 | 谷歌 | |
| | 23.24.24.32 | 测完 | |
| | 232.23.22.32 | 测试 | |
+----+-----------------+--------+--------+

shell脚本内容:

#!/bin/sh
PATH=$PATH DB_table="iphost"
DB_status="status"
DB_vlues="ip_host"

ping_ip(){
sql_host_status=`mysql -uusername -ppassword test -e "select status from $DB_table where ip_host='$1'"|grep -v $DB_status`
ping -c -W $ > /dev/null >&
if [ $? -ne $sql_host_status ]
then
des=`mysql -uusername -ppassword test -e "select * from iphost where $DB_vlues like '$1'"|awk '{print $3}'|grep -v desc`
if [ $sql_host_status == ]
then
#$sql_host_status=""
echo "$1 is bad"
#更新状态为1
mysql -uusername -ppassword test -e "update $DB_table set $DB_status='1' where ($DB_vlues='$1')"
else
#$sql_host_status=""
echo "$1 is ok"
#更新状态为0
mysql -uusername -ppassword test -e "update $DB_table set $DB_status='0' where ($DB_vlues='$1')"
fi
else
continue
fi
}
while true
do
echo ""
while true
do
for i in `mysql -uusername -ppassword test -e "select ip_host from iphost" |awk '{print $1}'|grep -v ip_host`
do
ping_ip $i
done
done
sleep
break
done