返回顶部

IP地址解析DNS

IP地址解析DNS

背景

指定IP解析域名,查看解析的域名,常用作CDN地址解析查询是否生效。本文章给出几个解决方案的shell脚本

#!/bin/bash
#
#****************************************************************************************
#Author:               wei
#***************************************************************************************

circulate_check () {
    RED=`echo -e '\e[1;31m'`
    GGREEN=`echo -e '\e[1;32m'`
    BLUE=`echo -e '\e[1;36m'`
    END=`echo -e '\e[0m'`
    while true ;do
        read -p "${BLUE}请输入ip地址:${END}"  IP_LIST
        if [[ "${IP_LIST}" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]];then
            echo "${GGREEN}输入的IP为IPV4${END}"
                RESOLVE_RESULT=$(curl --resolve www.baidu.com:443:${IP_LIST} 'https://www.baidu.com/' -o /dev/null -vk 2>&1| egrep 'common name' |awk '{print $NF}')
                if [[ "${RESOLVE_RESULT}" == *.weirui.cn ]];then
                    echo "${GGREEN}IP为杭${END}"
                elif [[ "${RESOLVE_RESULT}" == *.weiruivideo.com ]];then
                    echo "${GGREEN}IP为自${END}"
                fi
        elif [[ "${IP_LIST}"  =~ ([0-9a-Z]{0,4}\:){5}[0-9a-Z]{0,4}$ ]];then
            echo '输入的IP为IPV6'
            # RESOLVE_RESULT=$(curl  --resolve www.baidu.com:443 "https://[${IP_LIST}]" -vv -o /dev/null  -k -g 2>&1 | egrep 'common name' |awk '{print $NF}')
            RESOLVE_RESULT=$(curl  --resolve www.baidu.com:443:[${IP_LIST}] "https://www.baidu.com" -vv -o /dev/null  -k -g 2>&1 | egrep 'common name' | awk '{print $NF}')
            if [[ "${RESOLVE_RESULT}" == *.weirui.cn ]];then
                echo "${GGREEN}IP为杭${END}"
            elif [[ "${RESOLVE_RESULT}" == *.weiruivideo.com ]];then
                echo "${GGREEN}IP为自${END}"
            fi
        elif [[ "${IP_LIST}"  == exit ]];then
            echo "${GGREEN}exit...${END}" && exit
        else
            echo "${RED}输入的IP错误,请重新输入!${END}"
        fi
    done
}

ip_check () {
    #传入IP地址的变量和值
    read -p "请输入ip地址:"  IP_LIST
    #判断变量是否为IPV4
    if [[ "${IP_LIST}" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]];then
        echo '输入的IP为IPV4'
        #获取IPv4地址解析的结果,并自定义变量
        RESOLVE_RESULT=$(curl --resolve www.baidu.com:443:${IP_LIST} 'https://www.baidu.com/' -o /dev/null -vk 2>&1| egrep 'common name' |awk '{print $NF}')
        #判断变量是哪家的
        if [[ "${RESOLVE_RESULT}" == *.weirui.cn ]];then
            echo 'IPV4为杭'
        elif [[ "${RESOLVE_RESULT}" == *.weiruivideo.com ]];then
            echo 'IPV4为自'
        #都不满足条件则打印错误结果和IP,方便排查问题
        else
            echo "IPV4解析异常,解析结果为:${RESOLVE_RESULT},请检查解析IP为:${IP_LIST}"
        fi
    #如果上面匹配失败,接下来判断变量是否为IPV6
    elif [[ "${IP_LIST}"  =~ ([0-9a-Z]{0,4}\:){5}[0-9a-Z]{0,4}$ ]];then
        echo '输入的IP为IPV6'
        RESOLVE_RESULT=$(curl  --resolve www.baidu.com:443:[${IP_LIST}] "https://www.baidu.com" -vv -o /dev/null  -k -g 2>&1 | egrep 'commonname' | awk '{print $NF}')
        # RESOLVE_RESULT=$(curl  --resolve www.baidu.com:443 "https://[${IP_LIST}]" -vv -o /dev/null  -k -g 2>&1 | egrep 'common name' | awk'{print $NF}')
        if [[ "$RESOLVE_RESULT}" == *.weirui.cn ]];then
            echo 'IPV6为杭'
        elif [[ "${RESOLVE_RESULT}" == *.weiruivideo.com ]];then
            echo 'IPV6为自'
        else
            echo "IPV6解析异常,解析结果为:${RESOLVE_RESULT},请检查解析IP为:${IP_LIST}"
        fi
    #如果上面条件匹配都不满足,则打印信息退出
    else
        echo '输入的IP错误,请重新输入!'
    fi
}

ip_auto_check () {
    if [[ $1 =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]];then
        echo '输入的IP为IPV4'
        RESOLVE_RESULT=$(curl --resolve www.baidu.com:443:$1 'https://www.baidu.com/' -o /dev/null -vk 2>&1| egrep 'common name' |awk '{print $NF}')
        if [[ "${RESOLVE_RESULT}" == *.weirui.cn ]];then
            echo 'IPV4为杭'
        elif [[ "${RESOLVE_RESULT}" == *.weiruivideo.com ]];then
            echo 'IPV4为自'
        else
            echo "IPV4解析异常,解析结果为:${RESOLVE_RESULT},请检查解析IP为:${IP_LIST}"
        fi
    elif [[ $1  =~ ([0-9a-Z]{0,4}\:){5}[0-9a-Z]{0,4}$ ]];then
        echo '输入的IP为IPV6'
        RESOLVE_RESULT=$(curl  --resolve www.baidu.com:443:[$1] "https://www.baidu.com" -vv -o /dev/null  -k -g 2>&1 | egrep 'common name' |awk '{print $NF}')
        # RESOLVE_RESULT=$(curl  --resolve www.baidu.com:443 "https://[$1]" -vv -o /dev/null  -k -g 2>&1 | egrep 'common name' | awk '{print$NF}')
        if [[ "$RESOLVE_RESULT}" == *.weirui.cn ]];then
            echo 'IPV6为杭'
        elif [[ "${RESOLVE_RESULT}" == *.weiruivideo.com ]];then
            echo 'IPV6为自'
        else
            echo "IPV6解析异常,解析结果为:${RESOLVE_RESULT},请检查解析IP为:$1"
        fi
    else
        echo '输入的IP错误,请重新输入!'
    fi
}

ip_more_check () {
    echo '请将IP写入ip-list.txt文件'
    NUM=$(wc -l ip-list.txt |awk '{print $1}')
    [[ "${NUM}" == 0 ]] && echo 'ip-list.txt内容为空' || echo 'ip-list.txt内容不空'
    IP_LIST=$(cat ip-list.txt)
    for i in ${IP_LIST};do
        if [[ "${i}" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]];then
            echo '输入的IP为IPV4'
            RESOLVE_RESULT=$(curl --resolve www.baidu.com:443:${i} 'https://www.baidu.com/' -o /dev/null -vk 2>&1| egrep 'common name' |awk '{print $NF}')
            if [[ "${RESOLVE_RESULT}" == *.weirui.cn ]];then
                echo 'IPV4为杭'
            elif [[ "${RESOLVE_RESULT}" == *.weiruivideo.com ]];then
                echo 'IPV4为自'
            else
                echo "IPV4解析异常,解析结果为:${RESOLVE_RESULT},请检查解析IP为:${i}"
            fi
        elif [[ "${i}"  =~ ([0-9a-Z]{0,4}\:){5}[0-9a-Z]{0,4}$ ]];then
            echo '输入的IP为IPV6'
            RESOLVE_RESULT=$(curl  --resolve www.baidu.com:443:[${i}] "https://www.baidu.com" -vv -o /dev/null  -k -g 2>&1 | egrep 'common name' | awk '{print $NF}')
            # RESOLVE_RESULT=$(curl  --resolve www.baidu.com:443 "https://[$i]" -vv -o /dev/null  -k -g 2>&1 | egrep 'common name' | awk '{print $NF}')
            if [[ "${RESOLVE_RESULT}" == *.weirui.cn ]];then
                echo 'IPV6为杭'
            elif [[ "${RESOLVE_RESULT}" == *.weiruivideo.com ]];then
                echo 'IPV6为自'
            else
                echo "IPV6解析异常,解析结果为:${RESOLVE_RESULT},请检查解析IP为:${i}"
            fi
        else
            echo '输入的IP错误,请重新输入!'
        fi
    done
}


#方法一: 交互式循环查询操作,exit退出操作,如:./check-ip.sh
#circulate_check

#方法二: 单次查询IP操作,交互式,如:./check-ip.sh
#ip_check

# 方法三: 单次指定IP查询,非交互式,如:./check-ip.sh  120.24.61.213
#ip_auto_check $1

#方法四:  批量处理IP的解析
ip_more_check
#方法五: 菜单式查询,交互式,case语句
#方法六:循环菜单式查询,交互式,while + case语句

 

操作演示

方法一

 方法二

 方法三

 方法四

 剩下两种场景可自行研究编写,有需要提供思路可联系指导

 

posted @ 2023-06-11 22:11  九尾cat  阅读(84)  评论(0编辑  收藏  举报