Linux Shell脚本之更新hosts文件以便于访问谷歌服务推荐

Linux服务器一般不带图形界面,管理员通常都是通过命令行界面直接操作服务器。在日常的管理运维中,经常需要用到github、gist、githubusercontent等网站上的一些内容,也有时需要用到Google上的一些服务,如Google API之类的。然而这些网站大部分都被GFW防火长城屏蔽掉了,GFW用到的屏蔽手段很多,其中DNS污染就是一种。

PS: GFW防火长城并不是真正要拦截所有人,而是大部分人,要想访问墙外,办法总是有的。

在Linux环境下通过各种形式翻墙可能并不是最佳实践,最简单的方法是通过修改/etc/hosts文件避免国内DNS污染。

可用的hosts文件可以通过github/hosts项目(https://github.com/racaljk/hosts)获取,也可以从备份地址(https://coding.net/u/scaffrey/p/hosts/git)获取。

本文只是为Linux提供了一套简单的工具,通过这个Shell脚本结合crontab,可以实现自动更新hosts文件。虽然github/hosts项目中也提供了修改的工具,但自己写一个也是不错的,既可以锻炼自己,又可以学习和备忘。

此Shell脚本功能:

解析链接文件,用于增删改工作目录下的其他文件,这个是冗余的可选功能,当前Shell脚本不需要这个功能,但因为这个功能可能在其他脚本中比较重要,暂时罗列在此

echo带色显示,用于为用户提供友好的提示,如一般信息用白色,提示信息用蓝色,错误信息用红色,成功信息用绿色,警告信息用黄色等

检查是否是root或者sudo,修改hosts文件需要root权限

检查网络连通性和DNS解析是否正常,如果不正常,做出简单诊断和修改DNS为8.8.4.4和114.114.114.114

根据Linux发行版本的不同执行不同的命令(安装必要的软件包,如git),目前支持RHEL、CentOS、Ubuntu、Debian等版本

从github和备份地址两种途径获取并更改hosts文件

检测hosts文件是否添加成功,用curl google判断

脚本运行效果如下:

脚本最新内容也可以通过github获取:https://github.com/DingGuodong/GoogleHostsFileForLinux

脚本如下:

#!/bin/bash#Publicheader#=============================================================================================================================#resolvelinks-$0maybeasymboliclinkPRG="$0"while[-h"$PRG"];dols=`ls-ld"$PRG"`link=`expr"$ls":'.*- \(.*\)$'`ifexpr"$link":'/.*' /dev/null;thenPRG="$link"elsePRG=`dirname"$PRG"`/"$link"fi#GetstandardenvironmentvariablesPRGDIR=`dirname"$PRG"`#cecho-redsometext#Error,Failed#cecho-greensometext#Success#cecho-yellowsometext#Warning#cecho-bluesometext#Debug#cecho-whitesometext#info#cecho-n#newline#endwhile["$1"];docase"$1"in-normal)color="\033[00m";;#-black)color="\033[30;01m";;-red)color="\033[31;01m";;-green)color="\033[32;01m";;-yellow)color="\033[33;01m";;-blue)color="\033[34;01m";;#-magenta)color="\033[35;01m";;#-cyan)color="\033[36;01m";;-white)color="\033[37;01m";;-n)one_line=1;shift;continue;;*)echo-n"$1";shift;continue;;shiftecho-en"$color"echo-en"$1"echo-en"\033[00m"shiftif[!$one_line];thenecho#endechocolorfunction#echocolorfunction,smarterfunctionecho_r(){#Error,Failed[$#-ne1] return0echo-e"\033[31m$1\033[0m"functionecho_g(){#Success[$#-ne1] return0echo-e"\033[32m$1\033[0m"functionecho_y(){#Warning[$#-ne1] return0echo-e"\033[33m$1\033[0m"functionecho_b(){\#Debug[$#-ne1] return0echo-e"\033[34m$1\033[0m"#endechocolorfunction,smarterWORKDIR=$PRGDIR#endpublicheader#=============================================================================================================================USER="`id-un`"LOGNAME=$USERif[$UID-ne0];thenecho"WARNING:Runningasanon-rootuser,\"$LOGNAME\".Functionalitymaybeunavailable.Onlyrootcanusesomecommandsoroptions"#Name:replaceLocalHostsFileAgainstGfw.sh#Referto:https://github.com/racaljk/hosts#Backups:https://coding.net/u/scaffrey/p/hosts/git#defineuserfriendlymessagesheader="Function:ExecutethisshellscripttoaccessGoogle,etceasily.OpensourcesoftwareWrittenbyGuodongDing dgdenterprise@gmail.com .Blog:http://dgd2010.blog.51cto.com/Github:https://github.com/DingGuodongLastupdated:2016-4-19check_network_connectivity(){echo_b"checkingnetworkconnectivity..."network_address_to_check=8.8.4.4stable_network_address_to_check=114.114.114.114ping_count=2ping-c$ping_count$network_address_to_check /dev/nullretval=$?if[$retval-ne0];thenifping-c$ping_count$stable_network_address_to_check /dev/null;thenecho_g"networkto$stable_network_address_to_checksucceed!"echo_y"note:networkto$network_address_to_checkfailed!"elif!iproute|grepdefault /dev/null;thenecho_r"networkisunreachable,gatewayisnotset."exit1elif!ping-c2$(iproute|awk'/default/{print$3}') /dev/null;thenecho_r"networkisunreachable,gatewayisunreachable."exit1elseecho_r"networkisblocked!"exit1fielif[$retval-eq0];thenecho_g"Checknetworkconnectivitypassed!"ficheck_name_resolve(){echo_b"checkingnameresolve..."target_name_to_resolve="github.com"stable_target_name_to_resolve="www.aliyun.com"ping_count=1if!ping-c$ping_count$target_name_to_resolve /dev/null;thenecho_y"namelookupfailedfor$target_name_to_resolvewith$ping_counttimes"ifping-c$ping_count$stable_target_name_to_resolve /dev/null;thenecho_g"namelookupsuccessfor$stable_target_name_to_resolvewith$ping_counttimes"fi[-f/etc/resolv.conf] cp/etc/resolv.conf/etc/resolv.conf$(date+%Y%m%d%H%M%S)~cat /etc/resolv.conf eofnameserver8.8.4.4nameserver114.114.114.114check_name_resolveelseecho_g"Checknameresolvepassed!"returnficommand_exists(){#which"$@" /dev/null2 1command-v"$@" /dev/null2 1check_command_can_be_execute(){command_existsyum_install_packages(){echo_b"yuminstall$@..."yum-q-yyinstall$@retval=$?if[$retval-ne0];thenecho_r"yuminstall$@failed!"exit1elseecho_g"yuminstall$@successfully!"fiapt_get_install_packages(){echo_b"apt-getinstall$@..."apt-get-qq-yinstall$@retval=$?if[$retval-ne0];thenecho_r"apt-getinstall$@failed!"exit1elseecho_g"apt-getinstall$@successfully!"fi#Refer:https://get.docker.com/#'curl-sSLhttps://get.docker.com/|sh'#or:#'wget-qO-https://get.docker.com/|sh'#CheckifthisisaforkedLinuxdistrocheck_linux_distribution_forked(){#Checkforlsb_releasecommandexistence,itusuallyexistsinforkeddistrosifcommand_existslsb_release;then#Checkifthe`-u`optionissupportedset+elsb_release-a-u /dev/null2 1lsb_release_exit_code=$?set-e#Checkifthecommandhasexitedsuccessfully,itmeanswe'reinaforkeddistroif["$lsb_release_exit_code"="0"];then#Printinfoaboutcurrentdistrocat -EOFYou'reusing'$lsb_dist'version'$dist_version'.#Gettheupstreamreleaseinfolsb_dist=$(lsb_release-a-u2 1|tr'[:upper:]''[:lower:]'|grep-E'id'|cut-d':'-f2|tr-d'[[:space:]]')dist_version=$(lsb_release-a-u2 1|tr'[:upper:]''[:lower:]'|grep-E'codename'|cut-d':'-f2|tr-d'[[:space:]]')#Printinfoaboutupstreamdistrocat -EOFUpstreamreleaseis'$lsb_dist'version'$dist_version'.elseif[-r/etc/debian_version] ["$lsb_dist"!="ubuntu"];then#We'reDebiananddon'tevenknowit!lsb_dist=debiandist_version="$(cat/etc/debian_version|sed's/\/.*//'|sed's/\..*//')"case"$dist_version"in8|'KaliLinux2')dist_version="jessie";;7)dist_version="wheezy";;esacfifificheck_linux_distribution(){#referto/etc/issueand/etc/*-releasemaybemorebetterchoice#performsomeveryrudimentaryplatformdetectionlsb_dist=''dist_version=''ifcommand_existslsb_release;thenlsb_dist="$(lsb_release-si)"fiif[-z"$lsb_dist"] [-r/etc/lsb-release];thenlsb_dist="$(./etc/lsb-release echo"$DISTRIB_ID")"fiif[-z"$lsb_dist"] [-r/etc/debian_version];thenlsb_dist='debian'fiif[-z"$lsb_dist"] [-r/etc/fedora-release];thenlsb_dist='fedora'fiif[-z"$lsb_dist"] [-r/etc/oracle-release];thenlsb_dist='oracleserver'fiif[-z"$lsb_dist"];thenif[-r/etc/centos-release]||[-r/etc/redhat-release];thenlsb_dist='centos'fifiif[-z"$lsb_dist"] [-r/etc/os-release];thenlsb_dist="$(./etc/os-release echo"$ID")"filsb_dist="$(echo"$lsb_dist"|tr'[:upper:]''[:lower:]')"case"$lsb_dist"inubuntu)ifcommand_existslsb_release;thendist_version="$(lsb_release--codename|cut-f2)"fiif[-z"$dist_version"] [-r/etc/lsb-release];thendist_version="$(./etc/lsb-release echo"$DISTRIB_CODENAME")"fi;;debian)dist_version="$(cat/etc/debian_version|sed's/\/.*//'|sed's/\..*//')"case"$dist_version"in8)dist_version="jessie";;7)dist_version="wheezy";;esac;;oracleserver)#needtoswitchlsb_disttomatchyumrepoURLlsb_dist="oraclelinux"dist_version="$(rpm-q--whatprovidesredhat-release--queryformat"%{VERSION}\n"|sed's/\/.*//'|sed's/\..*//'|sed's/Server*//')";;fedora|centos)dist_version="$(rpm-q--whatprovidesredhat-release--queryformat"%{VERSION}\n"|sed's/\/.*//'|sed's/\..*//'|sed's/Server*//')";;*)ifcommand_existslsb_release;thendist_version="$(lsb_release--codename|cut-f2)"fiif[-z"$dist_version"] [-r/etc/os-release];thendist_version="$(./etc/os-release echo"$VERSION_ID")"fi;;#CheckifthisisaforkedLinuxdistrocheck_linux_distribution_forked#endReferabove#refertoLNMP,http://lnmp.org/download.htmlfunctionGet_OS_Bit(){if[[`getconfWORD_BIT`='32' `getconfLONG_BIT`='64']];thenIs_64bit='y'elseIs_64bit='n'fifunctionGet_Dist_Name(){ifgrep-Eqi"CentOS"/etc/issue||grep-Eq"CentOS"/etc/*-release;thenDISTRO='CentOS'PM='yum'elifgrep-Eqi"RedHatEnterpriseLinuxServer"/etc/issue||grep-Eq"RedHatEnterpriseLinuxServer"/etc/*-release;thenDISTRO='RHEL'PM='yum'elifgrep-Eqi"Aliyun"/etc/issue||grep-Eq"Aliyun"/etc/*-release;thenDISTRO='Aliyun'PM='yum'elifgrep-Eqi"Fedora"/etc/issue||grep-Eq"Fedora"/etc/*-release;thenDISTRO='Fedora'PM='yum'elifgrep-Eqi"Debian"/etc/issue||grep-Eq"Debian"/etc/*-release;thenDISTRO='Debian'PM='apt'elifgrep-Eqi"Ubuntu"/etc/issue||grep-Eq"Ubuntu"/etc/*-release;thenDISTRO='Ubuntu'PM='apt'elifgrep-Eqi"Raspbian"/etc/issue||grep-Eq"Raspbian"/etc/*-release;thenDISTRO='Raspbian'PM='apt'elseDISTRO='unknow'fiGet_OS_BitfunctionGet_RHEL_Version(){Get_Dist_Nameif["${DISTRO}"="RHEL"];thenifgrep-Eqi"release5."/etc/redhat-release;thenecho"CurrentVersion:RHELVer5"RHEL_Ver='5'elifgrep-Eqi"release6."/etc/redhat-release;thenecho"CurrentVersion:RHELVer6"RHEL_Ver='6'elifgrep-Eqi"release7."/etc/redhat-release;thenecho"CurrentVersion:RHELVer7"RHEL_Ver='7'fififunctionGet_ARM(){ifuname-m|grep-Eqi"arm";thenIs_ARM='y'fiInstall_LSB()if["$PM"="yum"];thenyum-yinstallredhat-lsbelif["$PM"="apt"];thenapt-getupdateapt-getinstall-ylsb-releasefiGet_Dist_Version()Install_LSBeval${DISTRO}_Version=`lsb_release-rs`evalecho"${DISTRO}\${${DISTRO}_Version}"#endrefertohttp://lnmp.org/download.htmlfunctionget_hosts_file_from_backup_site(){if!grepgithub/etc/hosts /dev/null;thencp/etc/hosts/etc/hosts$(date+%Y%m%d%H%M%S)~elserm-f/etc/hosts\cp-fhosts/hosts/etc/hostsfiwget-qhttps://coding.net/u/scaffrey/p/hosts/git/raw/master/hosts-O/etc/hostsiftest$?-eq0-a-f/etc/hosts;thenecho_g"sethostsfilefrombackupsitesuccessfully!"elseecho_r"sethostsfilefrombackupsitefailed!"fifunctionget_hosts_file_from_github(){if[!-dhosts];thencommand_existsgit gitclonehttps://github.com/racaljk/hosts.git /dev/null2 1retval=$?if[$retval-ne0];thenecho_r"gitclonefailed!"get_hosts_file_from_backup_sitereturnelse[-shosts/hosts] echo"gitclonesuccessfully!"||exit1fielif[-dhosts/.git];thencdhostscommand_existsgit gitpull /dev/null2 1cd..elseecho_r"therewasadirectorynamed\"hosts\",failed!"exit1fiif!grepgithub/etc/hosts /dev/null testhosts/hosts-nt/etc/hosts;thencp/etc/hosts/etc/hosts$(date+%Y%m%d%H%M%S)~\cp-fhosts/hosts/etc/hostselserm-f/etc/hosts\cp-fhosts/hosts/etc/hostsfifunctionvalidate_network_to_outside(){echo_b"validatinghostsfile..."http_code=$(curl-o/dev/null-m10--connect-timeout10-s-w"%{http_code}"https://www.google.com.hk/)if[$http_code-ne200];thenecho_g"Replacehostsfilesucceeded!"echoecho_g"NowyoucanaccessGoogle,etceasily!"elseecho_r"replacehostsfilefailed!"exit1fi#mainfunction#Runsetupforeachdistroaccordingly,installgithere.cat- eof$headercheck_network_connectivitycheck_name_resolvecheck_linux_distributioncase"$lsb_dist"inamzn);;'opensuseproject'|opensuse);;'suselinux'|sle[sd]);;ubuntu)command_existsgit||apt_get_install_packagesgit;;centos)command_existsgit||yum_install_packagesgit;;*)echo_r"unsupportedsystemtype"exit1get_hosts_file_from_githubvalidate_network_to_outside

一些有用的参考:

修改 hosts 文件的原理是什么? https://www.zhihu.com/question/19782572

github/hosts项目wiki https://github.com/racaljk/hosts/wiki

tag: Google hosts,Linux访问Google,Linux翻墙

–end–

人总是珍惜未得到的,而遗忘了所拥有的

Linux Shell脚本之更新hosts文件以便于访问谷歌服务推荐

相关文章:

你感兴趣的文章:

标签云: