正在加载...
2008-3
11
发表于: 未分类 | 作者: xurenlu
标签:

搞这个脚本的目的:因为yahoo目录严格地遵守开发、测试、生产环境三条线的方针,所以我经常需要设置hosts文件,在各种环境中倒来倒去,手动修改hosts文件,非常麻烦。所以,决定搞一个脚本来管理一下.
用法:
a:我要测试了,所以切换到testing环境。运行hosts test.马上就好了。
b:我要添加几个testing 环境的hosts ,运行hosts edit test

具体步骤:
1.建这样一个文件:

  1. #! /usr/bin/ruby
  2. subcmd=ARGV.shift
  3. DATADIR="/home/renlu/bin/hostsdata/"
  4. HOSTFILE="/etc/hosts"
  5. # load the yaml file to a variable
  6. require "yaml"
  7. def init(env)
  8.         data=Hash.new
  9.         open(DATADIR+env+".yaml","w") do |f|
  10.                 f.write(YAML.dump(data))
  11.         end
  12. end
  13. def firstboot
  14.         init("dev")
  15.         init("test")
  16.         init("online")
  17. end
  18. def quit(cmd)
  19.     if cmd =~ /exit|eixt|quit|qt/ then
  20.         exit(0)
  21.     end
  22. end
  23.  
  24. def switch(env)
  25.         data=load(env)
  26.         string=""
  27.         data.each{
  28.                 |domain,ip|
  29.                 string = string +  "#{ip} #{domain}\n"
  30.         }
  31.         open(DATADIR + "pre") do |f|
  32.                 string = f.read + string
  33.         end
  34.         open(HOSTFILE ,"w") do |f|
  35.                 f.write(string)
  36.         end
  37.         print string+"\n"
  38. end
  39. def load(env)
  40.         data=Hash.new
  41.         open(DATADIR + env+".yaml") do |f|
  42.                 data=YAML.load(f)
  43.         end
  44.         data
  45. end
  46. #write the hash as yaml to a file
  47. def save(env,data)
  48.         open(DATADIR+env+".yaml","w") do |f|
  49.                 f.write(YAML.dump(data))
  50.         end
  51. end
  52. def cat()
  53.         cmd2=ARGV.shift
  54.         if cmd2 =~ /dev/ then
  55.                 env="dev"
  56.         elsif cmd2 =~ /online|ol/ then
  57.                 env="online"
  58.         elsif cmd2 =~ /test/ then
  59.                 env="test"
  60.         end
  61.         data=load(env)
  62.         puts data
  63. end
  64. def usage
  65.         print "
  66.         Yet a simple hosts manager version 0.1
  67.         Author :renlu.xu<helloasp@hotmail.com>
  68.         URL             :http://www.162cm.com/
  69.         usage :
  70.                 hosts dev   ---- switch to dev environment
  71.                 hosts test  ---- switch to testing environment
  72.                 hosts online ---- switch to online environment
  73.                 hosts add dev ---- edit a domain for dev|online|test environment
  74.                 hosts add dev ---- the same with 'hosts add dev'
  75.  
  76.         Notice:
  77.                 dev means Development
  78.                 test means Testing
  79.                 online|ol means testing
  80.                 if you input Quit|exit, We asumed that you want to exit.
  81.  
  82.          When you use this program first time,Run 'hosts first ' and then initialze the data files'
  83.  
  84. "
  85. end
  86. def edit()
  87.         cmd2=ARGV.shift
  88.         if cmd2 =~ /dev/ then
  89.                 env="dev"
  90.         elsif cmd2 =~ /online|ol/ then
  91.                 env="online"
  92.         elsif cmd2 =~ /test/ then
  93.                 env="test"
  94.         end
  95.         print "input the domain:(such as www.sina.com)\n"
  96.         domain=gets.chomp
  97.         quit(domain)
  98.         print "input the ip:(127.0.0.1 and so on...\n"
  99.         ip=gets.chomp
  100.         quit(ip)
  101.         data=load(env)
  102.         data[domain]=ip
  103.         save(env,data)
  104.         switch(env)
  105. end
  106. quit(subcmd)
  107. if subcmd =~ /first/ then
  108.         firstboot
  109. elsif subcmd =~ /ls|view|cat/ then
  110.         cat
  111. elsif subcmd =~ /edit|add/ then
  112.         edit
  113. elsif subcmd =~ /dev/ then
  114.         switch "dev"
  115. elsif subcmd =~ /test/ then
  116.         switch "test"
  117. elsif subcmd =~ /online|ol/ then
  118.         switch "online"
  119. else
  120.         usage
  121. end

呵呵。头几行有个设置,一个是hosts文件的地址,一个是存储数据文件的目录。根据你的需要来设置。
3:
建立那个存储你的各位环境hosts信息的目录。
4 Enjoy the script.

5:如果在是linux环境下,你需要给这个脚本设置访问/etc/hosts的权限:

  1. #sudo chown root:root hosts
  2. #sudo chmod +s hosts

这样,这个脚本就自然而然地拥有了root用户的权限。也就是说,一个普通用户也能通过这个脚本去修改hosts文件。

: http://www.162cm.com/archives/609.html

本文相关评论 - 1条评论都没有呢

还没有评论。