脚本功能:查看登录过系统的IP地址的物理位置

执行方式:bash  login_city_info_use_taobaoAPI.sh  username

[liuxiao@hkecs Storage]$ bash login_city_info_use_taobaoAPI.sh root
"ip":"119.4.254.195" "country":"中国" "region":"四川" "city":"成都" "isp":"联通"
"ip":"182.149.160.103" "country":"中国" "region":"四川" "city":"成都" "isp":"电信"
"ip":"222.209.8.224" "country":"中国" "region":"四川" "city":"成都" "isp":"电信"
"ip":"222.209.8.70" "country":"中国" "region":"四川" "city":"成都" "isp":"电信"
"ip":"222.209.9.13" "country":"中国" "region":"四川" "city":"成都" "isp":"电信"
"ip":"223.85.209.56" "country":"中国" "region":"四川" "city":"成都" "isp":"移动"
[liuxiao@hkecs Storage]$

脚本内容如下:

[liuxiao@hkecs Storage]$ more login_city_info_use_taobaoAPI.sh 
#!/bin/bash

if [ -z "$1" ];then
	echo "Usage: bash login_city_info_use_taobaoAPI.sh root"
	exit 1
fi

ip_addr_list=`last | grep $1 | awk '{print $3}' | sort | uniq `

for ip_addr in ${ip_addr_list}
do
	result=`curl -s http://ip.taobao.com/service/getIpInfo.php?ip=${ip_addr}`
	result_code=`echo ${result} | awk -F',' '{print $1}' | awk -F':' '{print $2}'`
	result_info=`echo ${result} | awk -F':{' '{print $2}' | awk -F',' '{print $1,$2,$4,$5,$7}'`
	if [ "${result_code}" != "0" ];then
		echo -e "\e[1;32mIP:\e[0m${ip_addr} \e[1;32mERROR\e[0m please try again! "
	else
		echo ${result_info}
	fi
done