shell 获取域名或IP地址的地理位置
- #!/bin/bash
-
-
if [ -z "$1" ];then
-
echo "usage: $0 xxx.xxx.xxx.xxx";
-
exit 1;
-
fi
-
-
function queryIpRegion()
-
{
-
local ip="$1"
-
local tfile="/tmp/.ip138.${ip}"
-
-
wget "{ip}&action=2" &> /dev/null -O "${tfile}";
-
if [ $? -ne 0 ];
-
then
-
exit 1;
-
fi
-
-
iconv --from-code=cp936 --to-code=utf-8 "${tfile}"\
-
| sed -e 's/
- /\n
- /g' -e 's/
/\n/g'\
-
| grep -E "
- |
"\
-
| sed -e 's/^.*\(.*\)<\/h[0-9]>.*$/\1/g' -e 's/^.*
- \(.*\)<\/li>.*$/\1/g'
-
-
rm -f "${tfile}"
-
}
-
-
function digHostAndQueryIpRegion()
-
{
-
local host="$1";
-
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
-
do
-
echo -e "\n${host} >> ${ip}";
-
queryIpRegion "${ip}";
-
done
-
}
-
-
function main()
-
{
-
local host=$1
-
echo "${host}"|grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' &> /dev/null;
-
if [ $? -ne 0 ];then
-
digHostAndQueryIpRegion "${host}";
-
else
-
queryIpRegion "${host}";
-
fi
-
}
-
-
main "$1"
--------------end--------------
From: GS
-------------------------------
阅读(3097) | 评论(0) | 转发(1) |