preload
Jan 02

I finally managed to pick the right folks this year to dominate my Fantasy Football league this year. I ended up 13-1, and only lost that one game by 2 points, since I was on the road that week and didn’t rotate out an IA player. The second place team ended up with a 10-4 record. I did have a stroke of luck when I found Cam Newton in the ranks of QB and picked him up as a second QB behind Aaron Rodgers on week 2… right before Cam moved up to starting QB.

Mossybacks are Champs!

Hopefully next year I’ll be just as lucky :)

Happy New Year everyone!!

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Tagged with:
Dec 01

If you do any amount of work with F5 Nework’s iRules scripting language, eventually you run into the need to print out the contents of a packet you are working with to make sure you are processing the packet correctly, getting the fields lined up, getting sent the values you think you are being sent, etc.  Personally, I’m used to using a very common hexdump format that I’ve created Ruby methods for in the past…but I could not find anything similar for iRules, so I wrote my own.

The following code implements the hexdump:

if { $static::DEBUG eq 1 } {
        #  The hexbinary code we want to decode is stored in $payload
        ##
        ## format string for hexdump output
        ##
        set p 0     ;## buf ptr
        set sl [string length $payload]
        set inPkt "\n\n"
        while { $p < $sl } {
            set s [string range $payload $p [expr {$p+16}] ]
            binary scan $s H*@0a* hex ascii
            regsub -all -- {[^[:graph:] ]} $ascii {.} ascii
            set hex1   [string range $hex   0 15]
            set hex2   [string range $hex  16 31]
            set ascii1 [string range $ascii 0  7]
            set ascii2 [string range $ascii 8 15]
            # Convert the hex to pairs of hex digits
            regsub -all -- {..} $hex1 {& } hex1
            regsub -all -- {..} $hex2 {& } hex2
            append inPkt "[format {%08x  %-24s %-24s %-8s %-8s} $p $hex1 $hex2 $ascii1 $ascii2]\n"
            set p [expr {$p + 16}]
        }
        ###
        puts "Input PKT: $inPkt"            ;## print the output to /var/log/tmm
}

Continue reading »

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Tagged with:
Sep 13

When I’m writing iRules programs on my MacBook Pro as part of my job at F5 Networks, I like to copy in configuration and setup information as part of documenting the program.  This entails cutting and pasting in multi-line config file sections and then adding the ‘#’ comment character to the start of each line.  Well, after doing that a couple of times, I knew there was a way to automate that.  Enter some Applescript:

(*  Comment/Uncomment Block
    Add & remove '# ' in Selection. Good for iRules comments.
	Assign to a key for best use.

	John Allen, F5 Networks
	Sept. 12, 2011
*)

tell application "BBEdit"
	activate
	repeat with x in lines of selection of window 1 of text document 1
		if exists character 1 of text of x then
			set start to character 1 of text of x as text
		else
			set start to ""
		end if
		if start is not "#" then
			replace "^" using "# " searching in text of x options {search mode:grep}
		else
			replace "^# " using "" searching in text of x options {search mode:grep}
		end if
	end repeat
end tell

Of course, you can modify this to work with any programming language.

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Tagged with:
Aug 22

When I went to do a restore of my locked up iPhone (Thank you iOS 4.3.5 update), I plugged it in and it asked for a password before it could do the restore. Password?? I don’t remember putting any password on my backup?!?   Doing a quick google turns up a bunch of people looking for help, with lots of other people telling them they shouldn’t steal ;)

After trying a number of different passwords, I finally made the connections:  Its the password of your computer login Id you are logged in as.  Not any security PIN, not your AppleID password.

Hope that helps someone! :D

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Tagged with:
Jul 13

I was writing a Ruby program today that just needed a very simple way to control multiple web requests to a Sinatra server from querying a remote server at the same time. I would think that Sinatra does have some nice way to do this(and if you know it, please send me the link :) ), but I didn’t have time to dig into it, so I pulled out my old trusty Lockfile class.

