Script bash: Info del sistema con menú.
Anteriormente ya creamos un script que nos aportaba información simple con menú de nuestro sistema, el que vemos hoy es mucho mas completo y nos ofrecerá la siguiente información:
- Fecha y horario de uso.
- Información del sistema operativo.
- Detalles del hostname y las dns.
- Información de la red o redes.
- Quien esta conectado.
- Registro de ultimas visitas.
- Información detallada de la memoria libre y usada (ram y swap)
Ejemplo del menú…
Creamos el script.
nano infosistema.sh
Copia y pega lo siguiente…
#!/bin/bash # infosistema.sh - Menu driven shell script to get information # Linux server-desktop. # Define variables LSB=/usr/bin/lsb_release # Purpose: Display pause prompt # $1-> Message (optional) function pause(){ local message="$@" [ -z $message ] && message="Press [Enter] key to continue..." read -p "$message" readEnterKey } # Purpose - Display a menu on screen function show_menu(){ date echo "---------------------------" echo " Main Menu" echo "---------------------------" echo "1. Info del sistema operativo" echo "2. Info del hostname y dns" echo "3. Info de la red" echo "4. Quien esta conectado" echo "5. Registro de ultimas visitas" echo "6. Info de la memoria libre y usada" echo "7. Salir" } # Purpose - Display header message # $1 - message function write_header(){ local h="$@" echo "---------------------------------------------------------------" echo " ${h}" echo "---------------------------------------------------------------" } # Purpose - Get info about your operating system function os_info(){ write_header " System information " echo "Operating system : $(uname)" [ -x $LSB ] && $LSB -a || echo "$LSB command is not insalled (set \$LSB variable)" #pause "Press [Enter] key to continue..." pause } # Purpose - Get info about host such as dns, IP, and hostname function host_info(){ local dnsips=$(sed -e '/^$/d' /etc/resolv.conf | awk '{if (tolower($1)=="nameserver") print $2}') write_header " Hostname and DNS information " echo "Hostname : $(hostname -s)" echo "DNS domain : $(hostname -d)" echo "Fully qualified domain name : $(hostname -f)" echo "Network address (IP) : $(hostname -i)" echo "DNS name servers (DNS IP) : ${dnsips}" pause } # Purpose - Network inferface and routing info function net_info(){ devices=$(netstat -i | cut -d" " -f1 | egrep -v "^Kernel|Iface|lo") write_header " Network information " echo "Total network interfaces found : $(wc -w <<<${devices})" echo "*** IP Addresses Information ***" ip -4 address show echo "***********************" echo "*** Network routing ***" echo "***********************" netstat -nr echo "**************************************" echo "*** Interface traffic information ***" echo "**************************************" netstat -i pause } # Purpose - Display a list of users currently logged on # display a list of receltly loggged in users function user_info(){ local cmd="$1" case "$cmd" in who) write_header " Who is online "; who -H; pause ;; last) write_header " List of last logged in users "; last ; pause ;; esac } # Purpose - Display used and free memory info function mem_info(){ write_header " Free and used memory " free -m echo "*********************************" echo "*** Virtual memory statistics ***" echo "*********************************" vmstat echo "***********************************" echo "*** Top 5 memory eating process ***" echo "***********************************" ps auxf | sort -nr -k 4 | head -5 pause } # Purpose - Get input via the keyboard and make a decision using case..esac function read_input(){ local c read -p "Enter your choice [ 1 - 7 ] " c case $c in 1) os_info ;; 2) host_info ;; 3) net_info ;; 4) user_info "who" ;; 5) user_info "last" ;; 6) mem_info ;; 7) echo "Bye!"; exit 0 ;; *) echo "Please select between 1 to 7 choice only." pause esac } # ignore CTRL+C, CTRL+Z and quit singles using the trap trap '' SIGINT SIGQUIT SIGTSTP # main logic while true do clear show_menu # display memu read_input # wait for user input done
Guarda y cierra el archivo.
Le damos permisos.
chmod +x infosistema.sh
Lo ejecutamos…
./infosistema.sh
o bien…
bash infosistema.sh
Ya lo tenemos.
PD: Para un correcto funcionamiento del script, debes tener instalado y activo el «lsb_core» pues haremos uso del «lsb_release«.
Si no lo tienes instalado veras que es algo simple (el script avisa si lo tienes o no).
Ubuntu, Debian y derivados:
sudo apt-get update && sudo apt-get install lsb-core
Centos, Rhel y derivados:
sudo yum update && sudo yum install redhat-lsb-core
Espero te resulte útil el script, en un próximo articulo trataremos el «lsb_release» con más profundidad.