Category Archives: Ruby

quick and dirty dns lint

I run a DNS server that I and a lot of my friends use. The problem with it being “free” and being between friends is sometimes people forget to tell you if they have switched DNS serving arrangements or just haven’t kept the registration.  I wrote a quick and dirty script, thanks to the whois ruby gem, to check to see if all the domains listed in Bind9‘s named.conf are still registered and if so do they still have our nameserver listed as one of the nameservers.  g0ff asked me for a copy, so I thought it might be useful to share with others.

require 'rubygems'
require 'whois'
zones =
ZONE_RE = /^zone "(\w+.\w+)" in/[0]) do |f|
  f.each_line do |line|
    md = ZONE_RE.match(line.chomp)
    zones << md[1] unless md.nil?
NS_RE = /^#{ARGV[1]}$/i
zones.each do |zone|
  c =
    domain = c.query(zone)
    if domain.registered?
      if domain.nameservers.detect { |ns| !NS_RE.match( }.nil?
        puts "#{domain.domain} doesn't seem to have us as a dns server"
      puts "#{zone} doesn't appear to be registered"
  rescue Whois::ResponseIsThrottled
    sleep 120
    c =

FFI for Ruby and an mp4v2 example

Previously on my blog: In my TiVo2Podcast stuff I automated the process of putting chapters around commercials, but had to call out to a small C++ app I wrote to put the chapters in using libmp4v2.

A few weeks ago I was looking at some ruby gems for a project I was working on and stumbled across ffi, a foreign function interface gem for ruby, or as its docs put it: “a ruby extension for programmatically loading dynamic libraries, binding functions within them, and calling those functions from Ruby code.”  As long as you know the function signatures that you need, its pretty trivial to make the calls from Ruby. You do need to be aware of memory management stuff sometimes, but overall its pretty easy, especially for basic use. If you’re only going to be working in Ruby and need access to a C library, this is much easier than mucking with swig, that’s for sure.

The mind-blowing part for me is that the authors of the gem have made it smart enough to know what flavor of ruby vm and platform the code is running in and it does the right thing, no matter if its JRuby or on Windows or whatever. While I haven’t had a chance to use it yet, I suspect this property will be useful with JRuby at work in the future.
Continue reading FFI for Ruby and an mp4v2 example

Using Ruby with Viewpoint unhork an Exchange mailbox

(It’s been awhile since I’ve done a technical post, so I may do that on the next few.)

Through some craziness we haven’t figured out, a user at work managed to get over *pinky to mouth* one million items in their Deleted Items folder in Outlook which is hooked up to our Exchange server. This turned into an interesting problem.

Needless to say, that is a stupid amount of items that Outlook had issues with processing. Telling Outlook to empty the trash would send it out into la-la land as a hung process. I’m thinking it was trying to pull all million items into memory to do a delete on them. I’m sure if we let it sit long enough it may have done something, but it just really didn’t like that volume of stuff.  Similarly, using another Exchange or IMAP client like, Thunderbird, and mutt had similar issues as they needed to retrieve the million headers before they would do anything. Deleting it batches at a time by hand worked, but was slow and required someone to do it by hand which…we can see the issue there.

As is often the case, the solution to the problem could be had by creating a small script to do the pain in the butt stuff for you. Actually, the big reason a script works here is that it could be smart and just snag small batches and work on those. Luckily, a few months back I had played with Viewpoint, a “ruby client access library for Microsoft Exchange Web Services. Previously, I had used Viewpoint to read mail in a certain folder and generate stats for it, so using it to identify and delete messages would be a snap.

The first go around I was grabbing the messages in a batch and deleting them one by one.  That works, but was a bit slow.  I figured out how to do something that hasn’t been written into Viewpoint yet: a batch delete.  That sped up the operation by 4.5 times or so.  This still took over a day to run.  It also couldn’t delete everything.  For some reason it seemed to not be able to delete calendar invites.  After the script was running entirely, we were left with about 30,000 items left behind, but Outlook could handle wiping the rest of them out if we left it alone for 20 minutes or so.
Continue reading Using Ruby with Viewpoint unhork an Exchange mailbox

Tivo2Podcast update

I’ve made a few updates since my last release a few weeks ago. I thought I’d toss an updated version out there.  What’s new in this version:

  • Duration is no longer hard-coded to 32:00 and actually reflects the duration of the show
  • The script will attempt to find the TiVo via Bonjour/mDNS/ZeroConf/DNS-SD/whatever unless passed a -t flag with the TiVo’s IP address. If you have more than one TiVo, it will go with the first one it finds.
  • Moved the stuff in lib to lib/tivo so the package is more easier sucked in by something like encap or stow

Download: tivoscripts-20100314.tar.gz

When I get some motivation later in the week, I’ll put the git archive online, incase anyone wants to clone it and do some development on it.

TiVo -> Video Podcast

Previously on “You can imagine where it goes from here”: We released a script to download stuff from the tivo, and then made some improvements to it.

After two years of saying I was going to fully automate the process of downloading and transcoding shows for my iPhone, I finally got off my ass and did it.  The script is called TiVo2Podcast and it not only does the downloading and transcoding, but it stuffs the resultant video into a an RSS feed for easy consumption/playback by a podcatcher such as iTunes. I’m now automatically getting the shows off my TiVo and onto my iPhone for easy commute-time consumption. (I commute by train, I do not recommend commute-time consumption if you are driving.)

The ruby script wraps tivodecode, HandbrakeCLI, and AtomicParsley and is intended to be run from cron.  I’ve tested this on Linux, but it should run on any UNIX-alike, but it won’t run on windows since I make liberal use of the system() call. Also, this is intended for PERSONAL USE ONLY, do not set up podcast feeds and violate the ethics (and also the laws) of copyright left and right.

This is a very early version and can certainly use some tweaks and enhancements, primarily in configuring the shows you want to capture.  Right now, configuration is in the form of doing INSERT statements in SQLite.  Not very friendly, but it gets the job done until I can make a quick and dirty question based TUI. Here’s an example of setting up getting the best fucking news team on the planet:

INSERT INTO configs (config_name, show_name, rss_filename, rss_link,
                     rss_baseurl, rss_ownername, rss_owneremail, ep_to_keep, encode_decomb)
            VALUES ('tds', 'The Daily Show', 'tds.xml', '', 
                    '', 'Keith T. Garner', '', 4, 1);

Download tivoscripts-20100304.tar.gz and let me know what you think. Make sure you read the README!

[Update 3/5: Forgot to add that all the code I wrote is under the Simplified BSD License, so have at it.]

Place shifting action 3: Revenge of the Sith

tivo.jpgPreviously I put out an early version of my ruby based command line oriented tivo download script.

I’ve had a patch from MARK NOTARUS to make the menu have some more options and I’m using Console::ProgressBar from facets now.

It works well enough for my needs, but let me know if you hit any roadblocks. Just as a reminder, my target was to download stuff off my tivo on one of my headless Linux boxes for batch encoding for my iPhone and/or PSP.

Download tivo-ruby-0.2.tar.gz

Place shifting action 2

tivo.jpgI’ve been working on a command-line oriented TiVo2Go downloader so I can automate getting items off my tivo, or at least do it when I’m on the road via ssh (and then kick off that other script…)  I’ve written a quick and dirty TiVo library, and a sample script called TiVo2Disk which uses the library.

On its own, the TiVo library stuff I whipped up will just download the content from the TiVo still locked up in TiVo’s DRM.  If you pair it with tivodecode (like the sample script does) you can get the video as an MPEG-2 stream.  Beware, HD content is HUGE.

I’ve only tested with my Series 3, and I’ve run it under both OS X and Linux, but it should work on any .  The UI on the sample script is pretty bad, but its an early version.  I’m interested in an feedback, patches, etc anyone might have.  I still need to do some rdoc as well.

This works for me, but I’d like it to be more useful for more people.   In any case, feel free to checkout tivo-ruby-0.1.tar.gz.

fortune for xscreensaver for OS X

I love that jwz ported XScreenSaver to OS X at about the same time I moved to OS X for my laptop.  I got to keep all the screen savers I use and have used over the past decade or so.  Especially some of the cooler GL based ones.  (Its also nice that someone ported rss-glx as well, but that’s a different story.)  The only piece XScreenSaver is missing is the controller to randomly call only selected hacks.  Luckily, RandomExtra fills that hole.

One issue I’ve had with xscreensaver on OS X has been the difficulty/impossibility of having the text displaying hacks use fortune (or any program, for that matter) as the source of the text.  Mike mentioned this morning that all those hacks appear to be able to call out to a URL.  This morning I felt strangely motivated, so I whipped up a quick ruby script that is basically an HTTP based fortune server.  Since ruby ships with OS X, it’ll make it easier in case anyone else wants to get it running as well.  Once I got that working, the question was how to have this launch automatically on OS X so I wouldn’t have to worry about it.  I’ve played with Lingon on and off, and it seemed like the tool to help me do it.  I used Lingon to create a UserDeamon that should launch when one logs in, and should die when one logs out.  I then went through the screen savers that call out to text and pointed them at the localhost URL, and boom, I was back to fortune city.

To recreate this you’ll need this fortune_helper.rb script I whipped up, Lingon to create the launcher or this fortune.plist (put in /Library/LaunchDaemons) and fortune installed somewhere.  I installed fortune via DarwinPorts.