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

« Newer Snippets
Older Snippets »
Showing 1-10 of 30 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.
#!/usr/bin/env ruby
# 
# USAGE: Turn off wireless, run this, then turn it back on.
# ./random_mac.rb <nothing> # =>  random mac address
# ./random_mac.rb --reset # =>  original mac address

# Set this to your Original MAC Address
original_mac = ""

# Just a little warning
puts "Warning: You haven't set your original Mac Address in the script yet." if original_mac.empty?

# Generates a 2 digit code
def generate_pair
  a = rand(255).to_s(16)
  return (a.length < 2 ? generate_pair : a)
end

# Sets the mac address to the passed value
def set_mac_address( mac_addr )
  raise "MacAddressFormatError" if mac_addr.scan(/^(?:[\w\d]{2}\:){5}[\w\d]{2}$/).empty?
  puts "Old MAC address: #{`ifconfig en1 | grep ether`.match(/ether\s(.+)/)[1]}"
  `sudo ifconfig en1 ether #{mac_addr}`
  puts "New MAC address: #{`ifconfig en1 | grep ether`.match(/ether\s(.+)/)[1]}"
end



case ARGV.first
when "--reset"
  # Try to reset to old mac address
  if original_mac.empty?
    puts "ERROR - You need to set the old mac address in the script"
    exit(1)
  end
  puts "Resetting to old mac address"
  set_mac_address( original_mac )
  
when nil
  # Generate & set a random mac address
  random_mac = (0..5).map { |x| generate_pair }.join(":")
  set_mac_address( random_mac )
  
when "--help"
  # Show some help I guess
  puts [
    "USAGE: Turn off wireless, run this, then turn it back on.",
    "./random_mac.rb <nothing> # =>  random mac address",
    "./random_mac.rb --reset # =>  original mac address"
    ].join("\n")
  
else
  # Who knows?
  puts "Unknown Flag, try --help"
  exit(2)
end

Kill the cornball dock in Leopard

defaults write com.apple.dock no-glass -boolean YES
killall Dock

Mac Menu effect js

Here the html code

