LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2013, 12:07 PM   #1
woodson2
Member
 
Registered: Oct 2008
Posts: 51

Rep: Reputation: 15
Piping the "script" command through the logger command.


I use the snippet below in /etc/profile on RHEL Linux to capture command line logging and it all works well and good.

Now I'd like to pipe the same output from script through the logger command so it all gets logged to syslog.

The only additional code I've added is in bold below (| bin/logger).

This works as expected sans one issue, albeit a major one. My terminal session is blank as if nothing is being typed, however if I type commands I can see them being logged and if I type exit my session closes. I tried a nohup and & to see if that would help but it does not. I'm wondering why I can no longer see anything on my tty.

This is what my putty session looks like. So I have a fully functional session but I can't see any output.
[user@test1 ~]$ ssh cxxx
user@test1's password:
Last login: Thu Sep 12 09:56:01 2013 from 10.x.x.x



Code:
if [ -z $PS1 ]
  then
    echo "" > /dev/null
  else
    DATE="/bin/date"  SCRIPT="/usr/bin/script"
    LOGBASE="/log/cmdline_logs"
       if [ -d "${LOGBASE}" ]; then
           TIMESTAMP="$( ${DATE} +%Y%m%d%H%M%S )"
           LOGFILE="${LOGBASE}/${HOSTNAME}_${USER}_${TIMESTAMP}"
           umask 077
           [[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_profile" ]] && . ${HOME}/.bash_profile
           ${SCRIPT} -f -q ${LOGFILE}.log | /bin/logger
           [[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_logout" ]] && . ${HOME}/.bash_logout
           exit
       fi
  
fi
 
Old 09-12-2013, 12:48 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
Try using the tee command:

if [ -z $PS1 ]
then
echo "" > /dev/null
else
DATE="/bin/date" SCRIPT="/usr/bin/script"
LOGBASE="/log/cmdline_logs"
if [ -d "${LOGBASE}" ]; then
TIMESTAMP="$( ${DATE} +%Y%m%d%H%M%S )"
LOGFILE="${LOGBASE}/${HOSTNAME}_${USER}_${TIMESTAMP}"
umask 077
[[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_profile" ]] && . ${HOME}/.bash_profile
${SCRIPT} -f -q ${LOGFILE}.log | tee /bin/logger
[[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_logout" ]] && . ${HOME}/.bash_logout
exit
fi

fi


-------------------------
Steve Stites
 
Old 09-12-2013, 01:28 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by woodson2 View Post
I use the snippet below in /etc/profile on RHEL Linux to capture command line logging and it all works well and good.
Please define "good"? As 'script' here gets executed by the user, only works with BASH and doesn't use variables that can't be re-defined by the user afterwards it does not constitute a tamper proof audit trail IMHO.


Quote:
Originally Posted by woodson2 View Post
Now I'd like to pipe the same output from script through the logger command so it all gets logged to syslog.
The way you try to implement it should not work because 'script' logs to a file while 'logger' requires stdout. Note modern syslog implementations like Rsyslogd and Syslog-NG can read from files. Also note that if you allow user input to be logged to syslog you should not ever parse it afterwards (because the script utility also logs line feeds, backspaces and control characters) and you must watch the partition (or disk) closely for free disk space. (What happens if I 'cat /var/log/messages' repeatedly inside my script session? ;-p)
 
Old 09-12-2013, 01:37 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
you will have to forgive me on my naivety here...

I think ( not certain ) script works in the same way as a shell, but sending stdin to both stdout and a file and not just stdout
when you pipe to logger, logger takes stdout and sends to file ( syslog )

I believe it may in the long run be easier to modify script's source to send direct to syslog as well as it's normal behaviour.
I have not yet looked at the source, but I imagine it would be relatively straight forward

however, had a little play and came up with this

Code:
touch typescript # probably don't need
( tail -f typescript | logger ) &
script -f
the -f for script flushes to script's logfile (typyscript here) each write
tail 'follows' typescript, piping to logger

the downside is some of the output are 'odd'
'shell'
Code:
$ script -f
tail: typescript: file truncated
Script started, file is typescript
firerat@Ab9Pro:/home/firerat/LinuxQ/LogScript$ echo "testing one two three"
testing one two three
firerat@Ab9Pro:/home/firerat/LinuxQ/LogScript$ sleep 2
firerat@Ab9Pro:/home/firerat/LinuxQ/LogScript$ for i in {1..4};do sleep $i;echo slept 1 second;done
slept 1 second
slept 1 second
slept 1 second
slept 1 second
firerat@Ab9Pro:/home/firerat/LinuxQ/LogScript$ exit
syslog
Code:
Sep 12 19:19:22 Ab9Pro firerat: Script started on Thu 12 Sep 2013 19:19:22 BST
Sep 12 19:19:29 Ab9Pro firerat: #033]0;firerat@Ab9Pro: /home/firerat/LinuxQ/LogScript#007firerat@Ab9Pro:/home/firerat/LinuxQ/LogScript$ echo "testing one two three"#015
Sep 12 19:19:29 Ab9Pro firerat: testing one two three#015
Sep 12 19:19:56 Ab9Pro firerat: #033]0;firerat@Ab9Pro: /home/firerat/LinuxQ/LogScript#007firerat@Ab9Pro:/home/firerat/LinuxQ/LogScript$ sleep 2#015
Sep 12 19:20:52 Ab9Pro firerat: #033]0;firerat@Ab9Pro: /home/firerat/LinuxQ/LogScript#007firerat@Ab9Pro:/home/firerat/LinuxQ/LogScript$ for i in sle#010#033[K#010#033[K#010#033[K{1,2,3#010#033[K#010#033[K#010#033[K#010#033[K#010#033[K#010#033[K#010#033[K#010#033[Kn {1..4};do sleep $i;echo slpet#010#033[K#010#033[K#010#033[Kept 1#010#033[K1 second;done#015
Sep 12 19:20:53 Ab9Pro firerat: slept 1 second#015
Sep 12 19:20:55 Ab9Pro firerat: slept 1 second#015
Sep 12 19:20:58 Ab9Pro firerat: slept 1 second#015
Sep 12 19:21:02 Ab9Pro firerat: slept 1 second#015
Sep 12 19:21:08 Ab9Pro firerat: #033]0;firerat@Ab9Pro: /home/firerat/LinuxQ/LogScript#007firerat@Ab9Pro:/home/firerat/LinuxQ/LogScript$ exit#015
Sep 12 19:21:08 Ab9Pro firerat: exit#015
Sep 12 19:21:08 Ab9Pro firerat:
Sep 12 19:21:08 Ab9Pro firerat: Script done on Thu 12 Sep 2013 19:21:08 BST
you can see me 'fixing' my rethinks and typos

I guess you could have sed between tail and logger, to make them readable

'raw' typescript file
Code:
Script started on Thu 12 Sep 2013 19:19:22 BST
^[]0;firerat@Ab9Pro: /home/firerat/LinuxQ/LogScript^Gfirerat@Ab9Pro:/home/firerat/LinuxQ/LogScript$ echo "testing one two three"^M
testing one two three^M
^[]0;firerat@Ab9Pro: /home/firerat/LinuxQ/LogScript^Gfirerat@Ab9Pro:/home/firerat/LinuxQ/LogScript$ sleep 2^M
^[]0;firerat@Ab9Pro: /home/firerat/LinuxQ/LogScript^Gfirerat@Ab9Pro:/home/firerat/LinuxQ/LogScript$ for i in sle^H^[[K^H^[[K^H^[[K{1,2,3^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[Kn {1..4};do sleep $i;echo slpet^H^[[K^H^[[K^H^[[Kept 1^H^[[K1 second;done^M
slept 1 second^M
slept 1 second^M
slept 1 second^M
slept 1 second^M
^[]0;firerat@Ab9Pro: /home/firerat/LinuxQ/LogScript^Gfirerat@Ab9Pro:/home/firerat/LinuxQ/LogScript$ exit^M
exit^M

Script done on Thu 12 Sep 2013 19:21:08 BST

another downside is you need to clean up ( tail -f typescript | logger ),
you could get the PID of it with $!, and then kill it once script exits

ultimately, I think modifying script's source code, adding a log to syslog flag is the cleanest

edit:
prompted by unSpawn's post, I did
Code:
cat typescript
cat typescript
inside the script shell
it then 'took over' with a nasty loop, filling syslog

Last edited by Firerat; 09-12-2013 at 01:44 PM.
 
Old 09-13-2013, 08:09 AM   #5
woodson2
Member
 
Registered: Oct 2008
Posts: 51

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by unSpawn View Post
Please define "good"? As 'script' here gets executed by the user, only works with BASH and doesn't use variables that can't be re-defined by the user afterwards it does not constitute a tamper proof audit trail IMHO.




The way you try to implement it should not work because 'script' logs to a file while 'logger' requires stdout. Note modern syslog implementations like Rsyslogd and Syslog-NG can read from files. Also note that if you allow user input to be logged to syslog you should not ever parse it afterwards (because the script utility also logs line feeds, backspaces and control characters) and you must watch the partition (or disk) closely for free disk space. (What happens if I 'cat /var/log/messages' repeatedly inside my script session? ;-p)
The files are initiated by the user and placed in a directory with the necessary permissions to prevent tampering. The script works with bash,ksh and sh.

[root@test1 ~]# ls -ld /log
drwxr-x--x 6 root root 4096 Sep 12 13:38 /log

[root@test1 ~]# ls -ld /log/cmdline_logs/
drwxr-x-wx 2 root root 4096 Sep 12 14:12 /log/cmdline_logs/

We have measures in place for disk usage thresholds along with proper rotation/pruning. We would also be notified if space were to start filling up. I appreciate your efforts to bring to light some shortcomings or considerations, however I was able to get this working with the code below.



Code:
if [ -z $PS1 ]
  then
        :  #do nothing stub
  else
    DATE="/bin/date"  SCRIPT="/usr/bin/script"
    LOGBASE="/log/cmdline_logs"
       if [ -d "${LOGBASE}" ]; then
           TIMESTAMP="$( ${DATE} +%Y%m%d%H%M%S )"
           LOGFILE="${LOGBASE}/${HOSTNAME}_${USER}_${TIMESTAMP}"
           umask 077
           [[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_profile" ]] && . ${HOME}/.bash_profile

              : > $LOGFILE.log
                ( tail -f $LOGFILE.log | /bin/logger -t TRACKING ) &

                trap "kill $!" EXIT # Kill the logger subshell on exit

                ${SCRIPT} -a -f -q ${LOGFILE}.log

           [[ "${SHELL}" = "/bin/bash" && -e "${HOME}/.bash_logout" ]] && . ${HOME}/.bash_logout
           exit
       fi
fi

Last edited by woodson2; 09-13-2013 at 08:13 AM.
 
Old 09-14-2013, 02:25 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by woodson2 View Post
I appreciate your efforts to bring to light some shortcomings or considerations, however
Besides responding to only part of what I wrote about it's the "however" part that's especially worrying. Could it be you have no idea what constitutes a proper audit trail? Or that you just don't care? Have you spent any time at all figuring out the ways a user could subvert your "solution"?

Code:
~]$ cat ${HOME}/.bash_profile
declare -r LOGBASE="/dev/null"
declare -r SCRIPT="/bin/bash --noprofile --login --"
export LOGBASE SCRIPT
or
Code:
~]$ cat ${HOME}/.bash_profile
[ "${SHELL:5:5} == "bash" ] || /bin/bash --noprofile --login --"
or just
Code:
~]$ chsh -s tcsh

Again: an audit trail should not be initiated by and not be controlled by the user in any aspect.
 
Old 09-15-2013, 04:36 PM   #7
woodson2
Member
 
Registered: Oct 2008
Posts: 51

Original Poster
Rep: Reputation: 15
Thanks, your insults along with your unsolicited advice have been especially "helpful".
 
Old 09-15-2013, 06:01 PM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
When a man points
at the Moon the fool
looks at his Finger.
 
Old 09-15-2013, 07:48 PM   #9
woodson2
Member
 
Registered: Oct 2008
Posts: 51

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by unSpawn View Post
When a man points
at the Moon the fool
looks at his Finger.

Let me google a witty quote..
 
  


Reply

Tags
audit



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
Use of "Command line perl" in perl script using system command. aditya007 Linux - Newbie 4 11-29-2009 10:08 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
how to log user commands in xterm without using "script" command lilachb Linux - General 2 08-28-2005 01:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration