Skip to content

Instantly share code, notes, and snippets.

@jezman
Created February 13, 2018 14:21
Show Gist options
  • Save jezman/bbc3a9214845146356f3c1d856ece363 to your computer and use it in GitHub Desktop.
Save jezman/bbc3a9214845146356f3c1d856ece363 to your computer and use it in GitHub Desktop.
Fill DHCP table.
#!/usr/bin/env ruby
# Fill DHCP table.
require 'optparse'
options = {}
OptionParser.new do |opt|
opt.banner = 'Usage: ruby dhcpfill.rb [options]'
opt.on('-i', '--interface NAME', 'Network interface name') { |interface| options[:interface]=interface }
opt.on('-m', '--mac XX:XX:XX:XX:XX:XX', 'Mac address. Default - random') { |mac| options[:mac]=mac }
opt.on('-h', '--help', 'Displays Help') { puts opt }
end.parse!
rand_mac = (1..6).map { "%0.2x" % rand(256) }.join(':')
interface = options[:interface] if options[:interface]
rand_mac = options[:mac] if options[:mac]
if options[:interface]
if rand_mac =~ /^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$/i
puts `sudo ifconfig #{interface} down`
puts `sudo ifconfig #{interface} up`
puts `sudo ifconfig #{interface} hw ether #{rand_mac}`
puts("New mac on #{interface}: #{rand_mac}")
else
puts 'Invalid mac-address'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment