Search code examples
rubywindowsputtyzabbix

Scripting with PuTTY's PSFTP and suppressing the output


I'm trying to write a ruby script that executes PuTTY's PSFTP program and receive the output to use in further processing and then report a number to Zabbix. However, when I run the script, PSFTP outputs a bit of text that throws off my Zabbix item.

C:\Ruby\>corporate_sftp3 -o xml
Using username "serviceuser". #<--This is what is causing issues
3

The line with the comment is the portion that is throwing off my Zabbix item. Since I'm using Windows, the convient methods that Linux has to throw all output to /dev/null don't apply, and I can't install Cygwin. Is there any way to get Ruby, or PSFTP to not show that bit of text so I can get just the number?

My code is located here: https://github.com/predatorian3/gem_sftp3


Solution

  • After frantically Googling for a day, I finally stumbled across a blog post

    http://blog.bigbinary.com/2012/10/18/backtick-system-exec-in-ruby.html

    this blog post explained what Popen3 was. I put it in my code, and now it only displays the output I wanted to show and not what the external program was trying to show. The block of code looks like

    count = 0
    
    Open3.popen3( psftp_cmd ) do |stdin, stdout, stderr, wait_thr|
      while line = stdout.gets
        count += 1 if line =~ pattern
      end
    end
    
    puts count
    

    then the result I got was:

    C:\Ruby\>corporate_sftp3 -o xml
    3
    

    The final code is posted in the Github repository in the initial post.