openwrt下自动更新github hosts

openwrt下自动更新github hosts

GitHub hosts

本文在原作者的基础上进行修改https://blog.csdn.net/lovelyed/article/details/127882930
使用到gitee-hosts的仓库https://gitee.com/fliu2476/github-hosts/blob/main/hosts
该仓库每天更新hosts for GitHub

1.shh连接openwrt

cd /root
touch hostsUpdate.sh
vim hostsUpdate.sh

将以下内容粘贴到 hostsUpdate.sh中
命令分别对应获取、整合编辑、复制到hosts文件、修改权限、重启dns服务器

wget -q https://gitee.com/fliu2476/github-hosts/raw/main/hosts -O /tmp/hostsNew.txt --no-check-certificate
cat /tmp/hostsNew.txt /etc/hosts_origin > /tmp/hostsNewAll.txt
cp /tmp/hostsNewAll.txt /etc/hosts
chmod 664 /etc/hosts
/etc/init.d/dnsmasq restart

以下命令分别对应1.修改脚本运行权限;2.复制原hosts,使其更新时不破坏原有hosts文件信息;3.修改复制的hosts文件的权限;4.运行脚本

chmod 777 hostsUpdate.sh
cp /etc/hosts /etc/hosts_origin
chmod 664 /etc/hosts_origin
/root/hostsUpdate.sh

待运行完成后,查看hosts

cat /etc/hosts

若有更新,表示脚本顺利执行,则可以在OpenWRT路由器管理页面的系统-计划任务中添加下面的代码,添加后点提交,最后在系统-启动项中将cron重启即可。下面代码表示每小时运行该脚本更新hosts文件。
跳转过去学习->openwrt计划任务语法
每天6点执行脚本

0 6 * * * /root/hostsUpdate.sh
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值