Chinaunix首页 | 论坛 | 博客
  • 博客访问: 93493
  • 博文数量: 9
  • 博客积分: 330
  • 博客等级: 一等列兵
  • 技术积分: 117
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-05 18:36
文章分类

全部博文(9)

文章存档

2012年(4)

2011年(1)

2010年(4)

分类: 系统运维

2012-03-05 14:27:04


shell 获取域名或IP地址的地理位置


  1. #!/bin/bash
  2. if [ -z "$1" ];then
  3. echo "usage: $0 xxx.xxx.xxx.xxx";
  4. exit 1;
  5. fi
  6. function queryIpRegion()
  7. {
  8. local ip="$1"
  9. local tfile="/tmp/.ip138.${ip}"
  10. wget "{ip}&action=2" &> /dev/null -O "${tfile}";
  11. if [ $? -ne 0 ];
  12. then
  13. exit 1;
  14. fi
  15. iconv --from-code=cp936 --to-code=utf-8 "${tfile}"\
  16. | sed -e 's/
  17. /\n
  18. /g' -e 's/

    /\n

    /g'\

  19. | grep -E "
  20. |

    "\

  21. | sed -e 's/^.*\(.*\)<\/h[0-9]>.*$/\1/g' -e 's/^.*
  22. \(.*\)<\/li>.*$/\1/g'
  23. rm -f "${tfile}"
  24. }
  25. function digHostAndQueryIpRegion()
  26. {
  27. local host="$1";
  28. dig ${host} A +noall +answer|grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'|awk '{print $5}'| while read ip
  29. do
  30. echo -e "\n${host} >> ${ip}";
  31. queryIpRegion "${ip}";
  32. done
  33. }
  34. function main()
  35. {
  36. local host=$1
  37. echo "${host}"|grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;
  38. if [ $? -ne 0 ];then
  39. digHostAndQueryIpRegion "${host}";
  40. else
  41. queryIpRegion "${host}";
  42. fi
  43. }
  44. main "$1"

--------------end--------------
From: GS
-------------------------------


阅读(3026) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~