The concept is very simple: Open a file, put a random string into it, close it. I use the random string to make sure that my process really did create the file, rather than another process running at the same time. The verify method can be called at any time to check that we still have the lock….well, that we are suppose to have the lock ;) If the file already exists, then you don’t get the lock. If you can’t open the file, then someone else must have just opened it, and again no lock.

Now the source… Continue reading »

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Tagged with:
Jul 12

There are a lot of posts out there about how to add commas to numbers, but I haven’t seen any that showed how to make it a method of the built-in number types. Its very easy actually, but for those who are beginner Rubyists, here’s how I did it:


class Bignum

  def commas
    self.to_s =~ /([^\.]*)(\..*)?/
    int, dec = $1.reverse, $2 ? $2 : ""
    while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3')
    end
    int.reverse + dec
  end

end    

class Float

  def commas
    self.to_s =~ /([^\.]*)(\..*)?/
    int, dec = $1.reverse, $2 ? $2 : ""
    while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3')
    end
    int.reverse + dec
  end

end

class Fixnum

  def commas
    self.to_s =~ /([^\.]*)(\..*)?/
    int, dec = $1.reverse, $2 ? $2 : ""
    while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3')
    end
    int.reverse + dec
  end

end

Its the same function added to the three main number classes(Bignum, Float, and Fixnum). You would use them like so:

irb(main):001:0> require 'exo/format'
=> true
irb(main):002:0> g = 123456789
=> 123456789
irb(main):003:0> g.commas
=> "123,456,789"
irb(main):004:0> f = 123456.7891
=> 123456.7891
irb(main):005:0> f.commas
=> "123,456.7891"
irb(main):006:0> b = 12347862389461237846192873461287346
=> 12347862389461237846192873461287346
irb(main):007:0> b.commas
=> "12,347,862,389,461,237,846,192,873,461,287,346"
irb(main):008:0>

(exo/format is just the filename I use for the code above) These methods have the added benefit of converting the number into a string so you don’t have to convert before printing out.

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Jun 03

Recently I decided that I needed a copy of an ‘fport’ program for Windows that would let me see what program was making connections out of my computer. Unfortunately, my Anti-Virus software warned me that the website where it is posted was on the known virus websites list, so I just decided to make my own.

I started looking around for a programatic way to do what the normal ‘netstat’ program does, but everything I found was rather involved…and since I didn’t want to spend a whole lot of time on it, I cheated and just used the output from the ‘netstat -ano’ command and then did a quick lookup of the returned PID to find the program.

The result is the following code: Continue reading »

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Tagged with:
May 22

Black Cat Systems sells a number of radiation meters that will capture and measure all kinds of radiation (Alpha, Beta, Gama, X-Ray, etc.). I bought one of these several years ago and recently unearthed it from my Pile of Forgotten Electronic Projects and hooked it up to one of my computers. The software that comes with it allows for an ftp upload, so I set it all up and its now uploading its readings to the RadMeter page every five minutes or so. Now you too can see if I’m living under fallout or not :)

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Apr 28

From my bag of tricks…

On occasion, I run into the need to present some kind of table information on to a console/terminal session so that the information is readable. After doing this several times, I decided to write a text table class so that i didn’t have to worry about formatting the table within my code. I just set the table up with column titles and column widths and just focus on the data.

Here’s the code:

#
#  texttable.rb  --  testing tool to output a nicely formatted table.
#
# John Allen, June 2005
#  June 2010 -- Added support for word wrap fields
#

