#!/bin/bash

LOGFILE=$1
TXT=./ip_address.txt
Tmp=./ip_list.txt
n=0
if [ $# -eq 1 ]; then

  if [[ $LOGFILE =~ .*\.gz$ ]]; then
    COMM="zcat"
  else
    COMM="cat"
  fi 
  
  if [ -f $TXT ]; then
   >$TXT
  fi

  >$Tmp

 $COMM $1|awk '{ip[$1]++}END{for(i in ip){print i "   " ip[i]}}' >$Tmp

  while read ip num ;

  do
     {
     # let n++
     # echo "共计 $(wc -l ${Tmp}|awk '{print $1}')行,正在进行${n}行"

      curl -s https://ip.cn/index.php?ip=$ip|awk -v i=$ip -v N=$num -F "[><]+" '/id="tab0_address"/{printf  "%-20s%-40s%-0s\n",N,i,$3}'>>$TXT

	}&

  done < $Tmp

  wait

  echo "统计完成,文件为当前目录下的${TXT#*/}, 共计:$(wc -l ${Tmp}|awk '{print $1}')行"

  read -p "是否要直接查看${TXT#*/}文件内容?[yes|no]: " CAT

  case $CAT in
    Y|y|Yes|yes|YES)
      cat ${TXT} |more 
    ;;

    *)
      echo "输出完成,共计:$(wc -l ${Tmp}|awk '{print $1}')行,请查看当前目录下的${TXT#*/}文件"
      exit
   esac

  rm -f $Tmp

else

  echo -e "请传出脚本参数 \n${0} 日志路径名称(例如:/www/wwwlogs/test.log)"

fi            

