title: gracefully close ssh and exit net::ssh::perl when SIGINT
body: <p>I am connecting to a remote host and executing a command which does not exit (tail -f logfile)</p>
  
  <p>I am registering a handler and writing the output to a log file.
  This is working fine
  But I want to press Control+C on the main program and it should stop the command on remote machine and close the ssh connection gracefully</p>
  
  <p>So I am registering a signal handler for SIGINT</p>
  
  <p>Need the code which I need to put in the subroutine</p>
  
  <p>The below code is inside a function which is called from forked child</p>
  
  <pre><code>#!/ats/bin/perl
  
  use Net::SSH::Perl;
  use File::Path;
  use Text::CSV_XS;
  
  chomp ($progName = `basename $0`);
  
  if (@ARGV != 1 ) {
          print "usage: perl $progName &lt;tc_id&gt;\n";
          exit;
  }
  
  $tc_id = shift;
  $file = "config.prop";
  $log_dir = "logs/";
  
  
  #$SIG{INT}=\&amp;close_write;
  
  sub close_write
  {
  #-- write it to file
  print "\nInside END block\n";
  #open FH, "&gt;$log_file";
  #print $log_file;
  #print FH @out;
  #$thr-&gt;kill('INT');
  #close $ssh-&gt;sock;
  #undef $ssh;
  exit 1;
  }
  
  # Read the configuration file and populate the Hash
  $index = 0;
  my $csv = Text::CSV_XS-&gt;new ({ binary =&gt; 1, eol =&gt; $/ });
   open my $io, "&lt;", $file or die "$file: $!";
   while (my $row = $csv-&gt;getline ($io)) {
      next if (${$row}[0] =~ m/^#/);  #Ignore comments
      next if (${$row}[0] =~ m/^\s*$/);   #Ignore blank lines
      ($logHashArray-&gt;[$index]-&gt;{host}, $logHashArray-&gt;[$index]-&gt;{user}, $logHashArray-&gt;[$index]-&gt;{pass}, $logHashArray-&gt;[$index]-&gt;{cmd}, $logHashArray-&gt;[$index]-&gt;{log_file}) = @$row;
      $index++;
   }
  
  
  # Append "/" at the end of the directory if it does not exist
  unless ($log_dir =~ m/\/$/) {
          $log_dir = $log_dir . "/";
          print "LogDir: $log_dir\n";
          }
  # Check if the log directory exists, if not, create it
  if (-e $log_dir) {
          unless (-d $log_dir) {
                  die "File exists but is not directory";
                  }
          }
  else {
          # don't forget to check mkdir's failure
          print "Directory $log_dir does not exist... Creating it\n";
          mkpath($log_dir, 0777) or die "Can't make directory: $!";
          }
  
  
  
  foreach $logHash (@{$logHashArray}){
          #print "LogHash Index $logHash\n";
          $logHash-&gt;{log_file} = $tc_id . "_" . $logHash-&gt;{host} . "_" .$logHash-&gt;{log_file};
          $logHash-&gt;{cmd} = $logHash-&gt;{cmd} . " | tee /tmp/" . $logHash-&gt;{log_file};
          $logHash-&gt;{log_dir} = $log_dir;
  
          #$logHash-&gt;{thr}=threads-&gt;new(\&amp;connect_get_logs, $logHash);
  
          $logHash-&gt;{pid} = fork();
  
          if ($logHash-&gt;{pid}){
              # Parent
              push(@childs, $logHash-&gt;{pid});
          }
          elsif ($pid == 0){
              # Child
              connect_get_logs($logHash);
          }
          else {
              die "couldn’t fork: $!\n";
              }
  
  while (($key, $value) = each(%{$logHash})){
       print $key."=&gt;".$value."\n";
  }
  
  }
  
  #$SIG{INT}=\&amp;close_write;
  #$thr=threads-&gt;new(\&amp;connect_get_logs, $logHash);
  
  foreach (@childs) {
      waitpid($_, 0);
      }
  
  
  #print "Waiting...";
  #while(1) {sleep 1;}
  
  #$thr-&gt;join;
  
  
  sub connect_get_logs{
  
  $SIG{INT}= sub {
          print "Inside INT block\n"; ### Need proper code here
          close $ssh-&gt;sock;
          undef $ssh;
          };
  
  my $logHash = shift;
  
  while (($key, $value) = each(%{$logHash})){
       print $key."=&gt;".$value."\n";
  }
  my $stdout;
  my $stderr;
  my $exit;
  
  #-- setup a new connection
  print "Logging in to $logHash-&gt;{host}...";
  my $ssh = Net::SSH::Perl-&gt;new($logHash-&gt;{host}, debug =&gt; 0, protocol =&gt; '2', options =&gt; ["PasswordAuthentication yes", "BatchMode yes",
                      "PubkeyAuthenticaion no", "RhostsAuthentication no", "RhostsRSAAuthentication no", "RSAAuthentication no", "DSAAuthentication no"]);
  
  #-- authenticate
  $ssh-&gt;login($logHash-&gt;{user}, $logHash-&gt;{pass});
  print "Logged In\n";
  
  #-- Create or Overwrite the log files
  open LOG_FILE, "&gt;", "$logHash-&gt;{log_dir}$logHash-&gt;{log_file}" or die $!;
  
  #-- register a handler
  $ssh-&gt;register_handler("stdout", sub {
          my($channel, $buffer) = @_;
          $str = $buffer-&gt;bytes;
          #push @out, $str;
          print LOG_FILE $str;
          #print $str;
  });
  
  #$SIG{INT}=\&amp;close_write;
  
  #-- execute the command
  ($stdout, $stderr, $exit) = $ssh-&gt;cmd($logHash-&gt;{cmd});
  
  print "Error: $stderr";
  
  }
  </code></pre>
  
  <p>create a config.prop file in the csv format</p>
  
  <p>host/ip,username,password,command (tail -F /full/path/to/logfile),filename to save as</p>