shell script to capture keyboard activities - linux

How to capture all keyboard strokes using shell script .Is there any command that is related to keyboard activities.

If you would like to log all input and output, you can use the script command.
$ script transcript.txt
Script started, file is transcript.txt
$ echo 'Hello, world!'
Hello, world!
$ exit
Script done, file is transcript.txt
$ cat transcript.txt
Script started on Thu 09 Sep 2010 03:06:56 PM EDT
$ echo 'Hello, world!'
Hello, world!
$ exit
Script done on Thu 09 Sep 2010 03:07:06 PM EDT

Check out the trap command.
For example, type in your console :
trap "echo \"Arrrrggghhhh\"" INT
Now press Ctrl + C - fun fun :)

Related

Output shell script result to file like terminal screen

I am a beginner in shell scripting and this is my sample script:
#!/bin/ksh
echo "start script"
ls
echo "end script"
I run it by this command: ./runScript.ksh > outPutFile.txt
and got the output inside file:
start script
file1.txt
file2.txt
file3.txt
end script
I just want to ask if it is possible to have this output on the file? Like what is shown if the commands are executed on the terminal?:
user#serverName:/parentPath/childPath/>echo "start script"
user#serverName:/parentPath/childPath/>ls
file1.txt
file2.txt
file3.txt
user#serverName:/parentPath/childPath/>echo "end script"
Thank you so much in advance. I will really appreciate any help.
Use the 'script' command. Any command issued after the 'scipt' command will be captured in the specified file (or default file typescript).
**Example:**
$ script my_output.txt
Script started, output file is my_output.txt
$ uname -r
16.1.0
$ date
Wed Mar 8 12:55:29 IST 2017
$ exit
Script done, output file is my_output.txt
$ cat my_output.txt
Script started on Wed Mar 8 12:55:19 2017
$ uname -r
16.1.0
$ date
Wed Mar 8 12:55:29 IST 2017
$ exit
Script done on Wed Mar 8 12:55:32 2017
$
man script
SCRIPT(1) BSD General Commands Manual SCRIPT(1)
NAME
script -- make typescript of terminal session
SYNOPSIS
script [-adkpqr] [-F pipe] [-t time] [file [command ...]]
DESCRIPTION
The script utility makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session
as proof of an assignment, as the typescript file can be printed out later with lpr(1).
If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the file typescript.

"Ambiguous output redirect" trying to send both stdout and stderr to mailx from a command sent to at

