shell 写了一个查询 ip 地址和手机号归属的小脚本, 算是造个轮子了 233

2016-09-08 14:54:46 +08:00
 douglas1997

默认大家网速够快啊 233

#!/bin/bash

while getopts "i:p:h" arg
do
    case $arg in
        i)
            IPADDR=$OPTARG
            [ "$IPADDR" != "" ] && wget -q -O /tmp/$IPADDR.$$ "http://www.ip.cn/index.php?ip=$IPADDR"
            [ -f /tmp/$IPADDR.$$ ] && echo -n "$IPADDR: "; cat /tmp/$IPADDR.$$ | grep '<code>' | awk -F'[<>]' '{print $17,$23}' && rm -f /tmp/$IPADDR.$$
            ;;
        p)
            PHONE_NO=$OPTARG
            [ "PHONE_NO" != "" ] && wget -q -O /tmp/$PHONE_NO.$$ "http://ip.cn/db.php?num=$PHONE_NO"
            [ -f /tmp/$PHONE_NO.$$ ] && echo -n "$PHONE_NO: "; cat /tmp/$PHONE_NO.$$ | grep 'id="result"' |awk -F';|<' '{print $7}' && rm -f /tmp/$PHONE_NO.$$
            ;;
        h)
            echo "Usage:"
            echo "          -i [IP address|Domain name] -p [Phone number]"
            echo "          -h show this help message"
            echo "..."
            ;;
        ?)
            echo "Unknown argument..."
            exit 1
            ;;
    esac
done
1973 次点击
所在节点    分享发现
7 条回复
URgoy
2016-09-08 15:28:16 +08:00
有啥用啊,显示归属不是自带的吗? vpn 不是自带改了 ip 吗?勿喷,我啥都不会,只是提个问题
douglas1997
2016-09-08 15:30:56 +08:00
@URgoy 额。在 CLI 上面看到的只有 IP ,结合 access.log 可以统计访问服务器的来源和次数。
URgoy
2016-09-08 15:37:37 +08:00
好的,我去谷歌一下
jyf007
2016-09-09 08:25:43 +08:00
已查收,准备改写。
inmyfree
2016-09-09 09:16:22 +08:00
查询 ip
` curl ip.cn `
jyf007
2016-09-09 09:23:23 +08:00
#!/bin/bash
readonly ARGS=$@
usage(){
cat <<- EOF
usage $PROGNAME options

--help -h show this help
--ipaddr -i IP address
--phonenum -p Phone number
}
cmdline(){
local arg=
for arg
do
local delim=""
case "$arg" in
#translate --gnu-long-options to -g (short options)
--phonenum) arg_short="${arg_short}-p ";;
--ipaddr) arg_short="${arg_short}-i ";;
--help) arg_short="${arg_short}-h ";;
*) [[ "${arg:0:1}" == "-" ]] || delim="\" "
arg_short="${arg_short}${delim}${arg}${delim}";;
esac
eval set -- $arg_short
while getopts "i:p:h" OPTIONS
do
case $OPTIONS in
h)
usage
exit 0
;;
i)
readonly IPADDR=$OPTARG
readonly REQURIE_NAME=ip
;;
p)
readonly PHONENUM=$OPTARG
readonly REQURIE_NAME=phone
;;
esac
done
}
is_file_exist(){
[[ -f $1 ]]
}
is_empty(){
[[ -z $1 ]]
}
ip_requrie(){
load_requrie $1 "http://www.ip.cn/index.php?ip=$IPADDR"
is_file_exist $1
awk -F '[<>]' '/\<code\>/ {print $9 " " $17 $23}' $1
}
phone_requrie(){
load_requrie $1 "http://ip.cn/db.php?num=$PHONENUM"
is_file_exist $1
awk -F '[<>]' '/\<code\>/ {print $15 " " $9 } ' $1
}
load_requrie(){
wget -q -O $1 $2
}
main(){
readonly PROGNAME=${BASH_SOURCE}
readonly SEARCH_REQURIE=$(mktemp)
if [ $ARGS == 0 ];then
usage
return 0
is_empty $SEARCH_REQURIE &&
case $REQURIE_NAME in
ip)
ip_requrie $SEARCH_REQURIE
phone)
phone_requrie $SEARCH_REQURIE
esac
rm -f $SEARCH_REQURIE
exit 0
}
douglas1997
2016-09-09 09:54:28 +08:00
@jyf007 学习了学习了。谢谢分享

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/304848

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX