Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: User interaction mid script again
by Roger (Parson) on Dec 04, 2003 at 11:11 UTC
    Why do you post the same question again? Patience my friend, patience. If I were you I would rather go to CPAN or 'google' first to look for documentations/examples/demos than waiting for other monks to respond.

    Here's the example from CPAN - How to automate login.
      Hi Roger
      Thanks for answering. I sent the message a number of times as it was not answered and a huge number of mails came in in the intirim causing my mail to be buried. This lessens the chance of me getting an answer.
      The problem I have is that I don't want my password hardcoded anywhere in my scripts (for security). I want to telnet into the host and cd to a directory. So in essence I need to be able to interact with the script mid-script to send in my password and then return the script to doing its business

      Here is how it can be done in tcl Expect:
      #!/usr/dist/exe/expect -f set timeout -1 set hostname blah set login_name blah_blah spawn $env(SHELL) match_max 100000 send -- "telnet $hostname\r" expect "login:" send -- "$login_name\r" expect "Password:" interact -nobuffer -re "(.*)\r" return #ask user for password and then return to script send -- "cd $dir\r" interact
      Now I am trying to translate this into perl (as tcl has some pecularities I don't really like. Heres what I came up with:
      #!/usr/bin/perl use strict; use Expect; my $session = new Expect; my $server_connect = "blah"; my $username = "blah_blah"; my $shell_prompt= '(.*%|.*#|.*>|.*\\$) $'; my $dir="blah_blah_blah"; $session->spawn("bash"); print $session "telnet $server_connect\r"; $session->expect(60, -re, "ogin:"); print $session "$username\r"; $session->expect(10, -re, "assword:"); $session->interact(\*STDIN, "\r"); $session->expect(60, -re, "$shell_prompt"); print $session "cd $dir\r"; $session->interact();
      The problem is that the line:
      $session->interact(\*STDIN, "\r");
      does not act in the same way as the line
      interact -nobuffer -re "(.*)\r" return
      does in tcl
      I was wondering if anyone could elaborate why this is so?
      thanks in advance
Re: User interaction mid script again
by rdfield (Priest) on Dec 04, 2003 at 13:24 UTC
    What's wrong with the example in the Expect POD?
    $exp->expect($timeout, [ qr/username: /i, sub { my $self = shift; $self->send("$username\r"); exp_continue; }], [ qr/password: /i, sub { my $self = shift; $self->send("$password\r"); exp_continue; }], $shell_prompt);
    All you need to add before it is something like:
    print "username: "; my $username = <STDIN>; print "password: "; my $password = <STDIN>; chomp $username; chomp $password;
    I've never used Expect, but the POD seems pretty clear.

    rdfield

      my question was to do with the use of the interact command in perl expect. This allows you to interact in real time with the script. I know that I can ask for the variables at the start of the script or even pass them in as parameters but I would like to know why the interact statement that I used does not work as all the documentation on it says it should. Becoming proficient with the expect.pm module means asking questions about some of the apparently unclear aspects of some of its functions
Re: User interaction mid script again
by TomDLux (Vicar) on Dec 04, 2003 at 19:10 UTC

    Depending on what you are doing at the remote site, Expect may not be the best solution. Instead of using telnet/Expect, why not use ssh?

    Rather than having to handle login character by character yourself, you simply invoke ssh using system(), specifying the account and machine names. Assuming you are connecting to the same machines all the time, you can set up configuration files so that the remote sites know which sites will connect to them. You have to follow some slightly complicated procedure, but the instructions are clear enough.

    The result is a configuration where you do not have to specify a password, hence the password does not appear in any script nor in any config file. Since login is based on encryption keys, unauthorized sites are totally blocked. And your code becomes a lot simpler.

    If you are connecting to different machines each time, then you will have to provide a password, somehow. But you still gain benefits, your connection will be encrypted, the password will be passed encrypted, not clear-text, to the remote machine.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      for anybody who's interested I found out how to do it in perl expect.
      $session->interact(\*STDIN, "\r"); print $session "\r";
      is the same as
      interact -nobuffer -re "(.*)\r" return
      in tcl expect