BashHistoryByTty

De Wiki LOGre
Aller à la navigation Aller à la recherche
Langue : Français

Présentation

Si comme moi vous êtes parfois appelé à travailler a plusieurs en même temps sur le même serveur et avec le même user (typiquement tout ce qui nécessite root), vous avez déjà perdu des lignes d'historique.

Cette modification du .bashrc vous donne :

  • Enregistrement instantané dans le fichier d'historique
  • Horodatage de l'historique
  • Enregistrement ad vitam de l'historique (pas de limite au nombre de lignes)
  • Enregistrement par tty, afin de pouvoir trier les suites de commandes du même shell
  • Visualisation de l'historique par tty
  • Visualisation de l'historique de tous les ttys à la fois (pour une recherche efficace)

Pré-requis

  • Un serveur web *nix avec :
    • bash
    • cut
    • tty

Normalement vous avez tout ça de base.

Utilisation

<syntaxhighlight> user@server:~/$ history pts1 (pts1) 141 [2019-11-19 10:20:02] tty (pts1) 142 [2019-11-19 10:20:05] echo pts 1 (pts1) 143 [2019-11-19 10:20:13] echo pts1-1


user@server:~/$ history pts2 (pts2) 49 [2019-11-19 10:19:57] tty (pts2) 50 [2019-11-19 10:19:59] echo pts2 (pts2) 51 [2019-11-19 10:20:10] echo pts2-2


user@server:~/$ history all (pts2) 49 [2019-11-19 10:19:57] tty (pts2) 50 [2019-11-19 10:19:59] echo pts2 (pts1) 141 [2019-11-19 10:20:02] tty (pts1) 142 [2019-11-19 10:20:05] echo pts 1 (pts2) 51 [2019-11-19 10:20:10] echo pts2-2 (pts1) 143 [2019-11-19 10:20:13] echo pts1-1

</syntaxhighlight>

Installation

  • Ajouter les lignes suivantes a votre .bashrc
  • Recharger le fichier avec : . ~/.bashrc

<syntaxhighlight lang="bash">

export HISTCONTROL=ignoreboth

  1. append to the history file, don't overwrite it

shopt -s histappend

export HISTFILESIZE= export HISTSIZE= export TERMINAL=`tty | cut -c6-8,10-` export HISTTIMEFORMAT="[%F %T] "

  1. Change the file location because certain bash sessions truncate .bash_history file upon close.
  2. http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login

export HISTFILE=~/.bash_eternal_history.$TERMINAL

  1. Force prompt to write history after every command.
  2. http://superuser.com/questions/20900/bash-history-loss

PROMPT_COMMAND="\history -a; $PROMPT_COMMAND"

function myhistory(){

 mytty=`tty | cut -c6-8,10-`
 if [ $# -eq 0 ]
 then
   \history
 elif [ $1 = 'all' ]
 then
   ls -1 ~/.bash_eternal_history.* | while read line
   do
     pts="${line: -4}"
     myhistory $pts
   done | sort -k3
 elif [ -e ~/.bash_eternal_history.$1 ]
 then
   \history -a
   \history -c
   \history -r ~/.bash_eternal_history.$1
   \history | while read line ; do printf "($1) %s\n" "$line" ; done
   \history -c
   \history -r ~/.bash_eternal_history.$mytty
 else
   \history $*
 fi

}

alias history=myhistory

</syntaxhighlight>