LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-11-2012, 09:48 AM   #1
kalpeer
LQ Newbie
 
Registered: Aug 2011
Location: Bangalore
Posts: 17

Rep: Reputation: Disabled
Thumbs up 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
 
Old 06-11-2012, 11:02 AM   #2
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,356

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by kalpeer View Post
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"
 
1 members found this post helpful.
Old 06-12-2012, 12:27 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,149
Blog Entries: 55

Rep: Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414
Quote:
Originally Posted by kalpeer View Post
I want to capture all the operations performed in the terminal.
Have a look here?
 
Old 06-12-2012, 01:40 PM   #4
kalpeer
LQ Newbie
 
Registered: Aug 2011
Location: Bangalore
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by divyashree View Post
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
 
Old 06-16-2012, 03:54 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,149
Blog Entries: 55

Rep: Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414Reputation: 3414
Quote:
Originally Posted by kalpeer View Post
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.
 
Old 11-17-2015, 10:09 AM   #6
LanceTaylor
Member
 
Registered: Nov 2015
Location: Houston, Texas, US
Distribution: RHEL/CentOS, Debian/Linux Mint, SLES
Posts: 40

Rep: Reputation: 19
sed or HISTTIMEFORMAT

Quote:
Originally Posted by kalpeer View Post
...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.
 
Old 11-17-2015, 02:39 PM   #7
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM 17.1
Posts: 6,081
Blog Entries: 10

Rep: Reputation: 1550Reputation: 1550Reputation: 1550Reputation: 1550Reputation: 1550Reputation: 1550Reputation: 1550Reputation: 1550Reputation: 1550Reputation: 1550Reputation: 1550
http://www.unix.com/302404510-post4.html
https://dayaramb.wordpress.com/2013/...tory-in-linux/
 
Old 11-20-2015, 04:18 AM   #8
onlyonemac
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17 MATE Edition
Posts: 41

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Capture the telnet session output in file anirudha0012 Linux - Newbie 2 03-02-2012 12:16 AM
make commands keep running after leaving a terminal session or ssh session Danny3031 Programming 18 01-30-2012 11:29 AM
ipvsadm session packet capture zmunawar1 Linux - Server 0 07-13-2008 03:08 PM
Switching back to the Mandrake 9.1 desktop from terminal rdesktop terminal session marc218 Linux - General 6 02-08-2007 02:45 PM


All times are GMT -5. The time now is 11:38 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration