I am new to perl and don;t have much expertise. I was trying to get the below mentioned script working but it always throw following error when running from the windows using Cygwin
"admin@cisco-hbi76wq1f /Expect/Login/Login $ perl Postchange.pl Telnet to 10.106.97.194 No password prompt (that we recognize anyways for 10.106.97.194. at Po +stchange.pl line 257, <FH> line 1. "
The same script when executed from Linux (ubuntu) works fine without any issues.
Below is the script :Thanks in Advance /Adi#!/usr/bin/perl use strict; use warnings; use lib "./Expect-1.21/blib/lib"; use Expect; my $device = "0.0.0.0"; if ( -e "PostOutput.txt" ) #If PostOutputfile already exists will be r +emoved. { `rm PostOutput.txt`; } my $timeout = 15; #Number of seconds to wait aftre each command is exe +cuted.Can be increased or decreased. my $user = "cisco"; #Initialize username. my $pass = "cisco"; #Initialize password. if ( open ( FH, "GatewayList.txt" ) ) #This is the input file with IP +address in column1 and SSHorTelnet in column2. { while ( defined ( my $line = <FH> ) ) { $line =~ s/\s+$//; my @line_split = split(" ", $line); $device = $line_split[0]; if ( $line_split[1] eq "SSH" ) { &checkssh; } else { &checktelnet; } } close FH; } else { print "GatewayList.txt file not found\n"; } exit; sub checkssh { #SSH procedure. my @out = (); my @out1 = (); my @out2 = (); open FILE, ">>", "PostOutput_".$device.".txt" or die $!; print "SSH to $device\n"; my $check1 = Expect->spawn("ssh -o StrictHostKeyChecking=no -o + UserKnownHostsFile=/dev/null $user\@$device"); $check1->log_stdout(0); if ($check1->expect($timeout, -re => "Are you sure you want to co +ntinue connecting (yes/no)?") ) { print $check1 "yes\r"; } $check1->expect($timeout, -re => "password:|Password:"); print $check1 "$pass\r"; $check1->expect($timeout, -re => ">"); print $check1 "enable\r"; #If enable password is different th +an the regular password initialize as a seperate variable in the begi +nning. #$check1->expect($timeout, -re => "password:|Password:"); #print $check1 "$pass\r"; #If enable password is different th +an the regular password use enable password variable. if ( $check1->expect($timeout, -re => "#")) { #Specify all device commands in this section. print $check1 "term len 0\r"; #Command to be executed. $check1->expect($timeout, -re => "#"); #Return prompt. #@out = split(/\r/, $check1->before()); #@out2 = split(/\r/, $check1->after()); $check1->clear_accum(); #@out2 = (); #@out = (); #print $check1 "sh flash:\r"; print $check1 "sh flash:\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); #Command PostOutput wil +l be stored in the array variable. @out2 = split(/\r/, $check1->after()); #$file_handle->print($check1 . "\n"); print FILE @out;#`echo "@out" >> H323Log.txt`; #Data stored in + the array variable will be written to the PostOutput file. print FILE @out2; @out = (); #This will empty the array contents so that the nex +t command PostOutput can be stored in the same variable. @out2 = (); $check1->clear_accum(); #This will clear the previous command +PostOutput so that the next command PostOutput can be loaded to the a +rray variable. print $check1 "sh ver\r"; $check1->expect($timeout, -re => "#"); @out1 = split(/\r/, $check1->before()); @out2 = split(/\r/, $check1->after()); #`echo "@out1" >> H323Log.txt`; print FILE @out1; print FILE @out2; @out1 = (); @out2 = (); @out = (); $check1->clear_accum(); print $check1 "sh dspfarm all\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); @out2 = (); $check1->clear_accum(); print $check1 "sh voice dsp group all\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); @out2 = (); $check1->clear_accum(); print $check1 "sh ip int bri\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); @out2 = (); $check1->clear_accum(); print $check1 "sh isdn status\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); @out2 = (); $check1->clear_accum(); print $check1 "sh isdn service\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); @out2 = (); $check1->clear_accum(); print $check1 "sh controller t1\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); @out2 = (); $check1->clear_accum(); print $check1 "sh gateway\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); @out2 = (); $check1->clear_accum(); print $check1 "sh ephone registered \r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); @out2 = (); $check1->clear_accum(); print $check1 "sh run \r"; $check1->expect($timeout, -re => "#"); @out1 = split(/\r/, $check1->before()); @out2 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out1; print FILE @out2; @out = (); @out1 = (); @out2 = (); $check1->clear_accum(); print $check1 "sh status \r"; $check1->expect($timeout, -re => "#"); @out1 = split(/\r/, $check1->before()); @out2 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out1; print FILE @out2; @out = (); @out1 = (); @out2 = (); $check1->clear_accum(); print $check1 "sh status \r"; $check1->expect($timeout, -re => "#"); @out1 = split(/\r/, $check1->before()); @out2 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out1; print FILE @out2; @out = (); @out1 = (); @out2 = (); $check1->clear_accum(); #print $check1 "wr\r"; #Use this command only if configuration + needs to be written to memory. #$check1->expect($timeout, -re => "#"); `echo "$device - Success(SSH)" >> PostOutput.txt`; print $check1 "quit\r"; #This will exit the SSH session. $check1->soft_close; } else { `echo "$device - Unable to SSH" >> PostOutput.txt`; return; } } sub checktelnet { #Telnet Procedure.All SSH comments applies for teln +et too. my @out = (); my @out1 = (); my @out2 = (); open FILE, ">>", "PostOutput_".$device.".txt" or die $!; print "Telnet to $device\n"; my $check1 = Expect->spawn("telnet $device"); $check1->log_stdout(0); if ( $check1->expect($timeout, -re => "username|Username") ) { print $check1 "$user\r"; } $check1->expect($timeout, -re => "password|Password") or die ( +"No password prompt (that we recognize anyways for $device."); print $check1 "$pass\r"; if ( $check1->expect($timeout, -re => "#")) { print $check1 "term len 0\r"; #Command to be executed. $check1->expect($timeout, -re => "#"); #Return prompt. #@out = split(/\r/, $check1->before()); #@out2 = split(/\r/, $check1->after()); $check1->clear_accum(); #@out2 = (); #@out = (); #print $check1 "sh flash:\r"; print $check1 "sh flash:\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); #Command PostOutput wil +l be stored in the array variable. @out2 = split(/\r/, $check1->after()); #$file_handle->print($check1 . "\n"); print FILE @out;#`echo "@out" >> H323Log.txt`; #Data stored in + the array variable will be written to the PostOutput file. print FILE @out2; @out = (); #This will empty the array contents so that the nex +t command PostOutput can be stored in the same variable. @out2 = (); $check1->clear_accum(); #This will clear the previous command +PostOutput so that the next command PostOutput can be loaded to the a +rray variable. print $check1 "sh ver\r"; $check1->expect($timeout, -re => "#"); @out1 = split(/\r/, $check1->before()); @out2 = split(/\r/, $check1->after()); #`echo "@out1" >> H323Log.txt`; print FILE @out1; print FILE @out2; @out1 = (); @out2 = (); $check1->clear_accum(); print $check1 "sh dspfarm all\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); $check1->clear_accum(); print $check1 "sh voice dsp group all\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); $check1->clear_accum(); print $check1 "sh ip int bri\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); $check1->clear_accum(); print $check1 "sh isdn status\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); $check1->clear_accum(); print $check1 "sh isdn service\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); $check1->clear_accum(); print $check1 "sh controller t1\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); $check1->clear_accum(); print $check1 "sh gateway\r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); $check1->clear_accum(); print $check1 "sh ephone registered \r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); $check1->clear_accum(); print $check1 "sh run \r"; $check1->expect($timeout, -re => "#"); @out = split(/\r/, $check1->before()); @out1 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out; print FILE @out1; @out = (); @out1 = (); $check1->clear_accum(); print $check1 "sh status \r"; $check1->expect($timeout, -re => "#"); @out1 = split(/\r/, $check1->before()); @out2 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out1; print FILE @out2; @out = (); @out1 = (); @out2 = (); $check1->clear_accum(); print $check1 "sh status \r"; $check1->expect($timeout, -re => "#"); @out1 = split(/\r/, $check1->before()); @out2 = split(/\r/, $check1->after()); #`echo "@out" >> H323Log.txt`; print FILE @out1; print FILE @out2; @out = (); @out1 = (); @out2 = (); $check1->clear_accum(); `echo "$device - Success(Telnet)" >> PostOutput.txt`; print $check1 "quit\r"; #This will exit the SSH session. $check1->soft_close; } else { `echo "$device - Unable to Telnet" >> PostOutput.txt`; return; } }
In reply to Perl with Expect Module not working with Cygwin by adi001
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |