troubleshooting Question

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

Avatar of jpetter
jpetter asked on
SoftwareRuby
13 Comments1 Solution958 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
JESii

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros