perluday has asked for the wisdom of the Perl Monks concerning the following question:
Hi perl experts, im pretty new to perl expect and passing commands to client server and printing execution status in terminal. If you look at my output, I am executing “cat /etc/redhat-release” but actual execution happens at the end of code and prints the output at the end of execution. I would like to get actual execution of “cat /etc/redhat-release” output displayed in terminal after “TERMINAL: cat /etc/redhat-release” output. Could you please help me to fix ASAP?
<input file> INPUT FILE: [root@server-1 ~]# cat "cs3.csv" 192.18.16.19,root,conley,cat /etc/redhat-release,ls,date [root@server-1 ~]# </input file> <output> OUTPUT: [root@server-1 ~]# perl temp4.pl Testcase no:1 ++++++++++++++++++++++++++++ Test case details: 192.18.16.19,root,conley,cat /etc/redhat-release,ls +,date Logged into 192.18.16.19 root@192.18.16.19's password: Last login: Sat Sep 14 02:31:25 2013 from server-1.lss.emc.com TERMINAL: cat /etc/redhat-release TERMINAL: ls TERMINAL: date [root@client197 ~]# [root@client197 ~]# [root@client197 ~]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 5.9 (Tikanga) [root@client197 ~]# [root@client197 ~]# ls anaconda-ks.cfg install.log t +est.txt Desktop install.log.syslog LINUX-5.7.1.00.00-029.RHEL5.x86_64.rpm SSL [root@client197 ~]# [root@client197 ~]# date Sun Sep 15 04:23:42 EDT 2013 [root@client197 ~]# exit logout Connection to 192.18.16.19 closed. </output>
ACTUAL CODE: [root@server-1 ~]# cat /perlupdate/test/csv/temp4.pl #!/usr/bin/perl use Expect; use warnings; #use strict; $timestamp = getLoggingTime(); $l_file = "/perlupdate/test/csv/log/$timestamp.log"; # Use the open() function to create the file. unless(open FILE, '>' .$l_file) { # Die with error message # if we can't open it. die "\nUnable to create $l_file\n"; } close (FILE); my $file = "cs3.csv"; open(my $data, '<', $file) or die "Could not open '$file' $!\n"; my $lnnum = 0; while (my $line = <$data>) { $exp=new Expect; $exp->send("\r"); $lnnum++; print "\n"; print "Testcase no:$lnnum \n"; print "++++++++++++++++++++++++++++ \n"; print "\n"; print "Test case details: $line\n"; print "\n"; chomp $line; my @fields = split "," , $line; $exp-> raw_pty(1); #eliminates echo back to expect $exp-> log_file("$l_file"); $exp-> debug(0); $exp->spawn("ssh $fields[0] -l $fields[1]") or die "Cannot connect $fi +elds[0]: $!\n"; print"Logged into $fields[0]\n"; $exp->expect(10, "password"); $exp->send("$fields[2]\r"); $exp->expect(30, "Last"); $exp->send("\r"); $i=3; while($i <= $#fields) { print "\n"; print"TERMINAL: $fields[$i]\n"; $exp->send("\r"); $exp->send("$fields[$i]\r"); print "\n"; $i++; } $exp->send("exit\r"); #$exp->send("\r") $exp->soft_close(); } sub getLoggingTime { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime +(time); my $nice_timestamp = sprintf ( "%04d_%02d_%02d_%02d_%02d_%02d", $year+1900,$mon+1,$mday,$hour,$min, +$sec); return $nice_timestamp; } [root@server-1 ~]#
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: perl expect
by salva (Canon) on Sep 16, 2013 at 07:38 UTC | |
|
Re: perl expect
by Laurent_R (Canon) on Sep 15, 2013 at 21:26 UTC | |
|
Re: perl expect
by Laurent_R (Canon) on Sep 16, 2013 at 06:16 UTC |