Tag 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 = Array.new
ZONE_RE = /^zone "(\w+.\w+)" in/
File.open(ARGV[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 = Whois::Client.new
    domain = c.query(zone)
    if domain.registered?
      if domain.nameservers.detect { |ns| !NS_RE.match(ns.name).nil? }.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 = Whois::Client.new

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 Mail.app, 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', 'http://www.thedailyshow.com/', 
                    'http://example.com/podcasts/', 'Keith T. Garner', 'kgarner@example.com', 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.]

Great syntax highlighting plugin for WordPress

At times, I think I spend more time writing about using wordpress than actually using it sometimes. I’ll need to fix that. Today, thanks to WordPress’s dashboard, I came across the WP-Codebox plugin. THanks to GeSHI, it can color up a lot of languages and fixes some problems I’ve had with other plugins. Honestly, I’ve never been satisfied with any of them. I think this could change that.

Just to show it off, I present to you some random ruby I wrote the other day when I was working on something. I’ll leave it to you to guess what I’m doing and why with this code. (And I also think I could do it better than using a while loop, but I was able to bang it out quickly for what I needed.)

#!/usr/bin/env ruby
require 'fileutils'

offset = 214

files = Dir['*']

count = 1
while !files.empty?
  file = files.delete_at(rand(files.size))
  extention = File.extname(file).downcase
  name = sprintf('%d%s', count + offset, extention)
  puts "#{file} -> #{name}"
  FileUtils.move(file, name)
  count += 1