We help IT Professionals succeed at work.
Get Started

Problem Running an External Program from Ruby Script - No such file or directory

jpetter
jpetter asked
on
957 Views
Last Modified: 2013-11-15
Hi,
I'm trying to write a small script that will help us streamline remediating vulnerability scans. From within the Ruby script, I call a Perl script to which I pass a few parameters from the command line.

No matter what I do, I get an error similar to:
$ ./get-msrpc hosts 139
192.168.49.115:139 -- open
sh: ./findrpc 105.37.49.115 | grep 139: No such file or directory

192.168.49.11:139 -- open
sh: ./findrpc 105.37.49.11 | grep 139: No such file or directory

192.168.49.5:139 -- open
sh: ./findrpc 105.37.49.5 | grep 139: No such file or directory

I should mention that this command I can run from the command line and it works just fine. Put it in the script, and it bombs out.

I have changed the ./findrpc so that it contained the full path, and still got the same error. I'll paste the code below, and will greatly appreciate any and all help.

I should also note that this is running on Cygwin.

Thanks,
Jeff
#!/usr/bin/ruby

require 'rubygems'
require 'socket'
require 'net/ssh'

$file     = ARGV[0]
$port     = ARGV[1]
$address  = ""
$result   = ""

if ARGV.length != 2
        puts "Usage: #{$0} <ip address list file> <port>"
        exit 0
end

if !($file && FileTest.exists?("#{$file}"))
    puts "Invalid input filename detected."
    puts "Please ensure the filename is correct, the file exists, and is not just a single IP address."
    exit 1
end

File.open("#{$file}", "r") do |infile|
    while (line = infile.gets)
        if  line =~ /(\b(?:\d{1,3}\.){3}\d{1,3}\b)/
            $address = line.chomp!
        end
        begin
            t = TCPSocket.open("#{$address}", "#{$port}")
        rescue
            puts "#{$address}:#{$port} -- closed"
            next
        else
            puts "#{$address}:#{$port} -- open" 
            t.close
        end
      
        cmd = "./findrpc #{$address} | grep #{$port}"
        $result = `"#{cmd}"`
        puts "#{$result}"

    end
    infile.close
end

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 13 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE