From: Robin L. P. <rlp...@di...> - 2013-09-16 03:07:09
|
You really don't need expect for this; just use an ssh key and send the commands like "ssh host 'cat /etc/redhat-release'". In general, you shouldn't use Expect unless you have no choice. As to your actual code, the problem is that you send a bunch of commands very fast without waiting for responses. After: > $exp->send("$fields[$i]\r"); put something like: $exp=>expect(10, "root@"); to wait for the next prompt. -Robin On Sun, Sep 15, 2013 at 09:55:19PM +0530, uday wrote: > *Hi perl experts,* > > *I'm 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? Highly appreciated for early reply. > > * * > > * **INPUT FILE:* > > * ** [root@server-1 ~]# cat "cs3.csv"* > > *192.18.16.19,root,conley,cat /etc/redhat-release,ls,date* > > * [root@server-1 ~]#* > > * * > > *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 > test.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. > > > > *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 > $fields[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 ~]# > > > > > > -- > Cheers > Udhaya.D > ------------------------------------------------------------------------------ > LIMITED TIME SALE - Full Year of Microsoft Training For Just $49.99! > 1,500+ hours of tutorials including VisualStudio 2012, Windows 8, SharePoint > 2013, SQL 2012, MVC 4, more. BEST VALUE: New Multi-Library Power Pack includes > Mobile, Cloud, Java, and UX Design. Lowest price ever! Ends 9/20/13. > http://pubads.g.doubleclick.net/gampad/clk?id=58041151&iu=/4140/ostg.clktrk > _______________________________________________ > Expectperl-discuss mailing list > Exp...@li... > https://lists.sourceforge.net/lists/listinfo/expectperl-discuss -- http://intelligence.org/ : Our last, best hope for a fantastic future. .i ko na cpedu lo nu stidi vau loi jbopre .i danfu lu na go'i li'u .e lu go'i li'u .i ji'a go'i lu na'e go'i li'u .e lu go'i na'i li'u .e lu no'e go'i li'u .e lu to'e go'i li'u .e lu lo mamta be do cu sofybakni li'u |