dstrelau (owner)

Revisions

gist: 59057 Download_button fork
public
Public Clone URL: git://gist.github.com/59057.git
Embed All Files: show embed
Text #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env ruby
require 'rubygems'
require 'thor'
 
class Dscl < Thor
  map "-l" => :list, "-a" => :create, "-d" => :delete
 
  desc "list", "list known hosts"
  def list
    raw = `sudo dscl localhost -readall /Local/Default/Hosts`.split('-')
    out = raw.inject(Hash.new {|hash,k| hash[k]=[]}) do |hosts, entry|
      entry =~ /IPAddress: (.*)\nRecordName: (.*)/
      hosts[$1] << $2 unless $1.nil? or $2.nil?
      hosts
    end
    out.each do |ip,hosts|
      puts ip
      puts "=" * ip.length
      hosts.each {|h| puts h }
      puts
    end
  end
 
  desc "create HOST [IP=127.0.0.1]", "add a host"
  def create(host, ip='127.0.0.1')
    `sudo dscl localhost -create /Local/Default/Hosts/#{host} IPAddress #{ip}`
  end
  alias_method :add, :create
 
  desc "delete HOST", "remove a host"
  def delete(host)
    `sudo dscl localhost -delete /Local/Default/Hosts/#{host}`
  end
  alias_method :remove, :delete
 
end
 
Dscl.start