最新消息:阿啰哈,本人90后,目前单身,欢迎妹子们来撩!.(。→‿←。)

跳墙:自动更新hosts文件的shell脚本

Shell 林志斌 41浏览

用hosts文件来跳墙确实好用,但可恶的 G F W 导致hosts文件经常得更新,不过,这里有经常更新的hosts文件可供下载,感谢!
可是每次都手工下载,再覆盖旧的hosts文件,操作起来麻烦,不如一键执行脚本自动完成

使用说明:
1. 不用担心!系统中现有的hosts文件会原封不动备份在原路径中
2. /etc/hosts.base是你希望hosts文件中每次都要有的一些固定条目,可统一写入到此文件中,脚本会自动把这些固定的条目一并追加到新的hosts文件中

我的脚本如下:

#!/bin/bash
#Author: kashu
#My Website: https://kashu.org
#Date: 2015-08-03
#Filename: hosts.sh
#Description: Download and update the latest hosts file.

# Check user privilege
if [ `id -u` -ne "0" ]; then echo "User has insufficient privileage!" && exit 1; fi
# Check if wget is executable
test -x `which wget` || { echo "wget is not executable"; exit 2; }

DATE=`date +%Y%m%d`

# Download the hosts file from github.com
wget -r --retry-connrefused --no-check-certificate https://raw.githubusercontent.c\
om/racaljk/hosts/master/hosts -O /tmp/hosts."$DATE" || { echo "The hosts \
file download failed!"; exit 3; }

# Backup the existing hosts file before overwrite it.
mv -f /etc/hosts /etc/hosts."$DATE"

if [ -f /etc/hosts.base ]; then
	cat /etc/hosts.base >| /etc/hosts
else
	:>| /etc/hosts
fi

cat /tmp/hosts."$DATE" >> /etc/hosts
chmod a+r /etc/hosts && clear

# Have fun!
cat <<- END
Cool! The hosts file is up-to-date. 
Enjoy the Internet!
END

转载请注明:林志斌 » 跳墙:自动更新hosts文件的shell脚本

发表评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址