简体   繁体   English

Perl:SSH尾巴作为文件处理程序

[英]Perl: SSH Tail as File Handler

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. 该日志位于远程主机上,因此我正在使用SSH和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. 如果用户没有在警报延迟到期之前输入SSH连接的密码,警报将开始触发。 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? 或者,可以将File :: Tail模块与SSH,telnet,rlogin等结合使用以提供相同的功能吗?

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;
}

请参阅Net :: SFTP :: Foreign附带的sftp_tail.pl示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM