How to pass input from perl to shell programming?

Discussion in 'Perl Misc' started by Swamy, May 21, 2004.

  1. Swamy

    Swamy Guest

    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
     
    Swamy, May 21, 2004
    #1
    1. Advertising

  2. Swamy wrote:

    > 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--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, May 21, 2004
    #2
    1. Advertising

  3. (Swamy) writes:

    > 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\\
     
    Brian McCauley, May 21, 2004
    #3
  4. Swamy wrote:

    > 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
     
    Gregory Toomey, May 22, 2004
    #4
  5. Swamy

    J Krugman Guest

    In <> Sherm Pendley <> writes:

    >Swamy wrote:


    >> 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.


    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


    --
    To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.
     
    J Krugman, May 22, 2004
    #5
  6. Also sprach J Krugman:

    > In <> Sherm Pendley <> writes:
    >
    >>Swamy wrote:

    >
    >>> 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.

    >
    > 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
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, May 22, 2004
    #6
  7. Swamy

    J Krugman Guest

    In <> "Tassilo v. Parseval" <> writes:

    >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?


    --
    To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.
     
    J Krugman, May 23, 2004
    #7
  8. Swamy

    Anno Siegel Guest

    J Krugman <> wrote in comp.lang.perl.misc:


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


    ....also "Passwort" (guessable) and "Zugangsberechtigung" (best spelled
    in gothic letters).

    Anno
     
    Anno Siegel, May 23, 2004
    #8
  9. Swamy

    Swamy Guest

    (Swamy) wrote in message news:<>...
    > 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
     
    Swamy, May 24, 2004
    #9
  10. Swamy

    Swamy Guest

    (Swamy) wrote in message news:<>...
    > (Swamy) wrote in message news:<>...
    > > 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


    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
     
    Swamy, May 25, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Heimes
    Replies:
    0
    Views:
    619
    Christian Heimes
    Feb 27, 2008
  2. Gerardo Herzig
    Replies:
    1
    Views:
    1,126
    Philipp Pagel
    Feb 27, 2008
  3. D'Arcy J.M. Cain
    Replies:
    0
    Views:
    886
    D'Arcy J.M. Cain
    Feb 27, 2008
  4. Tricky
    Replies:
    0
    Views:
    598
    Tricky
    Mar 19, 2009
  5. sitnam81
    Replies:
    11
    Views:
    484
    Tad McClellan
    Apr 29, 2005
Loading...

Share This Page