#!/bin/bash #user: yp NGINX_LOG=/var/log/nginx/access.log DBNAME=nginx DBPWD=123456 DBUSER=yp DBHOST=127.0.0.1 DBPORT=13306 function main(){ local clientip=($(awk '{print $1}' ${NGINX_LOG} | sort | uniq -c | sort -nr -k1 | head -n 10)) for i in {1..20..2} do j=`expr ${i}-1` mysql -u${DBUSER} -p${DBPWD} -h ${DBHOST} -P ${DBPORT} ${DBNAME} -e 'INSERT INTO client_ip (ip_addr,ip_max_values, status_datetime) VALUES ("'${clientip[$i]}'", "'${clientip[$j]}'", NOW());' done } main