preload
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:
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:
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:
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 07

Every once and a while I get too fancy for my own good. :) A customer of mine has lots and lots (well…hundreds really) of Unix hosts that need updates of config files and such. There are lots of tools out there that can do what I need to do, but they all require an install on the nodes themselves. My customer does not want any extra software on these nodes, so to do any kind of automation requires me to upload the program, run it remotely, then remove it. I have a large number of tools to do all that several different ways.

Recently though, I needed to download a large file from each node while running another program on the node at the same time. Normally I would use SCP for the download part, but for reasons that would take too long to explain, that option was not open to me. So for some reason, I was struck with the ideal to just use a custom XMLRPC client/server to do the job. Turns out the code is pretty simple. My customer only has Python on these nodes, so my XMLRPC client is written in Python:

#
# xmlxfer.py
#
import xmlrpclib
import sys
import platform

port = 80    # Some port that passes through your firewall:  80, 21, 23, 25, etc.

farfname = sys.argv[1]
localfilename = sys.argv[2]
server = sys.argv[3]
#host = platform.uname()[1]
host = platform.node()
farname = host + "-" + farfname

ff = open(localfilename, "r").read()

svr = "http://" + server + ":" + port + "/RPC2"
xmlprxy = xmlrpclib.ServerProxy(svr)

print xmlprxy.xfer(farname, ff)

Continue reading »

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

While there are plenty of places on the web that talk about Ruby Arrays and Ruby Classes, I never have found one that just explained how to use a Class as an Array. So I thought I would post one for those who might want something simple to reference :)

First, the sample code:

class Test
  @dd = Array.new
  def initialize
    @dd = loadArray()
  end

  def [](a)
    @dd[a]
  end

  def []=(a, b)
    @dd[a] = b
  end

  def list
    @dd.each do |i|
      puts i
    end
  end

  def sort
    aa = Array.new
    aa = @dd.sort
    @dd = aa
  end

  private

  def loadArray
    rr = Array.new
    rr << "one"
    rr << "two"
    rr << "three"
    rr << "four"
    return rr
  end  

end

Continue reading »

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

In my line of work I end up trying to move a large amount of text from web pages into things like Word and Excel. Getting the Text from the Web Browser is easy…Putting the text with formatting into an Office app could be a lot of work trying to parse through all the HTML and send the equivalent formatting commands with the text. Fortunately, Word and Excel had the ability to paste from the clipboard HTML and render it with the correct formatting! …Only problem is that for some reason, no one added HTML support in the Win32::Clipboard gem?!? (At least not in the 1.8.x version of Ruby, which I use.) So I spent a few hours looking over the existing clipboard.rb file, found a VBA example of an HTML Copy, and came up with the following code: Continue reading »

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

If you try to “compile” a Ruby script that has the Watir gem in it with OCRA, you will find that running the compiled .exe file on a computer without the Watir gem previously being installed may result in this error:

