Avatar of jpetter
jpetter

asked on 

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

SoftwareRuby

Avatar of undefined
Last Comment
jpetter
Avatar of ozo
ozo
Flag of United States of America image

Try it without the quotes
Avatar of jpetter
jpetter

ASKER

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
Avatar of JESii
JESii
Flag of United States of America image

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.
Avatar of JESii
JESii
Flag of United States of America image

Sorry... not true; left overs from my Perl days...
Avatar of JESii
JESii
Flag of United States of America image

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
Avatar of jpetter
jpetter

ASKER

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
Avatar of JESii
JESii
Flag of United States of America image

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 :=)
Avatar of JESii
JESii
Flag of United States of America image

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.
Avatar of ozo
ozo
Flag of United States of America image

Try it without the ""
 $result = `#{cmd}`
Avatar of jpetter
jpetter

ASKER

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
Avatar of requeue
requeue

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

ASKER CERTIFIED SOLUTION
Avatar of JESii
JESii
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of jpetter
jpetter

ASKER

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
Software
Software

Software is any set of instructions that directs a computer to perform specific tasks or operations. Computer software consists of programs, libraries and related non-executable data (such as documentation). Computer software is non-tangible, contrasted with computer hardware, which is the physical component of computers. Software written in a machine language is known as "machine code". However, in practice, software is usually written in high-level programming languages than machine language. High-level languages are translated into machine language using a compiler or interpreter or a combination of the two.

43K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo