How to pass input from perl to shell programming?

S

Swamy

I have a perl program which executes a shell program using system
call. This shell program needs user input for several questions it
asks. How can I automate the user input so that user don't have to key
in the input?

thanks
Swamy
 
S

Sherm Pendley

Swamy said:
I have a perl program which executes a shell program using system
call. This shell program needs user input for several questions it
asks. How can I automate the user input so that user don't have to key
in the input?

Have a look at the Expect module on CPAN.

sherm--
 
B

Brian McCauley

I have a perl program which executes a shell program using system
call. This shell program needs user input for several questions it
asks. How can I automate the user input so that user don't have to key
in the input?

I Expect there's something on CPAN.
^^^^^^

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
G

Gregory Toomey

Swamy said:
I have a perl program which executes a shell program using system
call. This shell program needs user input for several questions it
asks. How can I automate the user input so that user don't have to key
in the input?

thanks
Swamy

The easiest way MAY be:

open(FH, "| command.sh") || die "can't run command.sh: $!";
print FH "stuff\n";
close(FH)


Otherwise use expect.

gtoomey
 
J

J Krugman

In said:
Swamy wrote:
Have a look at the Expect module on CPAN.

Expect.pm may be great if you, unlike me, have prior experience
using /usr/bin/expect. Otherwise, I found it to be rough going,
and not worth the aggravation. The documentation didn't make much
sense and the examples given in the tutorial did not work (as
acknowledged in the Expect.pm FAQ).

Does anyone know where one can find an extended example using
Expect.pm?

jill
 
T

Tassilo v. Parseval

Also sprach J Krugman:
Expect.pm may be great if you, unlike me, have prior experience
using /usr/bin/expect. Otherwise, I found it to be rough going,
and not worth the aggravation. The documentation didn't make much
sense and the examples given in the tutorial did not work (as
acknowledged in the Expect.pm FAQ).

The secret about Expect seems to be to know which of the many methods
one will most likely never need. So far I always got away with using
send() and expect().

I am using the following script to refresh the password of one of my ssh
accounts (policy requires to change it every six weeks). Since I cannot
be bothered to come up and remember new passwords all the time, I used
Expect to log in and cycle the password a bit until the original
password is set again:

my $exp = Expect->spawn("ssh", qw(-l tasvon cagney.informatik.rwth-aachen.de))
or die "Cannot spawn process: $!\n";

$exp->expect(5, 'password:');
$exp->send("origpwd\n");
$exp->expect(5, 'tasvon@cagney:~\$/');

my @pwd = qw(origpwd abc123 bcd234 cde456 origpwd);

for (0 .. $#pwd-1) {
$exp->send("passwd\n");
$exp->expect(5, 'Geben Sie ihr altes Kennwort ein:');
$exp->send("$pwd[$_]\n");
$exp->expect(5, 'Geben Sie ihr neues Kennwort ein:');
$exp->send("$pwd[$_+1]\n");
$exp->expect(5, 'Wiederholen Sie ihr neues Kennwort:');
$exp->send("$pwd[$_+1]\n");
!defined($exp->expect(10, 'Ihr Kennwort wird geaendert.')) and die "failed\n";
}

$exp->send("exit\n");
$exp->expect(5, ".");

# the second exit because the first one will just exit the bash and
# return to csh
$exp->send("exit\n");

The above lacks error checking as it assumes that each call to expect()
will succeed. Also, it doesn't need to make use of expect()'s more
sophisticated features such as providing a list of regular expressions
and checking its return value to figure out which one matched.

Tassilo
 
J

J Krugman

In said:
I am using the following script to refresh the password of one of my ssh
accounts (policy requires to change it every six weeks). Since I cannot
be bothered to come up and remember new passwords all the time, I used
Expect to log in and cycle the password a bit until the original
password is set again:
my $exp = Expect->spawn("ssh", qw(-l tasvon cagney.informatik.rwth-aachen.de))
or die "Cannot spawn process: $!\n";
$exp->expect(5, 'password:');
$exp->send("origpwd\n");
$exp->expect(5, 'tasvon@cagney:~\$/');
my @pwd = qw(origpwd abc123 bcd234 cde456 origpwd);
for (0 .. $#pwd-1) {
$exp->send("passwd\n");
$exp->expect(5, 'Geben Sie ihr altes Kennwort ein:');
$exp->send("$pwd[$_]\n");
$exp->expect(5, 'Geben Sie ihr neues Kennwort ein:');
$exp->send("$pwd[$_+1]\n");
$exp->expect(5, 'Wiederholen Sie ihr neues Kennwort:');
$exp->send("$pwd[$_+1]\n");
!defined($exp->expect(10, 'Ihr Kennwort wird geaendert.')) and die "failed\n";
}
$exp->send("exit\n");
$exp->expect(5, ".");

# the second exit because the first one will just exit the bash and
# return to csh
$exp->send("exit\n");

Hey, that's a nice little script. Didactive for TWO languages,
Perl and German.

I'll give Expect another go.

Thanks!

jill
....so password is "Kennwort". Who knew?
 
S

Swamy

I have a perl program which executes a shell program using system
call. This shell program needs user input for several questions it
asks. How can I automate the user input so that user don't have to key
in the input?

thanks
Swamy

Thanks a lot for your advice. I found IO->React more useful which is
docmented in CPAN.

regards
swamy
 
S

Swamy

Thanks a lot for your advice. I found IO->React more useful which is
docmented in CPAN.

regards
swamy

I tried perl expect also, which seems to be more useful. I am trying
to start a ssh session, login, and spawn another program(installation
program). It has "more based text", and some user input. I could not
grab control of the program nor send or expect anything. It just stays
at first "--More10%" prompt.

Can you please tell me how to do it?

thanks
Swamy
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top