Reputation: 3878
We need to log the command and its output. For example if I run the following command. I need to log it into a text file.
cat script.sh
find / -name sshd_config -print
/etc/ssh/sshd_config
cat log.out --should be as below
<server_name>$ find / -name sshd_config -print
/etc/ssh/sshd_config
If I use set-x in the script, I can log the command but it gives too much information. Is there any alternate way to use in the scripts?
Upvotes: 0
Views: 133
Reputation: 246764
I was curious and followed up: here's an expect
script that makes a shell script look interactive:
#!/usr/bin/env expect
set script [lindex $argv 0]
if { ! [file readable $script]} {
error "cannot read $script"
}
set fid [open $script r]
set lines [split [read -nonewline $fid] \n]
close $fid
set shebang [lindex $lines 0]
if {[string match {#!*} $shebang]} {
spawn -noecho {*}[string range $shebang 2 end]
} else {
spawn -noecho $env(SHELL)
}
foreach line $lines {
# skip blank lines
if {[string trim $line] eq ""} continue
# skip comments
if {[string match {#*} [string trim $line]]} continue
send -- "$line\r"
}
# send Ctrl-D to end shell session
send -- \x04
expect eof
Save as something like interactivize
, then:
$ cat test.sh
date
getent passwd $USER
echo Hello World
$ interactivize test.sh
date
getent passwd $USER
echo Hello World
[myprompt]$ date
Thu Apr 9 18:29:31 EDT 2015
[myprompt]$ getent passwd $USER
jackman:x:1001:1001:Glenn Jackman:/home/jackman:/usr/local/bin/fish
[myprompt]$ echo Hello World
Hello World
[myprompt]$ exit
exit
Upvotes: 1
Reputation: 2035
If you need to it for security audits I would have a look at GateOne.
It supports full recording of commands and output, for a short demo see: https://youtu.be/gnVohdlZXVY?t=3m13s
Upvotes: 0