feedburner

Lorem ipsum dolor sit amet,
consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua.

Script para grabar lo que se hace en un shell ( ssh )

Etiquetas:

Por mi trabajo debo documentar las acciones que ejecuto en los hosts de los clientes con total fidelidad. Hace un tiempo estuve buscando alguna herramienta tipo gnome-terminal ( o consola virtual ) con la posibilidad de guardar el log de toda la entrada/salida que realizara dentro de dicha terminal. Esta funcionalidad me sería muy útil no tan solo para poder realizar la documentación que quiero ofrecer, sino también para poder disponer de forma rápida de ejemplos prácticos de ejecución de comandos vía terminal o como log de algún conjunto de acciones particulares. De momento no he encontrado ningún terminal con esa funcionalidad ( tampoco he hecho una búsqueda exahustiva ) pero si que encontré la aplicación propia de bash script que permite hacer algo parecido. Mediante un shell que he montado, automáticamente se graba toda la entrada/salida de lo que ejecuto a través de un ssh, guardándome el log en un directorio predeterminado y organizado por hosts accedidos. El nombre del fichero de log me indica con que usuario realicé el ssh y en que momento. Os dejo aquí el script ( en mis ejemplos se llama fer_ssh2.sh ) por si os es útil o por si os lo puede ser con las modificaciones que creáis oportunas. También adjunto un ejemplo de salida de ejecuciones así como el aspecto del directorio de log para que sirva de ejemplo de lo que he contado.


#!/bin/bash
CADENA=$1
DIRBASE=$HOME/logs_ssh2
FECHA=`date +%Y%m%d-%H.%M.%S`
ELHOST=${CADENA#*@}
ELUSER=${CADENA%@*}
DIRLOG=$DIRBASE/$ELHOST
if [ -d $DIRLOG ]; then
# si existe no hacemos nada - TODO: mirar si podemos negar el if
echo "1" > /dev/null
#el directorio existe, seguimos
else
mkdir -p $DIRLOG
fi
LOG=$DIRLOG/$FECHA-$ELUSER.log
echo "**************************************************"
echo "* HACIENDO SSH CONTRA $1"
echo "* Guardando log en $LOG"
echo "**************************************************"
script -c "ssh -X $1" $LOG
exit


  • Salida real del terminal donde lanzo fer_ssh2.sh ( contra mi propio host vía nombre máquina )


dballester@nebuchadnezzar:~$ fer_ssh2.sh dballester@nebuchadnezzar
**************************************************
* HACIENDO SSH CONTRA dballester@nebuchadnezzar
* Guardando log en /home/dballester/logs_ssh2/nebuchadnezzar/20071220-12.19.26-dballester.log
**************************************************
Script iniciado; el fichero es /home/dballester/logs_ssh2/nebuchadnezzar/20071220-12.19.26-dballester.log
The authenticity of host 'nebuchadnezzar (127.0.1.1)' can't be established.
RSA key fingerprint is af:6e:94:98:83:e1:d3:22:a9:c2:cf:d7:28:3a:59:c3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'nebuchadnezzar' (RSA) to the list of known hosts.
dballester@nebuchadnezzar's password:
Linux nebuchadnezzar 2.6.22-14-generic #1 SMP Sun Oct 14 23:05:12 GMT 2007 i686

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
You have mail.
Last login: Thu Dec 20 11:59:12 2007 from localhost
dballester@nebuchadnezzar:~$ hostname
nebuchadnezzar
dballester@nebuchadnezzar:~$ df -h
S.ficheros Tamaño Usado Disp Uso% Montado en
/dev/sda6 4,6G 3,9G 536M 88% /
varrun 502M 112K 502M 1% /var/run
varlock 502M 0 502M 0% /var/lock
udev 502M 108K 502M 1% /dev
devshm 502M 0 502M 0% /dev/shm
lrm 502M 34M 468M 7% /lib/modules/2.6.22-14-generic/volatile
/dev/mapper/data-homes
30G 16G 13G 56% /home
/dev/sda1 76M 41M 32M 57% /boot
/dev/mapper/data-oracle11g
7,9G 5,9G 1,6G 79% /u01
/dev/scd0 7,6G 7,6G 0 100% /media/cdrom0
dballester@nebuchadnezzar:~$ exit
logout
Connection to nebuchadnezzar closed.
Script terminado; el fichero es /home/dballester/logs_ssh2/nebuchadnezzar/20071220-12.19.26-dballester.log
dballester@nebuchadnezzar:~$



  • Salida real del terminal donde lanzo fer_ssh2.sh ( contra mi propio host vía localhost )

dballester@nebuchadnezzar:~$ fer_ssh2.sh dballester@localhost
**************************************************
* HACIENDO SSH CONTRA dballester@localhost
* Guardando log en /home/dballester/logs_ssh2/localhost/20071220-12.21.35-dballester.log
**************************************************
Script iniciado; el fichero es /home/dballester/logs_ssh2/localhost/20071220-12.21.35-dballester.log
dballester@localhost's password:
Linux nebuchadnezzar 2.6.22-14-generic #1 SMP Sun Oct 14 23:05:12 GMT 2007 i686

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
You have mail.
Last login: Thu Dec 20 12:19:33 2007 from nebuchadnezzar
dballester@nebuchadnezzar:~$ ifconfig | grep -A2 Ether
eth0 Link encap:Ethernet HWaddr 00:14:22:A2:5E:86
inet addr:172.31.152.20 Bcast:172.31.152.255 Mask:255.255.255.0
inet6 addr: fe80::214:22ff:fea2:5e86/64 Scope:Link
dballester@nebuchadnezzar:~$ mii-tool
SIOCGMIIPHY on 'eth0' failed: Operation not permitted
SIOCGMIIPHY on 'eth1' failed: Operation not permitted
SIOCGMIIPHY on 'eth2' failed: Operation not permitted
SIOCGMIIPHY on 'eth3' failed: Operation not permitted
SIOCGMIIPHY on 'eth4' failed: Operation not permitted
SIOCGMIIPHY on 'eth5' failed: Operation not permitted
SIOCGMIIPHY on 'eth6' failed: Operation not permitted
SIOCGMIIPHY on 'eth7' failed: Operation not permitted
no MII interfaces found
dballester@nebuchadnezzar:~$ sudo mii-tool
[sudo] password for dballester:
eth0: no autonegotiation, 10baseT-HD, link ok
SIOCGMIIPHY on 'eth1' failed: Operation not supported
dballester@nebuchadnezzar:~$




  • Aspecto de los directorios de log

dballester@nebuchadnezzar:~$ ls -lR logs_ssh2
logs_ssh2:
total 36
drwxr-xr-x 2 dballester dballester 4096 2007-12-20 12:23 localhost
drwxr-xr-x 2 dballester dballester 4096 2007-12-20 12:19 nebuchadnezzar

logs_ssh2/localhost:
total 16
-rw-r--r-- 1 dballester dballester 4096 2007-12-20 12:22 20071220-12.21.35-dballester.log
-rw-r--r-- 1 dballester dballester 131 2007-12-20 12:23 20071220-12.23.10-dballester.log

logs_ssh2/nebuchadnezzar:
total 4
-rw-r--r-- 1 dballester dballester 1795 2007-12-20 12:19 20071220-12.19.26-dballester.log
dballester@nebuchadnezzar:~$