help-bash
[Top][All Lists]
Advanced

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

Re: how to log bash commands (like with history or 'script' utility, bu


From: Alexander Kolodziej
Subject: Re: how to log bash commands (like with history or 'script' utility, but better)
Date: Mon, 5 Dec 2022 18:51:23 +0100

This is cool!
1. didn't know the history saved comments like that!
2. using history in PROMPT_COMMAND, slick!

So i adapted this to suit my own needs:)
a) i make the function log to a separate file though
b) i actually use the history timestamp to log the time when the command
was started (because long running commands would get their timestamps wrong
if the function logged now() as timestamp, since that gets called after the
command has exited)
c) i must have "env_keep+=..." in sudoers.d to keep my own variables that
are set and used to identify the actual person
d) the function logs into files per user, but should not log for root when
sudo was used, so must look at stuff like $SUDO_USER and such
e) CAVEAT: commands are logged twice when logging out and in again (because
the the function is run again even before anything is executed when logging
in, so to speak) - no biggie
f) would be nice to have this in /etc/profile.d/, but those files are not
read when using 'sudo', or 'screen', and don't want to edit the global
/etc/bash.bashrc on servers. i guess i'll have to put in in ~/.bashrc (or
scripts sourced by it) somehow for all users on all servers:) but that's
doable.

I still wish that bash could invoke some function from some variable right
when i press Enter at the console, but this is a nice solution indeed!
Now i don't have to install some console-capturing software that streams my
sessions online in 4K by default hehe:)

Thanks!

  wbr / Alex

On Wed, Nov 30, 2022 at 1:42 AM Dennis Williamson <
dennistwilliamson@gmail.com> wrote:

>
>
> On Tue, Nov 29, 2022 at 5:37 PM Alexander Kolodziej <
> alexander.kolodziej@gmail.com> wrote:
>
>> Hi!
>>
>> I would like to log all (interactive) commands that are entered at the
>> prompt during an ssh/console session. Like this
>>
>> * the command (and its arguments)
>> * time/date
>> * who executed (just $USER will not suffice, because accounts are shared
>> in
>> this case, and people are differentiated by their ssh key fingerprints,
>> which can be found as variables in the environment)
>> * and would be nice to have $PWD logged as well (where that command was
>> executed)
>>
>> The bash history (with HISTTIMEFORMAT) takes care of the first two, but
>> not
>> the latter two.
>>
>> I was hoping that i could do something with $PS0 and $BASH_COMMAND, but it
>> looked like BASH_COMMAND was set to the previous command (it lagged behind
>> so to speak) when PS0 is evaluated.
>>
>> I would really like to avoid to download stuff from github and compile. I
>> want something that gets updated with the rest of the packages when 'apt
>> upgrade' is run.
>>
>> The 'script' utility? Well maybe.But it looks like that will try to log
>> everything entered in e.g. 'vi' as well, and from what i read - ot
>> handle that very good.
>> Also, doesnt look like i can log specific/custom env variables with that.
>>
>> It just really seems that there should be some simple way to do this
>> within
>> bash itself.
>> Isnt there? :)
>>
>>   wbr / Alex
>>
>> ps. Perhaps some readline magic somehow??
>>
>
> One of my functions posted as Stack Overflow answers here:
>
> https://stackoverflow.com/a/960684/26428
>
> or others of mine at the same question could probably be adapted for your
> needs.
>
> --
> Visit serverfault.com to get your system administration questions
> answered.
>


reply via email to

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