当前位置:首页 > NAS

群晖自动更新hosts

Yuki2024-01-13NAS742

关于群晖NAS video station TMDB连接测试失败及搜刮结果下载失败的解决方案

QQ截图20240113100231.png

如果使用过别的脚本或者hosts手动添加过相关的地址可以先在任务计划中执行以下脚本来清理hosts文件


#!/bin/sh

LOCAL_HOST_LIST=`cat /etc/hosts | grep -v "127.0.0.1\|::1\|^$"`

for HOST in $LOCAL_HOST_LIST; do

      sed -i "/$HOST/d" /etc/hosts

done


如果hosts文件没有修改过不需要这一步


控制面板——任务计划——新增(用户自定义脚本)

填写任务名称,账户选择root

运行命令栏填写以下代码

手动执行——运行

#!/bin/sh

# DOMAIN_HOST=1为启用0为关闭自定义域名hosts文件写入,GITHUB_HOST=1为启用0为关闭github域名hosts文件写入。

DOMAIN_HOST=1

GITHUB_HOST=0

# DOMAIN_LIST自定义域名列表,多域名以空格隔开,这里的域名会通过国外的DNS API返回正确的IP写入hosts文件以解决DNS污染问题。

DOMAIN_LIST=(api.themoviedb.org image.tmdb.org)

GITHUB_HOST_URL="https://hosts.gitcdn.top/hosts.txt"

DNS_API="https://networkcalc.com/api/dns/lookup"

REMOTE_GITHUB_HOST_AT=`curl -s $GITHUB_HOST_URL | grep -w "# last fetch time:"`

LOCAL_GITHUB_HOST_AT=`cat /etc/hosts | grep -w "# last fetch time:"`


check_domain_host(){

    for DOMAIN in ${DOMAIN_LIST[@]}; do

        DOMAIN_IP_LIST=`curl -k -X GET $DNS_API/$DOMAIN | jq -r '.records.A[].address'`

        if [ "$DOMAIN_IP_LIST" != "" ]; then

           for DOMAIN_IP in "$DOMAIN_IP_LIST"; do

               if [[ `cat /etc/hosts | grep -wc $DOMAIN_IP` = 0 ]]; then

                  [[ `cat /etc/hosts | grep -wc "fetch-domain-hosts"` != 0 ]] && sed -i "/# fetch-domain-hosts begin/Q" /etc/hosts

               fi

           done

        fi

    done

}


write_domain_host(){

    if [[ `cat /etc/hosts | grep -wc "fetch-domain-hosts"` = 0 ]]; then

        echo "# fetch-domain-hosts begin" >> /etc/hosts

        for DOMAIN in ${DOMAIN_LIST[@]}; do

            DOMAIN_IP_LIST=`curl -k -X GET $DNS_API/$DOMAIN | jq -r '.records.A[].address'`

            for DOMAIN_IP in $DOMAIN_IP_LIST; do

                echo -e "$DOMAIN_IP\t\t$DOMAIN" >> /etc/hosts

            done

        done

        echo "# fetch-domain-hosts end" >> /etc/hosts

    fi

}


if [ $DOMAIN_HOST = 1 ]; then

    check_domain_host

    write_domain_host

else

    [[ `cat /etc/hosts | grep -wc "fetch-domain-hosts"` != 0 ]] && sed -i "/# fetch-domain-hosts begin/Q" /etc/hosts

fi

if [ $GITHUB_HOST = 1 ]; then

    if [ "$REMOTE_GITHUB_HOST_AT" != "" ]; then

        [[ `cat /etc/hosts | grep -wc "fetch-github-hosts"` = 0 ]] && curl $GITHUB_HOST_URL >> /etc/hosts

        if [[ "$REMOTE_GITHUB_HOST_AT" != "$LOCAL_GITHUB_HOST_AT" ]];then

            sed -i "/# fetch-github-hosts begin/Q" /etc/hosts && curl $GITHUB_HOST_URL >> /etc/hosts

        else

            echo "HOST无需更新"

        fi

    fi

else

    [[ `cat /etc/hosts | grep -wc "fetch-github-hosts"` != 0 ]] && sed -i "/# fetch-github-hosts begin/Q" /etc/hosts

fi




扫描二维码推送至手机访问。

版权声明:本文由发布,如需转载请注明出处。

本文链接:http://tamako.cn:8003/?id=27

分享给朋友:

“群晖自动更新hosts” 的相关文章

群晖文件夹清理缓存删除方法

群晖文件夹清理缓存删除方法

以download station为例…

群晖激活Advanced Media Extensions 3.0

群晖激活Advanced Media Extensions 3.0

能够成功激活的前提是你的群晖为半白(有正确的SN号)…

群晖安全 | 禁止所有外国IP访问 添加网络威胁黑名单

群晖安全 | 禁止所有外国IP访问 添加网络威胁黑名单

最近群晖安安静静本本分分的运行着,直到昨晚临睡觉时一连串提醒使我失去了困意。一连串的国外IP一直在试图登录我的nas群晖的基本安全设置修改…