Skip to content

Instantly share code, notes, and snippets.

View seven1m's full-sized avatar

Tim Morgan seven1m

View GitHub Profile
@seven1m
seven1m / ldap_bind.rb
Created September 30, 2008 16:46
net/ldap example code for Active Directory
require 'net/ldap'
# how to "bind" to your ldap/ad server...
LDAP_HOST = 'server'
LDAP_PORT = 389
LDAP_USERNAME = 'cn=Username;cn=Users;dc=domain;dc=com'
LDAP_PASSWORD = 'your user password'
LDAP_BASE = 'dc=domain;dc=com'
# replace "domain" and "com" above with your AD domain
require 'test/unit'
require 'date'
class BirthdayCalculationTest < Test::Unit::TestCase
def test_calculation
bday = Birthday.new(1981, 12, 4)
today = Date.new(2008, 11, 4)
assert_equal 30, bday.days_from(today)
end
#!/bin/sh
# rotating snapshotted backup
src=/my/stuff
dest=/var/backup
today=`date +%a`
yesterday=`date -d yesterday +%a`
mkdir -p $dest/$yesterday # in case it doesn't exist yet
rm -rf $dest/$today # I think this might be necessary -- not sure
# I am learning VIM - here is what I've learned.
# A great reference is here: http://www.cs.runet.edu/~mhtay/CPSC120/VIM_Editor_Commands.htm
# (but I didn't just copy from there -- I tried each one first, and saved the ones my brain might remember here)
# basics
i # insert (edit) mode
o # "open" new line and insert (edit) mode there
ESC # command mode
:help # get help in a split window (:q to close)
:help topic # get help on a topic
# Basically, this bit of code adds a before_filter that checks
# the last modified time of the cache entry, and expires it
# if it is older than the specified time period.
module ActionController
module Caching
module Fragments
# expire a cache key only if the block returns true or
# if the age of the fragment is more than the specified age argument.
def expire_fragment_by_mtime(key, age=nil, &block)
@seven1m
seven1m / print_postscript_to_dymo_from_windows.bat
Created July 30, 2009 00:20
Print a postscript file to a Dymo LabelWriter using Ghostscript on Windows
rem This took me way too long to figure out - hope it helps someone.
rem ghostscript required
c:\path\to\gs\bin\gswin32c.exe -sDEVICE=mswinpr2 -dNoCancel -dNOPAUSE -dSAFER -sOutputFile="%printer%DYMO LabelWriter 450 Turbo" -q "\path\to\file.ps" -c quit
@seven1m
seven1m / gist:167902
Created August 14, 2009 15:42
Instructions for verifying tarsnap download
# I'm a noob
# Instructions for verifying tarsnap download
gpg --import tarsnap-signing-key.asc
# ^ only needed once I think
gpg --decrypt tarsnap-sigs-1.0.25
# should say good sig, cperciva, etc.
# and digest of tgz download, compare with:
openssl dgst -sha256 tarsnap-autoconf-1.0.25.tgz
# hashes should match
* * * * * /path/to/onebody/script/inbox -e production_lite "localhost" "POP-ACCOUNT-USERNAME" "POP-ACCOUNT-PASSWORD"
49 2 * * * /path/to/onebody/script/runner -e production_lite "ActionController::Session::ActiveRecordStore::Session.delete_all(['updated_at < ?', 1.day.ago.utc])"
19 * * * * /path/to/onebody/script/runner -e production_lite "Site.each { Group.update_memberships; NewsItem.update_from_feed; LogItem.flag_suspicious_activity }"
#!/usr/bin/env ruby
# multithreaded script that probes a subnet for used/unused ip addresses
# command line usage:
#
# ./probe_ips.rb [options] [subnet]
#
# subnet defaults to 192.168.1
# -u lists used ips rather than unused
#
#!/usr/bin/env ruby
data = File.read(ARGV.first)
reqs = data.scan(/Completed in (\d+)ms/).map { |r| r.first.to_i }
total_ms = reqs.inject(0) { |s, i| s += i }
avg_ms_per_req = total_ms / reqs.length
reqs_over_500ms = reqs.select { |r| r > 500 }
puts "#{reqs.length} total requests"