class TextTable
  #
  # tableinfo = Hash.new {
  #    "name" =>  Array(:string),
  #    "width" => Array(:fixnum),
  #    ["linebetweenrows" => Boolean,]
  #    ["hdrlinechar" => "=",]
  #    ["rowlinechar" => "-",]
  #    ["wordwrap" => Boolean]
  # }

  attr_accessor  :names,  :widths, :hdr_print_flg, :lbr_flg, :hdrchar, :linechar, :wordwrap

  @hdr_print_flg = false
  @ldr_flg = false
  @wordwrap = false

  def initialize(tableinfo)
    @names = tableinfo["name"]
    @widths = tableinfo["width"]
    if tableinfo["linebetweenrows"]
      @lbr_flg = true
    end
    if tableinfo["wordwrap"]
      @wordwrap = true
    end
    @hdrchar = tableinfo["hdrlinechar"] || "="
    @linechar = tableinfo["rowlinechar"] || "-"
  end

  def printrow(a)
    # a = Array
    buf = ""
    if not @hdr_print_flg
      buf << printHdr()
    end
    buf << "|"
    extra = []
    a.each_with_index do |n,i|
      if n.length > @widths[i]        ## check to see if value is bigger than field; chop if so
        b = n.slice(0..(@widths[i] -1))
        extra[i] = n.slice(@widths[i]..-1)
      else
        b = n
      end
      buf << " #{b}#{" "*(@widths[i] - b.length)}|"
    end
    buf << "\n"
    if @wordwrap and not extra.empty?   ## Word Wrap
      eflg = true
      while eflg          ## While stuff to word wrap
        eflg = false
        buf << "|"
        extra.each_with_index do |n,i|
          if not n.nil?
            if n.length > @widths[i]        ## check to see if value is bigger than field; chop if so
              b = n.slice(0..(@widths[i] -1))
              extra[i] = n.slice(@widths[i]..-1)
              eflg = true           ## still more to word wrap!!
            else
              b = n
              extra[i] = nil
            end
            buf << " #{b}#{" "*(@widths[i] - b.length)}|"
          else
            buf << " #{" "*@widths[i]}|"  ## add blank space for non-wordwrap field
          end
        end
        buf << "\n"
      end
    end
    buf << _line(@linechar)  if @lbr_flg
    return buf
  end

  def printHdr
    buf = _line(@hdrchar)
    buf << "|"
    @names.each_with_index do |n,i|
      buf << " #{n}#{" "*(@widths[i] - n.length)}|"
    end
    buf << "\n"
    buf << _line(@hdrchar)
    @hdr_print_flg = true
    return buf
  end

  def printLine
    buf = _line(@linechar)
    return buf
  end

  #-------------------------------------------------------------------------------------------------------#
  private
  #-------------------------------------------------------------------------------------------------------#

  def _line(char)
    b = "+"
    @names.each_with_index do |n,i|
      b << char*@widths[i]
      b << "#{char}+"
    end
    b << "\n"
    return b
  end

end

if __FILE__ == $0
  tt = {
     "name" => ["First","Last","City","State"],
     "width" => [15,15,15,6],
     "linebetweenrows" => false
  }

  names = [
      ["John","Allen","Redmond","WA"],
      ["Herman","Gonzales","Mill Creek","WA"],
      ["Jimmy","Doogle","Bothell","WA"],
      ["Jane","Goodman","Seattle","WA"]
  ]

  table = TextTable.new(tt)

  buf = ""
  names.each do |name|
    buf << table.printrow(name)
  end
  buf << table.printLine
  puts buf

end

Running the example code at the bottom prints out the following result:

C:\Server4\Dev\Ruby\exo>ruby texttable.rb
+================+================+================+=======+
| First          | Last           | City           | State |
+================+================+================+=======+
| John           | Allen          | Redmond        | WA    |
| Herman         | Gonzales       | Mill Creek     | WA    |
| Jimmy          | Doogle         | Bothell        | WA    |
| Jane           | Goodman        | Seattle        | WA    |
+----------------+----------------+----------------+-------+

the printrow() method takes an array of String values to print out. You can have a line printed out between each row if you set the linebetweenrows hash value to ‘true’ (it defaults to ‘false’). There is not a lot of error checking (as in, you can crash the program if you feed the printrow() method an array that is shorter than the number of columns), but since I mainly use it for testing or utilities, I didn’t put a lot in. Its a very handy tool to have around.

Update: I added support for word wrap in all fields recently, so I have updated the code above with that version.

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Tagged with:
Apr 21

One of my common Python tools: use this to list out all the directories from the specified root on down, ignoring some common directories that are specified in the program. You might want to modify this code to read in your ignore list. The program will also optionally take a size limit so that it only lists directories where the storage usage is over that limit.
Continue reading »

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Tagged with: