Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hey all I have this really simple expect script I am writing to log into a L4 switch and switch a server into or out of production and for some reason after I send the first command to the session my next regex doesn't match. Here is the code I have thus far:

This is the expected second prompt:

>> Real server 144 -

#!/usr/bin/perl -w # Script using Expect to roll servers in PHX in # the pop in and out of the VIP use strict; use warnings; use Expect; # Setup our reusable varibles here ( host password etc ) my $telnet = '/bin/telnet'; my $host = "myhost.net"; my $passwd = "mypass"; my $timeout = '10'; my $infocmd = '/info/slb/dump'; my $rcmd = '/cfg/slb/real'; my $apply = 'apply'; my $save = 'save'; my $yes = 'y'; my $no = 'n'; my $exit = 'exit'; my $serverL = '/var/tmp/server_list_vip'; # interact a bit till I get the CGI wrapper figured out print "Welcome to the PHX vip roll-in/out script\n I will show you a list of servers you can roll in and out of th +e vip\n choose the correct number for the given server\n EXAMPLE: If you want to roll newsfe01 in/out in the list you wi +ll see\n 144: newsfe01, so the correct number is 144\n"; open(IN,"$serverL"); chomp(my @list = <IN>); close(IN); foreach my $line(@list) { print "$line\n"; } print "Choose the server number now:\n"; chomp(my $number = <STDIN>); print "What action do you want to take?\n Roll server into production = ena\n Roll server out of production = dis\n"; chomp(my $action = <STDIN>); my $exp = Expect->spawn("$telnet $host") or die "Cannot spawn telnet to $host: $!\n";; my $spawn_ok; if($action =~ m/dis/) { my $func = 'dis'; $exp->expect($timeout, [qr'Enter password: $', sub{ $spawn_ok = 1; my $fh = shift; print $fh "$passwd\r"; #$fh->send("$passwd\n"); exp_continue;} ], [qr'>> Main- $', sub{ $spawn_ok = 1; my $fh = shift; print $fh "$rcmd $number/$func\r"; sleep 2; exp_continue; }],
This is where the interactions fails at
[qr'>> Real server $number - $', sub{ $spawn_ok = 1; my $fh = shift; print $fh "$apply\r"; sleep 2; exp_continue; }], [qr'>> Real server $number - $', sub{ $spawn_ok = 1; my $fh = shift; print $fh "$save\r"; sleep 2; exp_continue; }], [qr'Confirm saving to FLASH [y/n]: $', sub{ $spawn_ok = 1; my $fh = shift; print $fh "$yes\r"; sleep 2; exp_continue; }], [qr'>> Real server $number - $', sub{ $spawn_ok = 1; my $fh = shift; print $fh "$exit\r"; exp_continue; }] ); }

If anyone has a clue here please share as I am pretty lost at this point.

Thanks all
SUNADMN
USE PERL

In reply to Expect problems by sunadmn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-25 08:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found