查询ip归属地的shell脚本

    经常会遇到需要查找ip归属地,尤其是批量查找的时候,使用脚本就比较方便了,在网上找到一个,自己更改了一下,修复了在linux下出现中文乱码的问题,代码如下:

点击(此处)折叠或打开

  1. #!/bin/bash
  2. #Purpose: 查找ip地址所在地
  3. ipp (){
  4. exec < $1
  5. while read a
  6. do
  7. sring=`curl -s "http://ip138.com/ips138.asp?ip=${a}&action=2"| iconv -f gb2312 -t utf-8|grep '<ul class="ul1"><li>' | awk -F '[<> ]+' '{print substr($7
  8. ,7)}'`
  9. echo $a $sring
  10. done
  11. }
  12. case $1 in
  13. -f)
  14. shift
  15. ipp $1
  16. ;;
  17. -i)
  18. shift
  19. sring=`curl -s "http://ip138.com/ips138.asp?ip=${1}&action=2"| iconv -f gb2312 -t utf-8 |grep '<ul class="ul1"><li>' | awk -F '[<> ]+' '{print substr($7,7)}'`
  20. echo $1 $sring
  21. ;;
  22. *)
  23. echo "[Help]
  24. $0 need -f or -i
  25. -f ------- argument is a file
  26. -i ------- argument is a IP
  27. [For example]:
  28. $0 -f filename
  29. $0 -i ipadress
  30. "
  31. ;;
  32. esac


原文地址:https://www.cnblogs.com/feihongwuhen/p/7169826.html