aspose file tools*
The moose likes Ruby and the fly likes Calling external programs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Ruby
Bookmark "Calling external programs" Watch "Calling external programs" New topic
Author

Calling external programs

Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
I am writing a series of scripts that run and use output from a variety of network tools. What tools and what arguments will be determined at runtime, and these arguments can get complex. For example running nmap -PA -sV -O -T4 -p80,443,8080,8443,9000-10000 <target>. If the target and arguments are know before running it, it is simple, either use `...` or %x{...}, but this method seems to not take string parameters only literals, using system makes it impossible to capture output which is what I need to do. I am using open3 which works but doesn't seem to be ideal. I would like to have to be free from constructing the complete command line string, but this is the only thing that seems to work.

If I try:
inp,out,err = popen3(@tool, @args)
inp.puts @target
inp.close
It works with one argument, but not multiple. This would be perfect because I will likely have a list of targets, but arguments may change based on other runtime criteria. The problem is that this only works with 1 argument, passing in multiple arguments as either a single string or many arguments results in failure: either a broken pipe or nmap can't see the target. So my solution is to build a single string and pass that:

inp,out,err = popen3(@tool)
inp.close

Where @tool is something like 'nmap -PA -sV -O -T4 -p80,443,8080,8443,9000-10000 <target>'. Is there a better way to accomplish this? This works but having to build a lot strings during runtime is going to slow things down considerably as I may have hundreds of targets and a dozen or more tools to run and parse data from.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

"Rusty Shackeford", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
You can use #{} for string interpolation within %x[] statements like this:


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling external programs