Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Mark Percival www.mpercival.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Mac Wifi MAC spoofing

A certain unnamed national provider of bagels has seen fit to limit free wifi during lunch hours. They do this based on MAC address. I never liked my MAC address anyways.


Update -- My previous code had two considerable bugs when I posted it. Caius Durling (http://caius.name/) made a considerable improvement with his own script. I've deleted my old code and just placed his here.
   1  
   2  #!/usr/bin/env ruby
   3  # 
   4  # USAGE: Turn off wireless, run this, then turn it back on.
   5  # ./random_mac.rb <nothing> # =>  random mac address
   6  # ./random_mac.rb --reset # =>  original mac address
   7  
   8  # Set this to your Original MAC Address
   9  original_mac = ""
  10  
  11  # Just a little warning
  12  puts "Warning: You haven't set your original Mac Address in the script yet." if original_mac.empty?
  13  
  14  # Generates a 2 digit code
  15  def generate_pair
  16    a = rand(255).to_s(16)
  17    return (a.length < 2 ? generate_pair : a)
  18  end
  19  
  20  # Sets the mac address to the passed value
  21  def set_mac_address( mac_addr )
  22    raise "MacAddressFormatError" if mac_addr.scan(/^(?:[\w\d]{2}\:){5}[\w\d]{2}$/).empty?
  23    puts "Old MAC address: #{`ifconfig en1 | grep ether`.match(/ether\s(.+)/)[1]}"
  24    `sudo ifconfig en1 ether #{mac_addr}`
  25    puts "New MAC address: #{`ifconfig en1 | grep ether`.match(/ether\s(.+)/)[1]}"
  26  end
  27  
  28  
  29  
  30  case ARGV.first
  31  when "--reset"
  32    # Try to reset to old mac address
  33    if original_mac.empty?
  34      puts "ERROR - You need to set the old mac address in the script"
  35      exit(1)
  36    end
  37    puts "Resetting to old mac address"
  38    set_mac_address( original_mac )
  39    
  40  when nil
  41    # Generate & set a random mac address
  42    random_mac = (0..5).map { |x| generate_pair }.join(":")
  43    set_mac_address( random_mac )
  44    
  45  when "--help"
  46    # Show some help I guess
  47    puts [
  48      "USAGE: Turn off wireless, run this, then turn it back on.",
  49      "./random_mac.rb <nothing> # =>  random mac address",
  50      "./random_mac.rb --reset # =>  original mac address"
  51      ].join("\n")
  52    
  53  else
  54    # Who knows?
  55    puts "Unknown Flag, try --help"
  56    exit(2)
  57  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS