Solved

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

Posted on 2010-09-09
13
870 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

0
Comment
Question by:jpetter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 33639080
Try it without the quotes
0
 

Author Comment

by:jpetter
ID: 33639227
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
 
LVL 12

Expert Comment

by:JESii
ID: 33640582
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 12

Expert Comment

by:JESii
ID: 33640639
Sorry... not true; left overs from my Perl days...
0
 
LVL 12

Expert Comment

by:JESii
ID: 33640730
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
 

Author Comment

by:jpetter
ID: 33641252
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
 
LVL 12

Expert Comment

by:JESii
ID: 33642362
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
 
LVL 12

Expert Comment

by:JESii
ID: 33642412
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
 
LVL 84

Expert Comment

by:ozo
ID: 33642574
Try it without the ""
 $result = `#{cmd}`
0
 

Author Comment

by:jpetter
ID: 33649213
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
 
LVL 2

Expert Comment

by:requeue
ID: 33655005
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
 
LVL 12

Accepted Solution

by:
JESii earned 500 total points
ID: 33655080
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
 

Author Comment

by:jpetter
ID: 33661043
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
The viewer will learn how to create multiple layers to apply various filters and how to delete areas from each layer’s filter.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question