I have a bash script called test.sh which, for the sake of simplicity, prints one line to stdout and one line to stderr.
test.sh:
#!/bin/bash
echo "this is to stdout"
echo "this is to stderr" 1>&2
I want to run the script test.sh at 7:00 PM, but only if certain conditions are met. To this end, I have another bash script called schedule.sh, which checks some stuff and then submits the command to at to be run later.
I want the output of test.sh (both stdout and stderr) to be sent to me in an email. I use mailx to do this so I can get a nice subject name.
Furthermore, I want at to shut up. No output from at because it always sends me ugly emails (no subject line) if at produces any output.
schedule.sh:
#!/bin/bash
my_email="me#example.com" # Email is a variable
# Check some stuff, exit if certain conditions not met
echo "~/test.sh 2>&1 | mailx -s\"Cool title\" $my_email" | at 7:00 PM &> /dev/null
What's interesting is that when I run schedule.sh from cron (which runs the script with sh), it works perfectly. However, when I manually run schedule.sh from the terminal (NB: I'm using tcsh), at (not mailx) sends me an email saying
Ambiguous output redirect.
I'm not sure why the shell I run schedule.sh from makes a difference, when schedule.sh is a bash script.
Here is my thinking in looking at schedule.sh. Everything within the quotation marks "~/test.sh 2>&1 | mailx -s\"Cool title\" me#email.com" should be an argument to at, and at runs that argument as a command using sh. The redirection 2>&1 | is in the style of sh for this reason.
When I remove 2>&1 and only pipe the stdout of test.sh to mailx, it does work; however, I receive 2 emails: one with stdout from mailx and another from stderr from at.
What gives? How can I make this work regardless of the shell I'm calling it from?
Thanks.
edit:
uname -o says my OS is GNU/Linux
Here is uname -a if it helps:
Linux [hostname censored] 2.6.9-89.ELlargesmp #1 SMP Mon Jun 22 12:46:58 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux
When I check the at contents using at -c, here's what I see:
#!/bin/sh
# atrun uid=xxxxx gid=xxxxx
# mail username 0
# ...
SHELL=/bin/tcsh; export SHELL
# ...
${SHELL:-/bin/sh} << `(dd if=/dev/urandom count=200 bs=1 2>/dev/null|LC_ALL=C tr -d -c '[:alnum:]')`
~/test.sh 2>&1 | mailx -s"Cool title" me#example.com
I'm having a hard time understanding the second to last line... is this going to execute using $SHELL or /bin/sh?
The command executed via at is:
~/test.sh 2>&1 | mailx -s\"Cool title\" $my_email
The behavior of at command varies from one system to another. On Linux, the command is executed using /bin/sh. In fact, on my system (Linux Mint 14), it prints a warning message:
$ echo 'printenv > at.env' | at 19:24
warning: commands will be executed using /bin/sh
On Solaris, the command is executed by the shell specified by the current value of the $SHELL environment variable. Using an account where my default shell is /bin/tcsh on Solaris 9, I get:
% echo 'printenv > at.env' | at 19:25
commands will be executed using /bin/tcsh
job 1397874300.a at Fri Apr 18 19:25:00 2014
% echo 'printenv > at.env' | env SHELL=/bin/sh at 19:28
commands will be executed using /bin/sh
job 1397874480.a at Fri Apr 18 19:28:00 2014
Given that at's behavior is inconsistent (and frankly confusing), I suggest having it execute just a single command, with any I/O redirection being performed inside that command. That's the best way to ensure that the command will be executed correctly regardless of which shell is used to execute it.
For example (untested code follows):
echo '#!/bin/bash' > tmp.bash
echo "~/test.sh 2>&1 | mailx -s\"Cool title\" $my_email" >> tmp.bash
chmod +x tmp.bash
echo "./tmp.bash" | at 7:00 PM

How to print the previous linux command's output?

The problem is:
After user enter a linux command.
How can I get the output of the first command using another command?
Note: we cannot redirect output of first command to somewhere.
Using history expansion
$ date -d "12:00"
Thu Sep 19 12:00:00 EDT 2013
$ d=$(!!)
$ echo $d
Thu Sep 19 12:00:00 EDT 2013

Copy all typed command in a linux console & their result to a file

I'm trying to make a script to automatically install programs and configure them on my Fedora 19 linux distribution.
To create it, I made a VM and I'm typing all the command manually in my "Terminal" application.
I'd like to be able to log all what I've typed and all the output (stdin & stderr & stdout if I understood it well) so I can use this log to make my script.
Is there a way to do this ?
You can use the script command to record your session:
$ script session.txt
Script started, file is session.txt
$ ls
session.txt
$ exit
Script done, file is session.txt
$ cat session.txt
Script started on Wed 31 Jul 2013 07:36:40 AM CEST
$ ls
session.txt
$ exit
Script done on Wed 31 Jul 2013 07:36:42 AM CEST

Bash: Program next execution of current script using 'at'

I want to execute a script and make it schedule the next execution. A sample would be:
#!/bin/bash
TMP=/tmp/text.txt
SCRIPT=$(readlink -f $0)
date >>$TMP
at -f $SCRIPT now + 1 minutes >>$TMP 2>&1
echo -e "\n" >>$TMP
A sample execution would do as follows:
First execution OK. Schedules to next minute
Second execution writes OK but doesn't schedule
Resulting output would be:
tue mar 5 14:34:01 CET 2013
job 15 at 2013-03-05 14:35
tue mar 5 14:35:00 CET 2013
job 16 at 2013-03-05 14:36
[now at 2013-03-05 14:38]
atq outputs nothing and I don't see any /var/at/jobs (In fact, ls /var/at* outputs nothing. There is no message in any user in /var/mail/. I'm trying on a CentOS release 5.6 x86_64
Anyone has any hint as to what may be happening?
suspectus, you have hit the point... echo $SCRIPT gives '/bin/bash'... I've manually written the full path and now it works

Resources