Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

perl expect

by perluday (Initiate)
on Sep 15, 2013 at 08:46 UTC ( [id://1054174]=perlquestion: print w/replies, xml ) Need Help??

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
    useNet::OpenSSH;
    ... my ($host, $user, $password, @cmds) = split ',', $line; my $ssh = Net::OpenSSH->new($host, user => $user, password => $passwor +d); if ($ssh->error) { print STDERR "Unable to log into remote host: ".$ssh->error; next; } print "HOST: $host\n\n" for my $cmd (@cmds) { my ($out, $err) = $ssh->capture2($cmd); print "CMD: $cmd\nRC: $?\nSTDOUT:\n$out\nSTDERR:\n$err\n\n\n"; }
    Consider also using some CPAN module to parse the CSV files (i.e. Text::CSV). That way you will be able to handle files with quoted fields inside correctly.
Re: perl expect
by Laurent_R (Canon) on Sep 15, 2013 at 21:26 UTC

    Please try to reformat the first paragraph of your post, it is just incomprehensible for me as it is now. Please put code tags around what seems to be the output of your program. Possibly also explain what is wrong in the output you get and what you expect.

Re: perl expect
by Laurent_R (Canon) on Sep 16, 2013 at 06:16 UTC

    Thanks for reformating, it is much more readable now. Hmm, not quite sure what your program is doing and no way to test your code right now, but from the sheer description of the problem, maybe you are suffering from IO buffering.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1054174]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found