Skip to content

Instantly share code, notes, and snippets.

@mmzoo
Created September 3, 2012 10:22
Show Gist options
  • Select an option

  • Save mmzoo/3608375 to your computer and use it in GitHub Desktop.

Select an option

Save mmzoo/3608375 to your computer and use it in GitHub Desktop.

Revisions

  1. mmzoo revised this gist Sep 12, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions brainwash
    Original file line number Diff line number Diff line change
    @@ -96,6 +96,7 @@ module Brainwash
    content << " address #{ip}"
    content << " netmask 255.255.0.0"
    content << " gateway 172.20.0.1"
    content << " dns-nameservers 172.20.0.19"
    kill_dhclient! if dhclient?
    else
    puts "Enabling DHCP..."
  2. mmzoo revised this gist Sep 11, 2012. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions brainwash
    Original file line number Diff line number Diff line change
    @@ -96,6 +96,7 @@ module Brainwash
    content << " address #{ip}"
    content << " netmask 255.255.0.0"
    content << " gateway 172.20.0.1"
    kill_dhclient! if dhclient?
    else
    puts "Enabling DHCP..."
    content << "iface #{interface} inet dhcp"
    @@ -111,6 +112,15 @@ module Brainwash
    File.open(filename, 'w') { |file| file.write(content) }
    end

    def dhclient?
    %x{ps aux | grep dhclient3 | grep -v grep &> /dev/null}
    end

    def kill_dhclient!
    puts "Killing DHClient..."
    %x{killall -9 dhclient3 &> /dev/null}
    end

    def restart!
    puts "Restarting network..."
    %x{/etc/init.d/networking restart &> /dev/null}
  3. mmzoo revised this gist Sep 4, 2012. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion brainwash
    Original file line number Diff line number Diff line change
    @@ -153,11 +153,17 @@ module Brainwash
    content << '### BRAINWASH END ###'
    content << ''
    store! '/etc/hosts', content.join("\n")
    restart!
    end

    def store!(path, content)
    File.open(path, 'w') { |file| file.write(content) }
    end

    def restart!
    puts "Restarting hostname..."
    %x{/etc/init.d/hostname restart &> /dev/null}
    end
    end

    end
    @@ -192,4 +198,4 @@ if Params.ip
    Interface.set Params.ip
    end

    puts "Finished."
    puts "Finished."
  4. mmzoo revised this gist Sep 3, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions brainwash
    Original file line number Diff line number Diff line change
    @@ -179,6 +179,10 @@ unless File.writable?(Interface.filename)
    exit 2
    end

    if Params.fqdn
    Hostname.set Params.fqdn
    end

    if Params.ip
    unless Interface.identify
    puts Help.no_interface_found
    @@ -188,8 +192,4 @@ if Params.ip
    Interface.set Params.ip
    end

    if Params.fqdn
    Hostname.set Params.fqdn
    end

    puts "Finished."
  5. mmzoo created this gist Sep 3, 2012.
    195 changes: 195 additions & 0 deletions brainwash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,195 @@
    #!/usr/bin/env ruby
    require 'ipaddr'

    module Brainwash

    module Help
    extend self

    def examples
    <<-END
    EXAMPLES
    brainwash dhcp # Use DHCP
    brainwash 172.20.0.99 # Use static IP
    brainwash host.example.com # Set FQDN
    brainwash dhcp host.example.com # Use DHCP and set FQDN
    brainwash 172.20.0.99 host.example.com # Use static IP and set FQDN
    END
    end

    def only_debian_derivates
    <<-END
    Are you sure this is a Debian/Ubuntu system?
    END
    end

    def sudo_needed
    <<-END
    Please run me with super-user privileges.
    END
    end

    def no_interface_found
    <<-END
    No Network Interface found.
    END
    end
    end

    module OS
    extend self

    def debian?
    File.file?('/etc/lsb-release')
    end
    end

    module Params
    extend self

    def ip
    return :dhcp if ARGV[0] == 'dhcp'
    ip? ARGV[0]
    end

    def fqdn
    candidate = ARGV.last
    return nil if ip?(candidate)
    return candidate if candidate.split('.').size > 1
    end

    private

    def ip?(string)
    IPAddr.new(string) rescue nil
    end
    end

    module Interface
    extend self

    def identify
    %w{ eth0 eth1 }.detect { |name| exists?(name) }
    end

    def filename
    '/etc/network/interfaces'
    end

    def set(ip)
    interface = identify
    content = []
    content << "auto lo"
    content << "iface lo inet loopback"
    content << "auto #{interface}"
    if ip.is_a?(IPAddr)
    puts "Enabling static IP #{ip}..."
    content << "iface #{interface} inet static"
    content << " address #{ip}"
    content << " netmask 255.255.0.0"
    content << " gateway 172.20.0.1"
    else
    puts "Enabling DHCP..."
    content << "iface #{interface} inet dhcp"
    end
    content << ''
    store! content.join("\n")
    restart!
    end

    private

    def store!(content)
    File.open(filename, 'w') { |file| file.write(content) }
    end

    def restart!
    puts "Restarting network..."
    %x{/etc/init.d/networking restart &> /dev/null}
    end

    def exists?(name)
    return name if %x{ifconfig #{name} &> /dev/null}
    end
    end

    module Hostname
    extend self

    def set(fqdn)
    hostname = fqdn.split('.').first
    set_hostname!(hostname)
    set_fqdn!(fqdn)
    end

    private

    def set_hostname!(name)
    puts "Setting hostname #{name}..."
    store! '/etc/hostname', name
    end

    def set_fqdn!(fqdn)
    hostname = fqdn.split('.').first
    puts "Setting FQDN #{fqdn}..."
    content = []
    content << '### BRAINWASH START ###'
    content << '127.0.0.1 localhost'
    content << "127.0.0.1 #{fqdn} #{hostname}"
    content << '::1 localhost ip6-localhost ip6-loopback'
    content << 'fe00::0 ip6-localnet'
    content << 'ff00::0 ip6-mcastprefix'
    content << 'ff02::1 ip6-allnodes'
    content << 'ff02::2 ip6-allrouters'
    content << 'ff02::3 ip6-allhosts'
    content << '### BRAINWASH END ###'
    content << ''
    store! '/etc/hosts', content.join("\n")
    end

    def store!(path, content)
    File.open(path, 'w') { |file| file.write(content) }
    end
    end

    end

    include Brainwash

    if ARGV.empty?
    puts Help.examples
    exit
    end

    unless OS.debian?
    puts Help.only_debian_derivates
    exit 1
    end

    unless File.writable?(Interface.filename)
    puts Help.sudo_needed
    exit 2
    end

    if Params.ip
    unless Interface.identify
    puts Help.no_interface_found
    exit 3
    end

    Interface.set Params.ip
    end

    if Params.fqdn
    Hostname.set Params.fqdn
    end

    puts "Finished."