bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Saving command history for non-interactive shell


From: dethrophes
Subject: Re: Saving command history for non-interactive shell
Date: Mon, 19 Mar 2012 15:58:37 +0100
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20120312 Thunderbird/11.0

Am 19.03.2012 13:39, schrieb Greg Wooledge:
On Fri, Mar 16, 2012 at 06:15:35PM -0400, Chet Ramey wrote:
There is nothing stopping you from using history in a non-interactive
shell -- it's just not enabled by default.

Turn on history with `set -o history' and set HISTFILE and HISTSIZE as you
like.  You can probably set some of the right variables in .ssh/environment
and set BASH_ENV to a file that will run the commands you want.
The problem is, that doesn't actually work.

imadev:~$ ssh localhost bash<<'EOF'>  set -o history
HISTFILE=~/.bash_history
HISTFILESIZE=500
echo hello world
EOF
wooledg@localhost's password:
hello world
imadev:~$ tail -2 .bash_history
rm statistical.tcl.rej
less sched.tcl.rej

I blame this part of the documentation, although perhaps I should be
looking at the code instead:

   When an interactive shell exits, the last $HISTSIZE lines
   are copied from the history list to $HISTFILE.

I read that as "the HISTFILE doesn't get updated when a NON-interactive
shell exits".

This one works, though:

imadev:~$ ssh localhost bash<<'EOF'
set -o history
HISTFILE=~/.bash_history
HISTFILESIZE=500
echo hello world
history -a
EOF
wooledg@localhost's password:
hello world
imadev:~$ tail -6 .bash_history
rm statistical.tcl.rej
less sched.tcl.rej
HISTFILE=~/.bash_history
HISTFILESIZE=500
echo hello world
history -a

However, since the original intent was "I want to log commands that are
launched through ssh, without modifying what the client sends", it's not
clear to me how to wrap all of that prefix and postfix code around the
client's commands.




have you tried something like this

create a file called /usr/bin/ssh_gatekeeper.sh make it executable
put this into it
#!/bin/bash
## Disconnect clients who try to quit the script (Ctrl-c)
trap jail INT
jail()
 {
   kill -9 $PPID
   exit 0
 }

[ -n "$SSH_ORIGINAL_COMMAND" ] || exit 0
case "$SSH_ORIGINAL_COMMAND" in
  bash*)
    /bin/bash --init-file /usr/logssh/bin/logbashrc
    ;;
  *) $SSH_ORIGINAL_COMMAND ;;
esac
exit 0

in /etc/sshd_config add
ForceCommand /usr/bin/ssh_gatekeeper.sh

in /usr/logssh/bin/logbashrc or somewhere

set -o history
HISTFILE=~/.bash_history
HISTFILESIZE=500
trap 'history -a' EXIT SIGINT



Or something like that it should work I think, or at least point in a direction.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]