Interacting With A Terminal App

T

tpurl

Hi, I've searched for hours on how to do this to no avail. Any help I
can get would be greatly appreciated!

I would like to interact with a console app that expects you to log in,
send it commands, and then log out. A typical session might look like
this:

$ ./myapp -u user -p pass
myapp> setval 10
myapp> getval
(prints 10)
myapp> exit
$

I've heard that you can do something like this with the expect module,
but I would rather not have to install it on the machine with which I'm
working. I tried something like this:

my $myapp = "/usr/bin/myapp -u user -p pass";
open(MYAPP, "|$myapp") or die "Cannot execute $myapp: $!";
print MYAPP "setval 10";
print MYAPP "getval";
print MYAPP "exit";
my @lines = <MYAPP>; # read the value returned from the getval command
close(MYAPP);

However, this just blows up in my face. Does anyone point me in the
right direction on this one?

Thanks again!

Tom Purl
 
J

J. Gleixner

tpurl said:
Hi, I've searched for hours on how to do this to no avail. Any help I
can get would be greatly appreciated!

I would like to interact with a console app that expects you to log in,
send it commands, and then log out. A typical session might look like
I've heard that you can do something like this with the expect module,
but I would rather not have to install it on the machine with which I'm
working.

Why not? You'd be finished by now. You can simply install it in your
directory:

perldoc -q "How do I keep my own module/library directory"
my $myapp = "/usr/bin/myapp -u user -p pass";
open(MYAPP, "|$myapp") or die "Cannot execute $myapp: $!";
print MYAPP "setval 10";
print MYAPP "getval";
print MYAPP "exit";
my @lines = <MYAPP>; # read the value returned from the getval command
close(MYAPP);

However, this just blows up in my face. Does anyone point me in the
right direction on this one?

Expect is the best option. If you want to get your hands dirty,
perldoc IPC::Open2.
 
T

Tad McClellan

tpurl said:
I've searched for hours on how to do this to no avail.


Did you search the Perl Frequently Asked Questions?

I would like to interact with a console app that expects you to log in,
send it commands, and then log out.


perldoc -q expect

How can I write expect in Perl?

I've heard that you can do something like this with the expect module,

Oh.


but I would rather not have to install it on the machine with which I'm
working.


Why would you rather not have to install it on the machine with which
you're working?

open(MYAPP, "|$myapp") or die "Cannot execute $myapp: $!";
my @lines = <MYAPP>; # read the value returned from the getval command
However, this just blows up in my face. Does anyone point me in the
right direction on this one?


perldoc -q pipe

How can I open a pipe both to and from a command?


But that requires another module that you would probably
rather not install...
 
T

tpurl

Thanks a lot for the help guys! "Administrative constraints" are
keeping me from installing the Expect module on the servers with which
I'm working. Thanks for pointing me in the direction of the pipe
module.
 
A

A. Sinan Unur

Thanks a lot for the help guys! "Administrative constraints" are
keeping me from installing the Expect module on the servers with which
I'm working. Thanks for pointing me in the direction of the pipe
module.

You should quote some context when you reply.

Have you read:

Why not? You'd be finished by now. You can simply install it in your
directory:

perldoc -q "How do I keep my own module/library directory"

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top