<html>
<head>
<title>JS</title>
<script src="js/prototype.js" type="text/javascript"></script>
<script src="fisheye_new_2.js" type="text/javascript"></script>
<style>
#menuBar{ width: 100%; text-align: center; position: relative; }
#menuitems { text-align: center; margin-left: auto; margin-right: auto; width: 200px; }
.menuitem { vertical-align: top; display: inline; background-color: #f00; }
.menuitem img { vertical-align: top; width: 20px; height: 20px; border: 0px; }
#logDiv { font-size: 10px; }
</style>
</head>
<body>
	<div id="menubar">
		<div id="menuitems">
			<div class="menuitem"><img onclick="alert(this.id);" src="test2.jpg" id="img1"></div>
			<div class="menuitem"><img onclick="alert(this.id);" src="test2.jpg" id="img2"></div>
			<div class="menuitem"><img onclick="alert(this.id);" src="test2.jpg" id="img3"></div>
			<div class="menuitem"><img onclick="alert(this.id);" src="test2.jpg" id="img4"></div>
			<div class="menuitem"><img onclick="alert(this.id);" src="test2.jpg" id="img5"></div>
			<div class="menuitem"><img onclick="alert(this.id);" src="test2.jpg" id="img6"></div>
		</div>
	</div>
	<div id="position"></div>
	<div id="logDiv"></div>
</body>
</html>


and the javascript, require prototypejs
var maxDist = 0;

function mousemove(e) {
	var eX = Position.page($('menuitems'))[0];
	var eY = Position.page($('menuitems'))[1];
	var eWidth = parseInt($('menuitems').getStyle('width'));
	var pX = Event.pointerX(e);
	var pY = Event.pointerY(e);
	var imgs = $('menuitems').immediateDescendants();
	for (var i = 0; i < imgs.length; i++) {
		var distFromMouse = calcDistFromMouse(pX, pY, imgs[i].firstDescendant());
		if (distFromMouse < 45) {
			var newSize = parseInt(distFromMouse * (-0.4) + 50);
			imgs[i].firstDescendant().setStyle({ width: newSize, height: newSize});
		} else {
			imgs[i].firstDescendant().setStyle({ width: 20, height: 20});
		}
	}
}

function resetScaling () {
	var imgs = $('menuitems').immediateDescendants();
	for (var i = 0; i < imgs.length; i++) {
		var newSize = 20;
		imgs[i].setStyle({ width: newSize, height: newSize});
	}
}

function calcDistFromMouse(mX, mY, elem) {
	var elemCenterX = parseInt(Position.page(elem)[0]) + (parseInt(elem.getStyle('width')) / 2);
	var elemCenterY = parseInt(Position.page(elem)[1]) + (parseInt(elem.getStyle('height')) / 2);
	var distance = parseInt(Math.sqrt((elemCenterX - mX) * (elemCenterX - mX) + (elemCenterY - mY) * (elemCenterY - mY)));
	return distance;
}

function checkMousePos(pX, pY, eX, eY, eWidth ){
	if (pY - eY > 200)
		return false;
	if (pX > eX) {
		if (pX - (eX + eWidth) > 200)
			return false;
	} else {
		if (eX - pX > 200)
			return false;
	}
	return true;
}

Event.observe(window,'load', function() {
  Event.observe(document,'mousemove',mousemove, false);
});

restart appache

// description of your code here

sudo apachectl restart
..

Building curb under Mac OS X

Had a bit of a problem getting curb to work in OS X, but if you have the curl port installed, its easy:

sudo port install curl
cd src
curl -O http://rubyforge.iasi.roedu.net/files/curb/curb-0.1.0.tar.gz
tar -zxvf curb-0.1.0.tar.gz
cd curb-0.1.0.tar.gz
ruby extconf.rb --with-curl-lib=/opt/local/lib/ --with-curl-include=/opt/local/include/
ruby tests/alltests.rb
sudo make install

Configure command key as alt-gr key in a Apple Powerbook with Ubuntu GNU/Linux

Set this Option in "InputDevice" section at your /etc/X11/xorg.conf

Option          "XkbOptions"    "lv3:lwin_switch"

Ruby HMAC verifier

// Ruby script to verify the HMAC of a file or string.

#!/usr/bin/env ruby
#
#  Created by Jon (exabrial+hmacruby@gmail.com) on 2006-11-04.
#  Copyright (c) 2006. All rights reserved.
#  Released under MIT License

require 'openssl'
require "getopt/std"
include OpenSSL
include Digest

def printhelp
  help=<<end
Purpose: Provides HMAC-SHA1 of a file or string. Text passwords are SHA1 hashed.
Usage: hmac.rb ["string to digest"] [-f (pathtofile)] [-k (pathtokeyfile)]
Options:
  -f (pathtofile) digests a file instead of a string
  -k (pathtokeyfile) does not prompt for key and uses the specified file as a key instead.
end
  puts help
  exit
end

if ARGV.size < 1
  printhelp
elsif ARGV.size==1
  @plaintext=ARGV.shift
else
  begin
    opt = Getopt::Std.getopts("f:k:")
  rescue Getopt::StdError
    printhelp
  end
  
  if opt["f"]
    @plaintext=File.read(opt["f"])
  end
  if opt["k"]
    @key=File.read(opt["k"])
  end
  
  printhelp if (!@plaintext&&!@key)
end

def getkey
  return @key if @key
  print "Please type your key then push enter:"
  return SHA1.new(gets()).to_s
end

def main
  hmacd=HMAC.new(getkey(), SHA1.new)
  hmacd.update(@plaintext)
  puts hmacd.to_s
end

main

Use Launchd to run Rsnapshot daily

// run rsnapshot daily at 3am

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>local.my-mac.rsnapshot-daily</string>
        <key>ProgramArguments</key>
        <array>
                <string>/opt/local/bin/rsnapshot</string>
                <string>daily</string>
        </array>
        <key>StartCalendarInterval</key>
        <dict>
                <key>Hour</key>
                <integer>3</integer>
        </dict>
</dict>
</plist>

Restart apache on Mac OS X

sudo apachectl restart

Change iChat status from terminal.app

osascript ichatloc.scpt "This is a test"
« Newer Snippets
Older Snippets »
Showing 1-10 of 30 total  RSS