#!/usr/bin/perl use strict; #use warnings; use Term::ReadKey; use Text::Wrap; use Term::Prompt; use Expect; use threads; use threads::shared; use Term::ANSIColor; use File::Path; use HTML::Entities; use MIME::QuotedPrint; use MIME::Base64; use MIME::Lite; use Date::Calc qw(Delta_DHMS); my $pass = ""; my $User = ""; $SIG{'INT' } = 'interrupt'; $SIG{'QUIT'} = 'interrupt'; $SIG{'HUP' } = 'interrupt'; $SIG{'TRAP'} = 'interrupt'; $SIG{'ABRT'} = 'interrupt'; $SIG{'STOP'} = 'interrupt'; my $sshAuth = Expect->spawn("ssh $User\@hostname"); $sshAuth->log_stdout(0); my $thr1 = threads->create('thread1',$sshAuth); $thr1->join(); sub thread1 { my $sshAuth = shift; our $sleepFlag = ''; $sshAuth->expect(1000, [qr/[Yy]es/ => sub {$sshAuth->send("yes\n");}], [qr/[Pp]assword\:\s/ => sub {$sshAuth->send("$pass\n");}] ); $sshAuth->log_stdout(0); $sshAuth->expect(1000, [qr/\$/ => sub {$sshAuth->send("\n");}]); $sshAuth->log_stdout(0); $sshAuth->expect(1000, [qr/\$/ => sub {$sshAuth->send("cd Perl\/Test\n");}]); $sshAuth->log_stdout(0); ###### Here are the critical tasks that should not abort on Ctrl+C ######### our $sleepFlag = "true"; $sshAuth->expect(1000, [qr/\$/ => sub {$sshAuth->send("\.\/sleeper\.sh\n");}]); $sshAuth->log_stdout(0); print "Entering Critical sleeping ... \n"; $sshAuth->expect(1000, [qr/\$[\s]*/ => sub {$sshAuth->send("\n");}]); $sshAuth->log_stdout(0); print "Woke up \n"; our $sleepFlag = "false"; ###### I really dont mind if the stuff here terminates on Ctrl+C ######### $sshAuth->expect(1000, [qr/\$/ => sub {$sshAuth->send("\.\/sleeper\.sh\n");}]); $sshAuth->log_stdout(0); print "Entering Non Critical sleeping ... \n"; $sshAuth->expect(1000, [qr/\$[\s]*/ => sub {$sshAuth->send("\n");}]); $sshAuth->log_stdout(0); print "Woke up \n"; $sshAuth->close(); } sub interrupt { our $sleepFlag:shared; print "$sleepFlag\n"; print "Waiting for critical tasks to finish before terminating program"; while ($sleepFlag eq "true") { print "."; sleep(3); } print "\n\nNo critical tasks are running... it\'s safe to terminate now...\n"; print "Terminating Program!!!\n"; }