shell脚本配置DNS

#!/bin/bash
#设置DNS

[[ $# -ne 2 ]] && echo "ERROR:Invalid Param,Please Excute:bash $0 url dns_ip" && exit 1
URL=$1
DNS_IP=$2

http_code=$(curl -k -m 3 -s -o /dev/null -w %{http_code} ${URL})
if ! echo ${http_code} | egrep '^[234]' &> /dev/null;then
    [[ ! -f /etc/resolv.conf.bak ]] && cp /etc/resolv.conf{,.bak}
    sed -i "/${DNS_IP}$/d" /etc/resolv.conf
    echo "nameserver ${DNS_IP}" >> /etc/resolv.conf
else
    echo "INFO:Set Dns Success" && exit 0
fi

http_code=$(curl -k -m 3 -s -o /dev/null -w %{http_code} ${URL})
echo $http_code
if echo ${http_code} | egrep '^[234]' &> /dev/null;then
    echo "INFO:Set Dns Success"
else
    echo "ERROR:${URL} Unreachable,Please Check Network Or Dns" && exit 1
fi
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页