• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 879
  • Last Modified:

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

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

0
jpetter
Asked:
jpetter
  • 6
  • 4
  • 2
  • +1
1 Solution
 
ozoCommented:
Try it without the quotes
0
 
jpetterAuthor Commented:
Thanks for the quick response.

I tried what you suggested, but didn't get any response.

$ ./get-msrpc hosts 139
192.168.49.115:139 -- open

192.168.49.11:139 -- open

192.168.49.5:139 -- open
0
 
JESiiCommented:
I thought ARGV[0] was the name of the running program itself...

Try printing the value of ARGV[0] first and see what you get; I think you may have to shift your values one place to the right.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
JESiiCommented:
Sorry... not true; left overs from my Perl days...
0
 
JESiiCommented:
Well... it's saying that it cannot find the program findrpc (your Perl script, I presume?). It doesn't have the .pl extension, which is usually what perl requires to recognize and run a program.  You might try:
1) changing line 38 to read something like:
     cmd = ".perl findrpc #{$address} | grep #{$port}"
or renaming the program to findrpc.pl and trying that
2) making sure that you have the proper privilege on the program with:
     chmod +x findrpc
0
 
jpetterAuthor Commented:
JESii,

Thanks for the suggestions. Unfortunately none of them worked. It's very odd. I'd be embarrased to admit how much time I've spent trying to get this to work. At any rate, below is the output to the various attempts.

Thanks,
Jeff
-----------------------------------------------------------------------------------------------------------------
$ ./get-msrpc hosts 139
192.168.49.115:139 -- open
./findrpc.pl 192.168.49.115 | grep 139
sh: ./findrpc.pl 192.168.49.115 | grep 139: No such file or directory

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

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

$ ./get-msrpc hosts 139
192.168.49.115:139 -- open
perl ./findrpc.pl 192.168.49.115 | grep 139
sh: perl ./findrpc.pl 192.168.49.115 | grep 139: No such file or directory

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

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

$ ./get-msrpc hosts 139
192.168.49.115:139 -- open
perl findrpc.pl 192.168.49.115 | grep 139
sh: perl findrpc.pl 192.168.49.115 | grep 139: command not found

192.168.49.11:139 -- open
perl findrpc.pl 192.168.49.11 | grep 139
sh: perl findrpc.pl 192.168.49.11 | grep 139: command not found

192.168.49.5:139 -- open
perl findrpc.pl 192.168.49.5 | grep 139
sh: perl findrpc.pl 192.168.49.5 | grep 139: command not found
0
 
JESiiCommented:
OK; here are a couple of other things to try:

1) use the %x[...] construct to execute the command. Rather than saying cmd = ... and then result = ..., just say result = %x[...your CLI string....]
2) Failing that, you might then try to execute a shell script (such as findrpc.sh which actually executes your perl script.

Strange though; gotta be something obvious here which we're missing :=)
0
 
JESiiCommented:
I got it to work by using the system() method and putting "bash" in front of the ./findrpc; below is a test from the cygwin command line that shows it working.

$ ruby -e 'system("bash ./findrpc #{$address} | grep #{$port}")
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

Of course, there's that grep warning, but first things first.
0
 
ozoCommented:
Try it without the ""
 $result = `#{cmd}`
0
 
jpetterAuthor Commented:
Thanks a lot for the suggestions guys.

JESii,
When I try using the %x[....] construct, I get this for my response:
$ ./get-msrpc hosts 135
192.168.49.115:135 -- open
./findrpc 192.168.49.115 | grep 135
sh: ./findrpc 192.168.49.115 | grep 135: No such file or directory

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

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

which is very similar to my initial output.
I was having a tough time figuring out how to insert that command line version you suggested, and when I did, this is the response I was getting:
$ ./get-msrpc hosts 135
192.168.49.115:135 -- open
ruby -e system(bash ./findrpc 192.168.49.115 | grep
sh: ruby -e system(bash ./findrpc 192.168.49.115 | grep : No such file or directory

192.168.49.11:135 -- open
ruby -e system(bash ./findrpc 192.168.49.11 | grep
sh: ruby -e system(bash ./findrpc 192.168.49.11 | grep : No such file or directory

192.168.49.5:135 -- open
ruby -e system(bash ./findrpc 192.168.49.5 | grep
sh: ruby -e system(bash ./findrpc 192.168.49.5 | grep : No such file or directory

Ozo,
I tried your suggestion, but it seemed as though it didn't reference the value of the variable cmd. When I ran this, the line where I am writing the output to the screen was blank. It looked like:
$ ./get-msrpc hosts 135
192.168.49.115:135 -- open
./findrpc 192.168.49.115 | grep 135

192.168.49.11:135 -- open
./findrpc 192.168.49.11 | grep 135

192.168.49.5:135 -- open
./findrpc 192.168.49.5 | grep 135

I really appreciate the suggestions. I'll just keep on trying what I can.

Thanks,
Jeff
0
 
requeueCommented:
Check findrpc script output.

When I test with following code, output was visible.
#! /usr/bin/perl

print "192.168.49.5:135"

Open in new window

0
 
JESiiCommented:
You are missing the single quotes surrounding the ruby statement on your command line example; it should be:

   ruby -e 'system("bash ./findrpc #{$address} | grep #{$port}")'

(and I see that the trailing quote was missing in my code; cut/paste error; sorry)
0
 
jpetterAuthor Commented:
JESii,

Thanks for spotting that...it made the difference. It seemed very odd that I should have to do this, but as long as it works, I can try to reconcile it later.

Thanks to all for your help.

Jeff
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now