in reply to Logging user input to a file, and passing it to an external program, at the same time
#!/usr/bin/perl -l BEGIN { $| = 1; } use strict; use warnings; use Term::ReadLine; use POSIX qw(strftime); my $username = getlogin; my $logpath = '/tmp/sessionlogs'; my(%cfg) = ( shell => '/bin/bash', dateformat => '[%Y%m%d-%H%M%S] ', logpath => $logpath, logname => "$username.session.log", ); my(@prohibited_cmds) = ( 'mc', '/usr/bin/mc', ); open LOGFILE, ">>$cfg{'logpath'}$cfg{'logname'}" or die "Cannot open log file: $!"; print "enter 'exit' to quit."; print LOGFILE "-----------------------------------\n"; print LOGFILE strftime( $cfg{'dateformat'}, localtime ); print LOGFILE "Session started for $username."; my $term = Term::ReadLine->new('Shellwrapper'); $SIG{'INT'} = 'IGNORE'; while (defined ($_ = $term->readline('SH+log> '))) { if (/^\s*quit\s*$/ or /^\s*exit\s*$/) { print LOGFILE strftime( $cfg{'dateformat'}, localtime ); print LOGFILE "$_\n"; last; } print LOGFILE strftime( $cfg{'dateformat'}, localtime ); print LOGFILE "$_\n"; system "$cfg{'shell'} -c '$_'"; print "\n"; } print LOGFILE strftime( $cfg{'dateformat'}, localtime ); print LOGFILE "Session terminated for $username."; close LOGFILE;
|
|---|