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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ask for an efficient way to use expect.pm 1

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
US
Dear Experts,

I need to use expect.pm at work. I am wondering if there is a way to log onto a remote host once and execute multiple cmds?

According to the expect.pm's doc:

Also note that you cannot reuse an object with an already spawned command, even if that command has exited. Sorry, but you have to allocate a new object...

Right now, what I am doing is like this:

Create a new object, logon the remote host, execute ONE command, exit.

Create another new object, logon the remote host, execute the 2nd command, exit.

And so on, so forth, which is really tiresome.

There must be a more efficient way to do this kind task. Could some expert show me some sample codes?

Many thanks.
 
I did something similar in the past and it worked fine.. let's say you have all your servers (hostnames) in one array:

Code:
use Expect;
$PASSWD = "PASSWORD";
$PROMPT1="OLD PASSWORD:";
$PROMPT2="NEW PASSWORD:";
$PROMPT3="VERIFICATION:";


foreach  (@ALLSERVERS) {    
    accesing_ne();
    sleep 10;
      
}



sub accesing_ne {
	
    print "Transmiting to $_[0]\n";
    
    my $network_element = Expect->spawn("c7xtermx -n $_[0] 2>/dev/null");
    unless (defined $network_element) {
	    warn "Could not spawn network_element: $!\n";
	    return undef;
    }
    
    #$network_element->log_stdout(0);
    $network_element->stty(qw(echo));
    
    my $answer = $exp->expect(30, '<');    
    $network_element->send("ZIAG;");
    
    $answer = $exp->expect(10, $PROMPT1);
    $network_element->send($PASSWD);
    
    $answer = $exp->expect(10, $PROMPT2);
    $network_element->send($PASSWD);
    
    $answer = $exp->expect(10, $PROMPT3);
    $network_element->send($PASSWD);

}

dmazzini
GSM/UMTS System and Telecomm Consultant

 
From CPAN

Code:
#
How to expect on multiple spawned commands
  foreach my $cmd (@list_of_commands) {
    push @commands, Expect->spawn($cmd);
  }

  expect($timeout,
         '-i', \@commands,
         [
          qr"pattern",          # find this pattern in output of all commands
          sub {
            my $obj = shift;    # object that matched
            print $obj "something\n";
            exp_continue;       # we don't want to terminate the expect call
          }
         ],
         '-i', $some_other_command,
         [
          "some other pattern",
          sub {
            my ($obj, $parmref) = @_;
            # ...

            # now we exit the expect command
          },
          \$parm
         ],
        );

dmazzini
GSM/UMTS System and Telecomm Consultant

 
Excellent!! Thank you, dmazzini!!

Now I have another question. But I am not sure if it's related. So I'll start another new thread shortly. I appreciate if if you could take a look.

Thanks again.
 
From CPAN

Code:
#
How to expect on multiple spawned commands
  foreach my $cmd (@list_of_commands) {
    push @commands, Expect->spawn($cmd);
  }

  expect($timeout,
         '-i', \@commands,
         [
          qr"pattern",          # find this pattern in output of all commands
          sub {
            my $obj = shift;    # object that matched
            print $obj "something\n";
            exp_continue;       # we don't want to terminate the expect call
          }
         ],
         '-i', $some_other_command,
         [
          "some other pattern",
          sub {
            my ($obj, $parmref) = @_;
            # ...

            # now we exit the expect command
          },
          \$parm
         ],
        );

dmazzini
GSM/UMTS System and Telecomm Consultant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top