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!

running external programs - very simple help needed

Status
Not open for further replies.

twintriode

Technical User
Jan 6, 2005
21
AU
Hello all,

I have been looking for the last hour or two on information on how to pass commands onto a normal program that would normally take input from a bash command line.



How can I run the program from perl and enter the predefined info into the input fields automatically.

I have tried:

#!/usr/bin/perl -w

print "doing it now\n";
open(MAIL, "| /sbin/nokia_ctl gsm sendSMS");
print MAIL "Heres some email for you\n";
print MAIL "+61411666888\n";
print MAIL "+6421600601\n";
close(MAIL);
print "DONE\n";

but this does not seem to work. It just prints the standard output from nokia_ctl but does not pass any input to the program.

I just get:

# ./test1
doing it now
Please type the message (max 160 chars)
Please type the receiver number
Please type the SMS center number
DONE


Sorry if I have not made myself clear but it is 03:54 and I am a little tired.

Thanks in advance
 
What does the interaction with '/sbin/nokia_ctl' look like? Is it similar to the perl script you're writing? Does it prompt for a message, then the receiver number, and finally the SMS center number? If so, you may want to give expect a shot - a couple expect modules for Perl are Expect and Expect::Simple.

Or, if you happen to have the Tcl Expect already installed on your system, your perl script could write, then execute an expect script.
 
Hi - thanks for the reply.

The normal interaction from a bash shell would look like this:

#nokia_ctl gsm sendSMS
Please type the message (max 160 chars)
Hello this is the message - bye.
Please type the receiver number
+61411666888
Please type the SMS center number
+6421600601
D211: Command succeeded
#

I am having a bit of a look at the Expect module, looks like it will do what I need it to, but I am finding it a little hard to understand. Looking into it now. Cannot seem to find any simple examples.

What about Socket::IO would this be able to talk to command line programs?

Thanks
 
I would imagine Socket::IO would not help you out in this situation, but I could be wrong.

I can't test the Expect module since I'm on a WinXP box at the moment (I'm not much help today, it seems), but I'd imagine the code would go something like:

Code:
use Expect;
my $timeout = 30;
my $message = "Here's some mail for you.";
my $targetNum = '+61411666888';
my $smsNum = '+6421600601'

my $exp = Expect->spawn('nokia_ctl gsm sendSMS') or die "Could not spawn command\n$!\n";
$exp->expect($timeout, 'Please type the message');
$exp->send("$message\n");
$exp->expect($timeout, 'Please type the receiver number');
$exp->send("$targetNum\n");
$exp->expect($timeout, 'Please type the SMS center number');
$exp->send("$smsNum\n");
$exp->expect($timeout, 'Command succeeded');
$exp->soft_close();

print "Message Sent\n";
 
Hi Thanks again for your message. I solved the problem just as I received your last message.

Thanks anyway was having real problems searching for a solution to 'running external programs in perl' in google, just kept giving me perl command line option tutes etc...

Ended up having a close look at the Expect module manual and came up with the following that seems to work:

#!/usr/bin/perl

use Expect;
$comand = "/sbin/nokia_ctl gsm sendSMS";
my $exp = Expect->spawn("/sbin/nokia_ctl gsm sendSMS")
or die "Cannot spawn $command: $!\n";

$exp->expect(50, 'Please type the message');
print $exp "hello this is the message -bye.\n";

$exp->expect(50, 'Please type the');
print $exp "+61411666888\n";

$exp->expect(50, 'Please type the');
print $exp "+6421600601\n";

$exp->expect(10, 'D211');
print "Excellent it works\n";

$exp->soft_close();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top