sub new { my $class = shift; my $self = {}; bless $self, $class; my $log = Log::Log4perl->get_logger("$class"); $self->{LOGGER} = $log; my $pty = $self->spawn("/bin/bash"); # spawn() defined below $self->{PTY} = $pty; my $exp = Expect->exp_init($pty); my $ref = $exp->expect(TIMEOUT, [ $shell_prompts ] ); if (!$ref){ $log->warn("Timeout received!"); return -1; } $exp->clear_accum(); #$exp->log_stdout(1); $exp->raw_pty(1); #$exp->debug(3); $Expect::Multiline_Matching = 0 ; $self->{EXPECT} = $exp; $log->debug("Creating instance $class"); return $self; } #### sub spawn { my($cmd) = @_; my($pid, $pty, $tty, $tty_fd); ## Create a new pseudo terminal. $pty = new IO::Pty or die $!; ## Execute the program in another process. unless ($pid = fork) { # child process die "problem spawning program: $!\n" unless defined $pid; ## Disassociate process from existing controlling terminal. use POSIX (); POSIX::setsid or die "setsid failed: $!"; ## Associate process with a new controlling terminal. $tty = $pty->slave; $pty->make_slave_controlling_terminal(); $tty_fd = $tty->fileno; close $pty; ## Make stdio use the new controlling terminal. open STDIN, "<&$tty_fd" or die $!; open STDOUT, ">&$tty_fd" or die $!; open STDERR, ">&STDOUT" or die $!; close $tty; ## Execute requested program. exec "/bin/bash" #exec $cmd or die "problem executing /bin/bash\n"; } # end child process return $pty; } # end sub spawn #### my $pm = new Parallel::ForkManager(5); for (1...10) { my $pid = $pm->start and next; my $exp = Expect_common->new(); # expect send ... # $pm->finish; # Terminates the child process } $pm->wait_all_children;