• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Calling external programs

 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67390
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Rusty Shackeford", please check your private messages for an important administrative matter.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use #{} for string interpolation within %x[] statements like this:
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic