Santosh
Santosh

Reputation: 95

gracefully close ssh and exit net::ssh::perl when SIGINT

I am connecting to a remote host and executing a command which does not exit (tail -f logfile)

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

So I am registering a signal handler for SIGINT

Need the code which I need to put in the subroutine

The below code is inside a function which is called from forked child

#!/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 <tc_id>\n";
        exit;
}

$tc_id = shift;
$file = "config.prop";
$log_dir = "logs/";


#$SIG{INT}=\&close_write;

sub close_write
{
#-- write it to file
print "\nInside END block\n";
#open FH, ">$log_file";
#print $log_file;
#print FH @out;
#$thr->kill('INT');
#close $ssh->sock;
#undef $ssh;
exit 1;
}

# Read the configuration file and populate the Hash
$index = 0;
my $csv = Text::CSV_XS->new ({ binary => 1, eol => $/ });
 open my $io, "<", $file or die "$file: $!";
 while (my $row = $csv->getline ($io)) {
    next if (${$row}[0] =~ m/^#/);  #Ignore comments
    next if (${$row}[0] =~ m/^\s*$/);   #Ignore blank lines
    ($logHashArray->[$index]->{host}, $logHashArray->[$index]->{user}, $logHashArray->[$index]->{pass}, $logHashArray->[$index]->{cmd}, $logHashArray->[$index]->{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->{log_file} = $tc_id . "_" . $logHash->{host} . "_" .$logHash->{log_file};
        $logHash->{cmd} = $logHash->{cmd} . " | tee /tmp/" . $logHash->{log_file};
        $logHash->{log_dir} = $log_dir;

        #$logHash->{thr}=threads->new(\&connect_get_logs, $logHash);

        $logHash->{pid} = fork();

        if ($logHash->{pid}){
            # Parent
            push(@childs, $logHash->{pid});
        }
        elsif ($pid == 0){
            # Child
            connect_get_logs($logHash);
        }
        else {
            die "couldn’t fork: $!\n";
            }

while (($key, $value) = each(%{$logHash})){
     print $key."=>".$value."\n";
}

}

#$SIG{INT}=\&close_write;
#$thr=threads->new(\&connect_get_logs, $logHash);

foreach (@childs) {
    waitpid($_, 0);
    }


#print "Waiting...";
#while(1) {sleep 1;}

#$thr->join;


sub connect_get_logs{

$SIG{INT}= sub {
        print "Inside INT block\n"; ### Need proper code here
        close $ssh->sock;
        undef $ssh;
        };

my $logHash = shift;

while (($key, $value) = each(%{$logHash})){
     print $key."=>".$value."\n";
}
my $stdout;
my $stderr;
my $exit;

#-- setup a new connection
print "Logging in to $logHash->{host}...";
my $ssh = Net::SSH::Perl->new($logHash->{host}, debug => 0, protocol => '2', options => ["PasswordAuthentication yes", "BatchMode yes",
                    "PubkeyAuthenticaion no", "RhostsAuthentication no", "RhostsRSAAuthentication no", "RSAAuthentication no", "DSAAuthentication no"]);

#-- authenticate
$ssh->login($logHash->{user}, $logHash->{pass});
print "Logged In\n";

#-- Create or Overwrite the log files
open LOG_FILE, ">", "$logHash->{log_dir}$logHash->{log_file}" or die $!;

#-- register a handler
$ssh->register_handler("stdout", sub {
        my($channel, $buffer) = @_;
        $str = $buffer->bytes;
        #push @out, $str;
        print LOG_FILE $str;
        #print $str;
});

#$SIG{INT}=\&close_write;

#-- execute the command
($stdout, $stderr, $exit) = $ssh->cmd($logHash->{cmd});

print "Error: $stderr";

}

create a config.prop file in the csv format

host/ip,username,password,command (tail -F /full/path/to/logfile),filename to save as

Upvotes: 5

Views: 4412

Answers (5)

salva
salva

Reputation: 10242

Here, you can see an alternative solution that uses SFTP instead of SSH to continuously read from a remote file: sftp_tail.pl.

The remote sftp-server process will exit when the connection is closed (for instance, because you press Ctrl-C) without requiring any further action on your part.

Upvotes: 0

Leonardo
Leonardo

Reputation: 1

The full procedure to login, access and close an ssh session inside the perl.

            my $host = 192.168.0.1;

            my $username = "example";

            my $passwd  = "example";

            my $ssh = Net::SSH::Expect->new(
            host => $host,
            user => $username,
            password => $passwd,
            timeout => 20,
            );
            $ssh->login();
            $ssh->exec("command_to_be_executed_in_ssh\r\n");
            $ssh ->close;

Upvotes: -1

Leonardo
Leonardo

Reputation: 1

$ssh->login(); #to login in a ssh session

$ssh ->close; #to logout from an ssh session

enjoy.

Upvotes: 0

strix
strix

Reputation: 83

The trick is invoking ssh with -t (or possibly -tt).

By default, ssh allocates a pty on the remote machine only for interactive login shells (ie, not when invoking a command on the remote). -t forces ssh to allocate a pty, even when executing a remote command. You can see the effect:

$ ssh starquake tty
not a tty

vs

$ ssh -t starquake tty
/dev/pts/1
Connection to starquake closed.

ssh apparently will pass on signals to the remote process when attached to the terminal, but not otherwise.

Upvotes: 2

simbabque
simbabque

Reputation: 54371

If you know the process id of the call to tail -f, you can send it an INT signal from the command line with:

kill -s INT <pid>

What you need to do in your $SIG{'INT'} in your main program is let it also send an INT through the Net::SSH connection. Unfortunately, I have no idea how to do that.

Upvotes: 0

Related Questions