Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling external python program with argument

Status
Not open for further replies.

wsnsecurity

Programmer
Aug 19, 2008
3
US
Hi there,

I am trying to call a python program that requires an argument at prompt within a perl script. Below is a few combinations that I have tried. It would call the program fine without the argument, but it wouldn't take the argument. The python program itself works fine. Could someone help with it? Thanks in advance.

YanYan

#!/usr/local/bin/perl

$file = 'host-list.txt'; # Name the file
open(INFO, $file); # Open the file
@DNSSer = <INFO>; # Read it into an array
close(INFO); # Close the file

$index = 0;
foreach $morsel (@DNSSer){
#@args = ("python dnspredict.py", @DNSSer[$index++]);
#print @args;
#system(@args) == 0
#or die "system @args failed: $?"
#system "python dnspredict.py", "@DNSSer[$index++]";
#exec "python dnspredict.py < @DNSSer[$index++]";
$SerIP = @DNSSer[$index++];
#print $SerIP;
exec "python dnspredict.py", "$SerIP";
}
 
If it is prompting for more input you need to look at using expect.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
1 - Do you know how dnspredict.py is asking for the input? (i.e. STDIN or ARGS) Can you supply a screen shot of it running (may help answer the previous question)

2 - If dnspredict.py is looking for input from ARGS, the (commented out) method should have worked. (see #4 below)

3 - If dnspredict.py is looking for input from STDIN, you may be at a roadblock. I've tried the following from a WinXX prompt
Code:
echo 'test' | redir_stdin.pl
redir_stdin.pl <test_file.txt
where
Code:
# redir_stdin.pl
$does_this_work = <STDIN> ;
print ">>$does_this_work<<\n";
The redir_stdin.pl prog works ok directly at the command prompt, but I'm not able to figure out how to redirect STDIN. If you wrote dnspredict.py, have it look to ARGS first before going to STDIN.

4 - FWIW: Your foreach loop is working too hard. Consider
Code:
foreach $morsel (@DNSSer){
   #@args = ("python dnspredict.py", $morsel);
}
 
I think it's asking ARGS, because I could call the python program with an argument from command prompt. It worked when I put all input in as one string, rather than separate the two parameters. I've changed the foreach loop, it looks much better than what I had. Thanks a lot Travis and PinkeyiNBrain.
 
Hi,

Have you taken a look at the Perl expect module?


It allows you to script a normally interactive process.

It looks like it can be used like something like this:

my $oExp = new Expect;

$oExp->spawn("python dnspredict.py");

$oExp->raw_pty(1);

$oExp->(120, '-re', "some prompt");

$oExp->send($SerIP);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top