开发者

Perl: SSH Tail as File Handler

开发者 https://www.devze.com 2023-04-09 05:20 出处:网络
I am creating a log parser that has the ability to \"stream\" a log as it is written. The log resides on a remote host, so I am creating a file handler using a combination of

I am creating a log parser that has the ability to "stream" a log as it is written. The log resides on a remote host, so I am creating a file handler using a combination of SSH and tail. The following works quite well, but I do have some questions regarding error handling.

  • If the user doesn't enter the password for the SSH connection prior to the alarm delay expiring, the alarm will start triggering. This leads to the console being cleared so it is not clear that the password needs to be entered.

  • If i enter the wrong password, i still enable the alarm leading to screen clears, ect...

    Password:
    Password:
    Password:
    Permission denied (publickey,keyboard-interactive).
    
  • If i provide a log filename that doesn't exist, the code continues....

    tail: cannot open `/path_to_log/mylog.logXXXX' for reading: No such file or directory
    tail: no files remaining
    

So, my question is what is the best way add some additional error handling. Alternatively, can the File::Tail module be used in combination with SSH, telnet, rlogin, etc to provide the same functionality?

Thanks!

    my $stopMsg = "Use CTRL+C to stop streaming at any time...\n";
    my $开发者_运维知识库SSH = sprintf "ssh %s@%s tail -f %s | ", $user, $host, $log;

    printf "Log: %s\n", $log;
    printf "Handle: %s\n", $SSH;

    my $errMsg = sprintf "Couldn't establish SSH connection to \"%s\":",
                 $host;

    open my $pipe, $SSH or error( $errMsg );

    my $loadTime = time;

    printf $stopMsg;

    setSignalHandler( 'INT', sub{ stopAlarm( TRUE ); } );

    startAlarm( $delay,
                $interval,
                sub { system "clear"; $handler->( \@sysLogArr ); printf $stopMsg; } );

    while ( alarmHandlerSet() )
    {
        my $data = <$pipe>;

        next unless defined $data;

        mapSysLog( line   => $data,
                   arrRef => $logRef,
                   varRef => \%sysLogVars,
                   dbRef  => $dbRef );
    }

    clearSignalHandler( 'INT' );



sub error(@)
{
    my $color = "BOLD";
    $color = $CONFIG{errorPrinter} if ( $CONFIG{colorEnable} &&
                                        defined $CONFIG{errorPrinter} );

    color2PrinterRef( $color )->( "\nERROR: " );

    printf "%s\n", shift;
    printf "      %s\n", $_ foreach ( @_ );
    printf "Called From: %s::%d\n", (caller)[1], (caller)[2];
    printf "\n";

    exit EXIT_FAILURE;
}


See the sftp_tail.pl sample included with Net::SFTP::Foreign.

0

精彩评论

暂无评论...
验证码 换一张
取 消