c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/ie.rb:113:in `initialize': unknown OLE server: `AutoItX3.Control' (WIN32OLERuntimeError)
    HRESULT error code:0x800401f3
      Invalid class string      from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/ie.rb:113:in `new'
        from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/ie.rb:113:in `autoit'
        from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/ie-class.rb:425:in `autoit'
        from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/ie-class.rb:422:in `set_window_state'
        from c:/ruby/lib/ruby/gems/1.8/gems/watir-1.6.2/lib/watir/ie-class.rb:398:in `maximize'
        ...

Huh? Why am I getting a WIN32OLE error? This all runs fine on my computer when I tested it! … Well, it seems that Watir uses its own version of a win32ole gem, and not the one that you already have installed. In fact, when you compile a Ruby script that has both win32ole and watir gems, you will need to comment out the “require ‘win32ole’” line in order for it to work. Anyway… as part of the win32ole gem install, it seems that it registers the AutoItX3.dll file into the registry. OCRA will, however, *NOT* copy this file over and register it for you, so you may see the error above.

So…the trick is add the AutoItX3.dll file to your OCRA compile, and to temporarily register the DLL before calling watir or win32ole commands. I simply copied the DLL from the win32ole gem directory to my Ruby script’s working directory, and then added it to my OCRA compile command:

C:\Server4\Dev\MyProg>ocra --console --icon c:/Server4/Dev/icons/exonets.ico myprog.rb AutoItX3.dll

OCRA will add the DLL to the EXE and when run will place it in the current temporary directory. After that you need to run the DLL register command to make it an OLE server, then when done, be sure to unregister it before your program completes.
Here a sample of code that I use to accomplish all this: Continue reading »

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

While doing a quick search for Ruby code that could catch and print out the Beacon packet that Tivo machines send out on the local network, I was shocked to not find anything….tons of stuff in Perl….but nothing Ruby! Unbelievable! So I went and figured out what the code would be in Ruby. Here’s what I got:

require 'socket'
require 'exo/xdump'

trap("INT") do
   puts "---[Ctrl-C: Exiting]---\n\n"
   Kernel.exit(0)
end

BasicSocket.do_not_reverse_lookup = true
s = UDPSocket.new
s.bind("0.0.0.0", 2190)

loop do
   flg = IO.select([s], nil, nil, 5)
   if flg
      text, sender = s.recvfrom_nonblock(1024)
      puts "[#{Time.now}] #{sender[3]} sent beacon packet:"
      puts "[#{Time.now}]\n#{text.hexdump}"
   end
end

Nothing too fancy. The beacon is sent out as a UDP packet to port 2190. The code looks for data on the socket, then retrieves the the data and prints it out in a hex dump format like so:

[Sun Mar 07 20:37:53 -0800 2010] 10.11.12.52 sent beacon packet:
[Sun Mar 07 20:37:53 -0800 2010]
000000: 7469 766f 636f 6e6e   6563 743d 310a 7377 |tivoconnect=1.sw|
000010: 7665 7273 696f 6e3d   392e 332e 3262 2d30 |version=9.3.2b-0|
000020: 312d 322d 3134 300a   6d65 7468 6f64 3d62 |1-2-140.method=b|
000030: 726f 6164 6361 7374   0a69 6465 6e74 6974 |roadcast.identit|
000040: 793d 3234 3030 3030   30Xx XxXx XxXx XxXx |y=2400000XXXXXXX|
000050: Xx0a 6d61 6368 696e   653d XxXx XxXx XxXx |X.machine=XXXXXX|
000060: 0a70 6c61 7466 6f72   6d3d 7463 642f 5365 |.platform=tcd/Se|
000070: 7269 6573 320a 7365   7276 6963 6573 3d54 |ries2.services=T|
000080: 6956 6f4d 6564 6961   5365 7276 6572 3a38 |iVoMediaServer:8|
000090: 302f 6874 7470 0000   0000 0000 0000 0000 |0/http.         |

The identity and the machine values have been changed just to be safe :)

A big issue that a lot of folks have with UDP sockets in Ruby is trying to get a non-blocking read working correctly. The use of the IO.select above seems to be the best answer that I’ve seen…but its still not perfect. Some folks have been trying out eventmachine (See the “Non-Blocking UDP” thread over on Ruby Forum), but for this simple test, the IO.select should work just fine.

I covered the hexdump function in a previous post.

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

Ever wanted to have a spell check function in your Ruby Program? As long as you have MS Word installed, you can use the Spellcheck function from your program. Here’s the code:

require 'exo/iswindows'

if not RUBY_PLATFORM.isWindows?
   puts "This program only runs under Windows!"
   exit
end

require 'win32ole'

def spellcheck(scstring)
   word = WIN32OLE.new('Word.Application')
   doc = word.Documents.Add     ## Blank document

   word.Selection.Text = scstring
   word.Dialogs(828).Show

   ### return the corrected text
   if not scstring[/ /]      ## only one word with no spaces in the string
     # highlight the word first,
     word.Selection.MoveLeft( 'Unit'=>2,
            'Count'=> 1,
            'Extend'=>2)
   end
   ## multiple words end up already selected after the spell check
   # then retrieve.
   correct = word.Selection.Text
   doc.close(0)
   word.Quit
   return correct
end

if __FILE__ == $0
   puts "Corrected => #{spellcheck(ARGV[0])}"
end

The program opens a new document, pastes the text to check into the document, and then brings up the spellcheck dialog box with any words it can’t find in the dictionary and prompts you to correct the mistakes. Here’s an sample output:

C:\Server6\Dev\Ruby>ruby spellcheck.rb "this is a test srting to seee how the slpell check is working"
Corrected => This is a test string to see how the spell check is working

C:\Server6\Dev\Ruby>ruby spellcheck.rb  antidisestablishmenttarianism
Corrected => antidisestablishmentarianism

For some reason, if you check multiple words, the dialog auto-closes after the last ‘fix’ …but if its just one word, it stays open until you click the close button. Conversely, multiple words stay selected in the Word doc after the dialog closes, but with only one word, it does not stay selected, and you have to select it back in order to read it off the page of the document.

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