Skip to content

Instantly share code, notes, and snippets.

@GerrieWell
Created June 21, 2018 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GerrieWell/482203380f20b203fa4aca60b1557c8a to your computer and use it in GitHub Desktop.
Save GerrieWell/482203380f20b203fa4aca60b1557c8a to your computer and use it in GitHub Desktop.
my python script for switching host
import easydict
import yaml
import argparse
config = easydict.EasyDict()
config.users_host_path = '/Volumes/more/source/ilotuo/gerrieSwitchhost/user.yaml'
config.host_path = '/etc/hosts'
config.host_prefix = '##user-mark'
def main():
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--disable', dest='disable', action='store_false')
args = parser.parse_args()
user_hosts = {}
with open(config.users_host_path,'r') as chf:
user_hosts = yaml.load(chf)
with open(config.host_path,"r+") as hf:
lines = hf.readlines()
i = len(lines)
for i,line in enumerate(lines):
if line.startswith(config.host_prefix):
lines = lines[:i+1]
break
hf.seek(0,0)
hf.writelines(lines)
hf.truncate()
if args.disable:
for key,value in user_hosts.items():
hf.write('# ' + key+'\n')
hf.writelines(value)
if __name__ == '__main__':
main()
#
#yaml示例
#
#user_host: |-
# 0.0.0.0 weibo.com
# 0.0.0.0 jandan.net
# 0.0.0.0 hupu.com
# 0.0.0.0 taobao.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment