LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   How to Capture a Unix Terminal Session? (http://www.linuxquestions.org/questions/linux-server-73/how-to-capture-a-unix-terminal-session-4175410875/)

kalpeer 06-11-2012 10:48 AM

How to Capture a Unix Terminal Session?
 
Hi All,

I want to capture all the operations performed in the terminal. So to achieve this I used “script” command. This works as I expected. But this command captures all the standard output which is redirected to terminal. For example if i “tail” a file, even the tail output is getting captured. But I want to capture only the commands not the output. I don’t want to capture the Standard Output. Is there any way to capture only the commands that are typed in the terminal?

I don’t want use the bash history since it will not store TTY information. I have automated this process, whenever user logins a script will be started to capture the operations.

Thanks,
Kalai

divyashree 06-11-2012 12:02 PM

Quote:

Originally Posted by kalpeer (Post 4700708)
Hi All,

I want to capture all the operations performed in the terminal. So to achieve this I used “script” command. This works as I expected. But this command captures all the standard output which is redirected to terminal. For example if i “tail” a file, even the tail output is getting captured. But I want to capture only the commands not the output. I don’t want to capture the Standard Output. Is there any way to capture only the commands that are typed in the terminal?

I don’t want use the bash history since it will not store TTY information. I have automated this process, whenever user logins a script will be started to capture the operations.

Thanks,
Kalai

You can tweak the HISTFILE variable in user's rc/profile to save the command history on terminal number basis of each user.

Code:

TTY=$(tty | sed 's|/|_|g')
HISTFILE="~/.bash_history$TTY"


unSpawn 06-12-2012 01:27 PM

Quote:

Originally Posted by kalpeer (Post 4700708)
I want to capture all the operations performed in the terminal.

Have a look here?

kalpeer 06-12-2012 02:40 PM

Quote:

Originally Posted by divyashree (Post 4700742)
You can tweak the HISTFILE variable in user's rc/profile to save the command history on terminal number basis of each user.

Code:

TTY=$(tty | sed 's|/|_|g')
HISTFILE="~/.bash_history$TTY"


Thanks a lot this helped me..
But in history file a date is getting updated along with each command. Is it possible to remove all other data except command.

Quote:

#13000123
ls
#13000124
cd /tmp

unSpawn 06-16-2012 04:54 AM

Quote:

Originally Posted by kalpeer (Post 4700708)
I don’t want use the bash history since it will not store TTY information.

One of the rules of safe(r) programming says you should never trust user-submitted input. So no, you should not trust or rely on settings or files if the user owns them and can modify them at will.

LanceTaylor 11-17-2015 11:09 AM

sed or HISTTIMEFORMAT
 
Quote:

Originally Posted by kalpeer (Post 4701651)
...in history file a date is getting updated along with each command. Is it possible to remove all other data except command.

The HISTTIMEFORMAT environment variable is what is causing the timestamps to be set in your history file. You could unset that variable and it will no longer be put in the history. There may be other times when you want or need the timestamps, so this might not be the best option.

Alternatively, you could use sed to read the history, discard the timestamps, and save it to a new file.
Code:

sed -n '/^#/ !p' /home/user/.bash_history > newfile
The -n option will prevent sed from copying out the active buffer. Without this you would see the the timestamps once and then each command line would repeated in the output. The next part matches lines that begin with a hash. The exclamation point reverses the match, and the p tells sed to print the matching lines. We get our input from the bash history file and redirect output of the command to newfile.

Using sed will keep the timestamps in your history file, which may be useful for other things you may want to do later, but allows you to get only the info that you want to use now.

Habitual 11-17-2015 03:39 PM

http://www.unix.com/302404510-post4.html
https://dayaramb.wordpress.com/2013/...tory-in-linux/

onlyonemac 11-20-2015 05:18 AM

You'll also want to bear in mind if you want your users to be aware that their session is being logged. Launching a shell session inside a wrapper for the "script" command is fairly clandestine, but modifying the profile file as suggested by divyashree is likely to raise suspicion if the user sees what has been done.


All times are GMT -5. The time now is 01:02 AM.