From: Brett M. <bre...@qa...> - 2003-10-28 01:05:28
|
Folks, I am new to expectperl and am having some conceptual troubles (I think). I am using ssh to issue a command to multiple hosts read from a text file. I have kludged together some expect code to handle some responses I may get to the ssh command. I would like to make my code a little neater and be able to trap the condition where the host does not respond at all to the command (ie it is powered off at the time). Therefore I have the following questions (to be followed by a section of my code to illustrate my problem). I really hate the whole $skiphost thing and would really like to be able to just issue a next; for the conditions which require no further interaction with that particular host (thus returning to the top of my while loop). This however does not seem to work. If I do this, the expect call is not broken and continues until timeout and then some other code in the whileloop is eroniously executed. Is there a means of doing this (ie something like exp_next; instead of exp_continue)? How can i differentiate a situation where the host just does not respond? I thought to do this by catching when the expect call timesout but with the current logic it timesout in every condition (hopefully a solution to pont 1 above will also resolve this issue). Kind Regards Brett while (<HOSTS>) { chomp(); @nexthost = split(/,/, $_); # take first field of each line as a valid host (comma seperated data) # Issue the command to the current host $ssh = Expect->spawn("ssh $nexthost[0] $mycommand") or die "Couldn't start program: $!\n"; #$ssh->debug(1); $ssh->log_file("exp.log"); # prevent the program's output from being shown on our STDOUT $ssh->log_stdout(0); $ssh->expect(20, [ qr/continue/i, sub { my $self = shift; $self->send("yes\n"); exp_continue; }], [ qr/denied/i, sub { print "your password for $nexthost[0] was incorrect - please reenter: "; ReadMode 'noecho'; $newpass = <STDIN>; # read the next line chomp($newpass); ReadMode 'normal'; if ($newpass) {$password = $newpass;} $newpass=""; print "\n"; exp_continue; }], [ qr/password:/i, sub { my $self = shift; $self->send("$password\n"); exp_continue; }], [ qr/publickey/i, sub { print "your login to $nexthost[0] failed - maybe you dont have an account there "; $skiphost = "yes"; exp_continue; }], [ qr/not known/i, sub { print "host $nexthost[0] is unknown\n"; $skiphost = "yes"; exp_continue; }], [ qr/closed/i, sub { print "host $nexthost[0] closed the connection - your password may have expired here\n"; $skiphost = "yes"; exp_continue; }], [ qr/connection/i, sub { print "host $nexthost[0] closed the connection\n"; $skiphost = "yes"; exp_continue; }], [ qr/refused/i, sub { print "host $nexthost[0] is not responding on port 22\n"; $skiphost = "yes"; exp_continue; }], ); if ($skiphost) { $skiphost = ""; next; print "you shouldnt see this as it should have skipped"; } # close the expect for ssh $ssh->soft_close(); <some other stuff> <more other stuff> } # loop to next host close HOSTS; *******************Confidentiality and Privilege Notice******************* This email is intended only to be read or used by the addressee. It is confidential and may contain legally privileged information. If you are not the addressee indicated in this message (or responsible for delivery of the message to such person), you may not copy or deliver this message to anyone, and you should destroy this message and kindly notify the sender by reply email. Confidentiality and legal privilege are not waived or lost by reason of mistaken delivery to you. Qantas Airways Limited ABN 16 009 661 901 Visit Qantas online at http://www.qantas.com.au |