V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
douglas1997
V2EX  ›  分享发现

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

  •  
  •   douglas1997 · 2016-09-08 14:54:46 +08:00 · 1968 次点击
    这是一个创建于 2802 天前的主题,其中的信息可能已经有所发展或是发生改变。

    默认大家网速够快啊 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
    
    7 条回复    2016-09-09 09:54:28 +08:00
    URgoy
        1
    URgoy  
       2016-09-08 15:28:16 +08:00 via Android
    有啥用啊,显示归属不是自带的吗? vpn 不是自带改了 ip 吗?勿喷,我啥都不会,只是提个问题
    douglas1997
        2
    douglas1997  
    OP
       2016-09-08 15:30:56 +08:00 via iPhone
    @URgoy 额。在 CLI 上面看到的只有 IP ,结合 access.log 可以统计访问服务器的来源和次数。
    URgoy
        3
    URgoy  
       2016-09-08 15:37:37 +08:00 via Android
    好的,我去谷歌一下
    jyf007
        4
    jyf007  
       2016-09-09 08:25:43 +08:00 via Android
    已查收,准备改写。
    inmyfree
        5
    inmyfree  
       2016-09-09 09:16:22 +08:00
    查询 ip
    ` curl ip.cn `
    jyf007
        6
    jyf007  
       2016-09-09 09:23:23 +08:00 via Android
    #!/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
        7
    douglas1997  
    OP
       2016-09-09 09:54:28 +08:00 via iPhone
    @jyf007 学习了学习了。谢谢分享
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2781 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 05:37 · PVG 13:37 · LAX 22:37 · JFK 01:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.