INPUT FILE:
[root@server-1 ~]# cat "cs3.csv"
192.18.16.19,root,conley,cat /etc/redhat-release,ls,date
[root@server-1 ~]#
####
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 ~]#