Skip to content

Instantly share code, notes, and snippets.

@mmzoo
Created September 3, 2012 10:22
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 mmzoo/3608375 to your computer and use it in GitHub Desktop.
Save mmzoo/3608375 to your computer and use it in GitHub Desktop.
A Ruby 1.9.3 snippet that can set IP and FQDN on a Debian/Ubuntu system
#!/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"
content << " dns-nameservers 172.20.0.19"
kill_dhclient! if dhclient?
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 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}
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")
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
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.fqdn
Hostname.set Params.fqdn
end
if Params.ip
unless Interface.identify
puts Help.no_interface_found
exit 3
end
Interface.set Params.ip
end
puts "Finished."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment