| 1 | #!/bin/bash |
|---|
| 2 | #/** |
|---|
| 3 | #@file Inventory.lib |
|---|
| 4 | #@brief Librería o clase Inventory |
|---|
| 5 | #@class Inventory |
|---|
| 6 | #@brief Funciones para recogida de datos de inventario de hardware y software de los clientes. |
|---|
| 7 | #@version 1.1.0 |
|---|
| 8 | #@warning License: GNU GPLv3+ |
|---|
| 9 | #*/ |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #/** |
|---|
| 13 | # ogGetArch |
|---|
| 14 | #@brief Devuelve el tipo de arquitectura del cliente. |
|---|
| 15 | #@return str_arch - Arquitectura (i386 para 32 bits, x86_64 para 64 bits). |
|---|
| 16 | #@version 0.9.2 - Primera versión para OpenGnSys. |
|---|
| 17 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 18 | #@date 2010-07-17 |
|---|
| 19 | #*/ |
|---|
| 20 | function ogGetArch () |
|---|
| 21 | { |
|---|
| 22 | if [ "$*" == "help" ]; then |
|---|
| 23 | ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME => x86_64" |
|---|
| 24 | return |
|---|
| 25 | fi |
|---|
| 26 | |
|---|
| 27 | [ -d /lib64 ] && echo "x86_64" || echo "i386" |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | #/** |
|---|
| 32 | # ogGetOsType int_ndisk int_npartition |
|---|
| 33 | #@brief Devuelve el tipo del sistema operativo instalado. |
|---|
| 34 | #@param int_ndisk nº de orden del disco |
|---|
| 35 | #@param int_npartition nº de orden de la partición |
|---|
| 36 | #@return OSType - Tipo de sistema operativo. |
|---|
| 37 | #@see ogGetOsVersion |
|---|
| 38 | #*/ ## |
|---|
| 39 | function ogGetOsType () |
|---|
| 40 | { |
|---|
| 41 | # Si se solicita, mostrar ayuda. |
|---|
| 42 | if [ "$*" == "help" ]; then |
|---|
| 43 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
|---|
| 44 | "$FUNCNAME 1 2 => Linux" |
|---|
| 45 | return |
|---|
| 46 | fi |
|---|
| 47 | ogGetOsVersion "$@" | cut -sf1 -d: |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | #/** |
|---|
| 52 | # ogGetOsUuid int_ndisk int_nfilesys |
|---|
| 53 | #@brief Devuelve el UUID del sistema operativo instalado en un sistema de archivos. |
|---|
| 54 | #@param int_ndisk nº de orden del disco |
|---|
| 55 | #@param int_nfilesys nº de orden de la partición |
|---|
| 56 | #@return str_uuid - UUID del sistema operativo. |
|---|
| 57 | #@exception OG_ERR_FORMAT Formato incorrecto. |
|---|
| 58 | #@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositiv |
|---|
| 59 | #@version 1.1.0 - Primera versión para OpenGnsys |
|---|
| 60 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 61 | #@date 2015-09-09 |
|---|
| 62 | #*/ ## |
|---|
| 63 | function ogGetOsUuid () |
|---|
| 64 | { |
|---|
| 65 | # Variables locales. |
|---|
| 66 | local MNTDIR |
|---|
| 67 | # Si se solicita, mostrar ayuda. |
|---|
| 68 | if [ "$*" == "help" ]; then |
|---|
| 69 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \ |
|---|
| 70 | "$FUNCNAME 1 2 => 540e47c6-8e78-4178-aa46-042e4803fb16" |
|---|
| 71 | return |
|---|
| 72 | fi |
|---|
| 73 | # Error si no se reciben 2 parametros. |
|---|
| 74 | [ $# = 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
|---|
| 75 | |
|---|
| 76 | # Montar la particion, si no lo estaba previamente. |
|---|
| 77 | MNTDIR=$(ogMount $1 $2) || return $? |
|---|
| 78 | |
|---|
| 79 | # Obtener UUID según el tipo de sistema operativo. |
|---|
| 80 | case "$(ogGetOsType $1 $2)" in |
|---|
| 81 | Linux) |
|---|
| 82 | # Leer el UUID del sistema de ficheros raíz o el fichero de identificador. |
|---|
| 83 | findmnt -no UUID $MNTDIR 2>/dev/null || cat $MNTDIR/etc/machine-id 2>/dev/null |
|---|
| 84 | ;; |
|---|
| 85 | Windows) |
|---|
| 86 | # Leer identificador en clave de registro. |
|---|
| 87 | ogGetRegistryValue $MNTDIR SOFTWARE '\Microsoft\Cryptography\MachineGuid' 2>/dev/null |
|---|
| 88 | ;; |
|---|
| 89 | esac |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | #/** |
|---|
| 94 | # ogGetOsVersion int_ndisk int_nfilesys |
|---|
| 95 | #@brief Devuelve la versión del sistema operativo instalado en un sistema de archivos. |
|---|
| 96 | #@param int_ndisk nº de orden del disco |
|---|
| 97 | #@param int_nfilesys nº de orden de la partición |
|---|
| 98 | #@return OSType:OSVersion - tipo y versión del sistema operativo. |
|---|
| 99 | #@note OSType = { Android, BSD, GrubLoader, Hurd, Linux, MacOS, Solaris, Windows, WinLoader } |
|---|
| 100 | #@note Requisitos: awk, head, chroot |
|---|
| 101 | #@exception OG_ERR_FORMAT Formato incorrecto. |
|---|
| 102 | #@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositiv |
|---|
| 103 | #@exception OG_ERR_PARTITION Fallo al montar el sistema de archivos. |
|---|
| 104 | #@version 0.9 - Primera versión para OpenGnSys |
|---|
| 105 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 106 | #@date 2009-09-15 |
|---|
| 107 | #@version 1.0.4 - Incluir tipos BSD, MacOS y Solaris. |
|---|
| 108 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 109 | #@date 2012-06-29 |
|---|
| 110 | #@version 1.0.5 - Incluir tipos GrubLoader, Hurd y WinLoader, leer por defecto fichero /etc/os-release. |
|---|
| 111 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 112 | #@date 2013-10-07 |
|---|
| 113 | #@version 1.0.6 - Detectar GrubLoader al final y sistemas basados en EFI. |
|---|
| 114 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 115 | #@date 2014-08-27 |
|---|
| 116 | #*/ ## |
|---|
| 117 | function ogGetOsVersion () |
|---|
| 118 | { |
|---|
| 119 | # Variables locales. |
|---|
| 120 | local MNTDIR TYPE DISTRIB VERSION IS64BIT FILE |
|---|
| 121 | # Si se solicita, mostrar ayuda. |
|---|
| 122 | if [ "$*" == "help" ]; then |
|---|
| 123 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \ |
|---|
| 124 | "$FUNCNAME 1 2 => Linux:Ubuntu precise (12.04 LTS) 64 bits" |
|---|
| 125 | return |
|---|
| 126 | fi |
|---|
| 127 | # Error si no se reciben 2 parametros. |
|---|
| 128 | [ $# = 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
|---|
| 129 | |
|---|
| 130 | # Montar la particion, si no lo estaba previamente. |
|---|
| 131 | MNTDIR=$(ogMount $1 $2) || return $? |
|---|
| 132 | |
|---|
| 133 | # Buscar tipo de sistema operativo. |
|---|
| 134 | # Para GNU/Linux: leer descripción. |
|---|
| 135 | TYPE="Linux" |
|---|
| 136 | FILE="$MNTDIR/etc/os-release" |
|---|
| 137 | [ -r $FILE ] && VERSION="$(awk -F= '$1~/PRETTY_NAME/ {gsub(/\"/,"",$2); print $2}' $FILE)" |
|---|
| 138 | # Si no se puede obtener, buscar en ficheros del sistema. |
|---|
| 139 | if [ -z "$VERSION" ]; then |
|---|
| 140 | FILE="$MNTDIR/etc/lsb-release" |
|---|
| 141 | [ -r $FILE ] && VERSION="$(awk -F= '$1~/DESCRIPTION/ {gsub(/\"/,"",$2); print $2}' $FILE)" |
|---|
| 142 | for DISTRIB in redhat SuSE mandrake gentoo; do |
|---|
| 143 | FILE="$MNTDIR/etc/${DISTRIB}-release" |
|---|
| 144 | [ -r $FILE ] && VERSION="$(head -1 $FILE)" |
|---|
| 145 | done |
|---|
| 146 | FILE="$MNTDIR/etc/arch-release" |
|---|
| 147 | [ -r $FILE ] && VERSION="Arch Linux" |
|---|
| 148 | FILE="$MNTDIR/etc/slackware-version" |
|---|
| 149 | [ -r $FILE ] && VERSION="Slackware $(cat $FILE)" |
|---|
| 150 | fi |
|---|
| 151 | # Si no se encuentra, intentar ejecutar "lsb_release". |
|---|
| 152 | [ -z "$VERSION" ] && VERSION=$(chroot $MNTDIR lsb_release -d 2>/dev/null | awk -F":\t" '{print $2}') |
|---|
| 153 | # Comprobar Linux de 64 bits. |
|---|
| 154 | [ -n "$VERSION" ] && [ -e $MNTDIR/lib64 ] && IS64BIT="$MSG_64BIT" |
|---|
| 155 | # Para Android, leer fichero de propiedades. |
|---|
| 156 | if [ -z "$VERSION" ]; then |
|---|
| 157 | TYPE="Android" |
|---|
| 158 | FILE="$MNTDIR/android*/system/build.prop" |
|---|
| 159 | [ -r $FILE ] && VERSION="Android $(awk -F= '$1~/(product.brand|build.version.release)/ {print $2}' $FILE | tr '\n' ' ')" |
|---|
| 160 | [ -e $MNTDIR/lib64 ] && IS64BIT="$MSG_64BIT" |
|---|
| 161 | fi |
|---|
| 162 | # Para GNU/Hurd, comprobar fichero de inicio (basado en os-prober). |
|---|
| 163 | if [ -z "$VERSION" ]; then |
|---|
| 164 | TYPE="Hurd" |
|---|
| 165 | FILE="$MNTDIR/hurd/init" |
|---|
| 166 | [ -r $FILE ] && VERSION="GNU/Hurd" |
|---|
| 167 | fi |
|---|
| 168 | # Para Windows: leer la version del registro. |
|---|
| 169 | if [ -z "$VERSION" ]; then |
|---|
| 170 | TYPE="Windows" |
|---|
| 171 | FILE="$(ogGetHivePath $MNTDIR SOFTWARE)" |
|---|
| 172 | if [ -n "$FILE" ]; then |
|---|
| 173 | # Nuevo método más rápido para acceder al registro de Windows.. |
|---|
| 174 | VERSION=$(echo $(hivexsh << EOT 2>/dev/null |
|---|
| 175 | load $FILE |
|---|
| 176 | cd \Microsoft\Windows NT\CurrentVersion |
|---|
| 177 | lsval ProductName |
|---|
| 178 | lsval ReleaseId |
|---|
| 179 | EOT |
|---|
| 180 | )) |
|---|
| 181 | [ -n "$(reglookup -H -p "Microsoft/Windows/CurrentVersion/ProgramW6432Dir" "$FILE" 2>/dev/null)" ] && IS64BIT="$MSG_64BIT" |
|---|
| 182 | if [ -z "$VERSION" ]; then |
|---|
| 183 | # Compatibilidad con métrodo antiguo y más lento de acceder al registro. |
|---|
| 184 | VERSION=$(ogGetRegistryValue $MNTDIR software '\Microsoft\Windows NT\CurrentVersion\ProductName' 2>/dev/null) |
|---|
| 185 | [ -n "$(ogGetRegistryValue $MNTDIR software '\Microsoft\Windows\CurrentVersion\ProgramW6432Dir' 2>/dev/null)" ] && IS64BIT="$MSG_64BIT" |
|---|
| 186 | fi |
|---|
| 187 | fi |
|---|
| 188 | fi |
|---|
| 189 | # Para cargador Windows: buscar versión en fichero BCD (basado en os-prober). |
|---|
| 190 | if [ -z "$VERSION" ]; then |
|---|
| 191 | TYPE="WinLoader" |
|---|
| 192 | FILE="$(ogGetPath $MNTDIR/boot/bcd)" |
|---|
| 193 | [ -z "$FILE" ] && FILE="$(ogGetPath $MNTDIR/EFI/Microsoft/boot/bcd)" |
|---|
| 194 | if [ -n "$FILE" ]; then |
|---|
| 195 | for DISTRIB in "Windows Recovery" "Windows Boot"; do |
|---|
| 196 | if grep -aqs "$(echo "$DISTRIB" | sed 's/./&./g')" $FILE; then |
|---|
| 197 | VERSION="$DISTRIB loader" |
|---|
| 198 | fi |
|---|
| 199 | done |
|---|
| 200 | fi |
|---|
| 201 | fi |
|---|
| 202 | # Para macOS: detectar kernel y completar con fichero plist de información del sistema. |
|---|
| 203 | if [ -z "$VERSION" ]; then |
|---|
| 204 | TYPE="MacOS" |
|---|
| 205 | # Kernel de Mac OS (no debe ser fichero de texto). |
|---|
| 206 | FILE="$MNTDIR/mach_kernel" |
|---|
| 207 | if [ -z "$(file -b $FILE | grep 'text')" ]; then |
|---|
| 208 | # Obtener tipo de kernel. |
|---|
| 209 | [ -n "$(file -b $FILE | grep 'Mach-O')" ] && VERSION="macOS" |
|---|
| 210 | [ -n "$(file -b $FILE | grep 'Mach-O 64-bit')" ] && IS64BIT="$MSG_64BIT" |
|---|
| 211 | # Datos de configuración de versión de Mac OS. |
|---|
| 212 | FILE="$MNTDIR/System/Library/CoreServices/SystemVersion.plist" |
|---|
| 213 | [ -r $FILE ] && VERSION=$(awk -F"[<>]" ' |
|---|
| 214 | /ProductName/ {getline;s=$3} |
|---|
| 215 | /ProductVersion/ {getline;v=$3} |
|---|
| 216 | END {print s,v}' $FILE) |
|---|
| 217 | # Datos de recuperación de macOS. |
|---|
| 218 | FILE="$MNTDIR/com.apple.recovery.boot" |
|---|
| 219 | [ -r $FILE -a -n "$VERSION" ] && VERSION="$VERSION recovery" |
|---|
| 220 | fi |
|---|
| 221 | fi |
|---|
| 222 | # Para FreeBSD: obtener datos del Kernel. |
|---|
| 223 | ### TODO Revisar solución. |
|---|
| 224 | if [ -z "$VERSION" ]; then |
|---|
| 225 | TYPE="BSD" |
|---|
| 226 | FILE="$MNTDIR/boot/kernel/kernel" |
|---|
| 227 | if [ -r $FILE ]; then |
|---|
| 228 | VERSION="$(strings $FILE|awk '/@.*RELEASE/ {sub(/@\(#\)/,""); print $1,$2}')" |
|---|
| 229 | [ -n "$(file -b $FILE | grep 'x86-64')" ] && IS64BIT="$MSG_64BIT" |
|---|
| 230 | fi |
|---|
| 231 | fi |
|---|
| 232 | # Para Solaris: leer el fichero de versión. |
|---|
| 233 | ### TODO Revisar solución. |
|---|
| 234 | if [ -z "$VERSION" ]; then |
|---|
| 235 | TYPE="Solaris" |
|---|
| 236 | FILE="$MNTDIR/etc/release" |
|---|
| 237 | [ -r $FILE ] && VERSION="$(head -1 $FILE)" |
|---|
| 238 | fi |
|---|
| 239 | # Para cargador GRUB, comprobar fichero de configuración. |
|---|
| 240 | if [ -z "$VERSION" ]; then |
|---|
| 241 | TYPE="GrubLoader" |
|---|
| 242 | for FILE in $MNTDIR/{,boot/}grub/menu.lst; do |
|---|
| 243 | [ -r $FILE ] && VERSION="GRUB Loader" |
|---|
| 244 | done |
|---|
| 245 | for FILE in $MNTDIR/{,boot/}{grub{,2},EFI/*}/grub.cfg; do |
|---|
| 246 | [ -r $FILE ] && VERSION="GRUB2 Loader" |
|---|
| 247 | done |
|---|
| 248 | fi |
|---|
| 249 | |
|---|
| 250 | # Mostrar resultado y salir sin errores. |
|---|
| 251 | [ -n "$VERSION" ] && echo "$TYPE:$VERSION $IS64BIT" |
|---|
| 252 | return 0 |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | #/** |
|---|
| 257 | # ogGetSerialNumber |
|---|
| 258 | #@brief Obtiene el nº de serie del cliente. |
|---|
| 259 | #@version 1.1.0 - Primeras versión con OpenGnsys |
|---|
| 260 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 261 | #@date 2015-06-08 |
|---|
| 262 | function ogGetSerialNumber () |
|---|
| 263 | { |
|---|
| 264 | # Si se solicita, mostrar ayuda. |
|---|
| 265 | if [ "$*" == "help" ]; then |
|---|
| 266 | ogHelp "$FUNCNAME" "$FUNCNAME" |
|---|
| 267 | return |
|---|
| 268 | fi |
|---|
| 269 | |
|---|
| 270 | # Obtener nº de serie (ignorar los no especificados) |
|---|
| 271 | dmidecode -s system-serial-number | egrep -vi "(^[ 0]+$|not specified|filled by o.e.m.)" |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | #/** |
|---|
| 276 | # ogIsEfiActive |
|---|
| 277 | #@brief Comprueba si el sistema tiene activo el arranque EFI. |
|---|
| 278 | #*/ ## |
|---|
| 279 | function ogIsEfiActive () |
|---|
| 280 | { |
|---|
| 281 | test -d /sys/firmware/efi |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | #/** |
|---|
| 286 | # ogListHardwareInfo |
|---|
| 287 | #@brief Lista el inventario de hardware de la máquina cliente. |
|---|
| 288 | #@return TipoDispositivo:Modelo (por determinar) |
|---|
| 289 | #@warning Se ignoran los parámetros de entrada. |
|---|
| 290 | #@note TipoDispositivo = { bio, boa, bus, cha, cdr, cpu, dis, fir, mem, mod, mul, net, sto, usb, vga } |
|---|
| 291 | #@note Requisitos: dmidecode, lshw, awk |
|---|
| 292 | #@version 0.1 - Primeras pruebas con OpenGnSys |
|---|
| 293 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 294 | #@date 2009-07-28 |
|---|
| 295 | #@version 1.1.0 - Incluir nuevos componentes al inventario. |
|---|
| 296 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 297 | #@date 2014-04-23 |
|---|
| 298 | #*/ ## |
|---|
| 299 | function ogListHardwareInfo () |
|---|
| 300 | { |
|---|
| 301 | # Si se solicita, mostrar ayuda. |
|---|
| 302 | if [ "$*" == "help" ]; then |
|---|
| 303 | ogHelp "$FUNCNAME" "$FUNCNAME" |
|---|
| 304 | return |
|---|
| 305 | fi |
|---|
| 306 | |
|---|
| 307 | # Recopilación de dispositivos procesando la salida de \c lshw |
|---|
| 308 | ogEcho info "$MSG_HARDWAREINVENTORY}" |
|---|
| 309 | echo "cha=$(dmidecode -s chassis-type)" | grep -v "Other" |
|---|
| 310 | [ -e /sys/firmware/efi ] && echo "boo=UEFI" || echo "boo=BIOS" |
|---|
| 311 | lshw | awk 'BEGIN {type="mod";} |
|---|
| 312 | /product:/ {sub(/ *product: */,""); prod=$0;} |
|---|
| 313 | /vendor:/ {sub(/ *vendor: */,""); vend=$0;} |
|---|
| 314 | /version:/ {sub(/ *version: */,"v.");vers=$0;} |
|---|
| 315 | /size:/ {size=$2;} |
|---|
| 316 | /clock:/ {clock=$2;} |
|---|
| 317 | /slot:/ {sub(/ *slot: */,""); slot=$0;} |
|---|
| 318 | /\*-/ {if (type=="mem"){ |
|---|
| 319 | if (size!=""){ |
|---|
| 320 | numbank++; |
|---|
| 321 | print type"="vend,prod,size,clock" ("slot")";} |
|---|
| 322 | }else{ |
|---|
| 323 | if (type=="totalmem"){ |
|---|
| 324 | if (size!=""){ |
|---|
| 325 | totalmemory="mem="size;} |
|---|
| 326 | }else{ |
|---|
| 327 | if (type!="" && prod!=""){ |
|---|
| 328 | if (prod=="v."vers) |
|---|
| 329 | vers=""; |
|---|
| 330 | print type"="vend,prod,size,vers;} } |
|---|
| 331 | } |
|---|
| 332 | type=prod=vend=vers=size=clock=slot="";} |
|---|
| 333 | $1~/-core/ {type="boa";} |
|---|
| 334 | $1~/-firmware/ {type="bio";} |
|---|
| 335 | $1~/-cpu/ {type="cpu";} |
|---|
| 336 | $1~/-bank/ {type="mem";} |
|---|
| 337 | $1~/-memory/ {type="totalmem";} |
|---|
| 338 | $1~/-ide/ {type="ide";} |
|---|
| 339 | $1~/-storage/ {type="sto";} |
|---|
| 340 | $1~/-disk/ {type="dis";} |
|---|
| 341 | $1~/-cdrom/ {type="cdr";} |
|---|
| 342 | $1~/-display/ {type="vga";} |
|---|
| 343 | $1~/-network/ {type="net";} |
|---|
| 344 | $1~/-multimedia/ {type="mul";} |
|---|
| 345 | $1~/-usb/ {type="usb";} |
|---|
| 346 | $1~/-firewire/ {type="fir";} |
|---|
| 347 | $1~/-serial/ {type="bus";} |
|---|
| 348 | END {if (type!="" && prod!="") |
|---|
| 349 | print type"="vend,prod,size,vers; |
|---|
| 350 | if (length(numbank)==0 && length(totalmemory)>=4) |
|---|
| 351 | print totalmemory; } |
|---|
| 352 | ' |
|---|
| 353 | # */ (comentario para Doxygen) |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | #/** |
|---|
| 358 | # ogListSoftware int_ndisk int_npartition |
|---|
| 359 | #@brief Lista el inventario de software instalado en un sistema operativo. |
|---|
| 360 | #@param int_ndisk nº de orden del disco |
|---|
| 361 | #@param int_npartition nº de orden de la partición |
|---|
| 362 | #@return programa versión ... |
|---|
| 363 | #@warning Se ignoran los parámetros de entrada. |
|---|
| 364 | #@note Requisitos: ... |
|---|
| 365 | #@todo Detectar software en Linux |
|---|
| 366 | #@version 0.1 - Primeras pruebas con OpenGnSys |
|---|
| 367 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 368 | #@date 2009-09-23 |
|---|
| 369 | #@version 1.0.5 - Aproximación para inventario de software de Mac OS. |
|---|
| 370 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 371 | #@date 2013-10-08 |
|---|
| 372 | #@version 1.0.6 - Proceso depende del tipo de SO y soporte para FreeBSD. |
|---|
| 373 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 374 | #@date 2014-11-13 |
|---|
| 375 | #@version 1.1.0 - Se muestra el sistema operativo en la primera línea de la salida |
|---|
| 376 | #@author Irina Gomez, ETSII Universidad de Sevilla |
|---|
| 377 | #@date 2016-04-26 |
|---|
| 378 | #*/ ## |
|---|
| 379 | function ogListSoftware () |
|---|
| 380 | { |
|---|
| 381 | # Variables locales. |
|---|
| 382 | local MNTDIR TYPE DPKGDIR RPMDIR PACMANDIR k |
|---|
| 383 | |
|---|
| 384 | # Si se solicita, mostrar ayuda. |
|---|
| 385 | if [ "$*" == "help" ]; then |
|---|
| 386 | ogHelp "$FUNCNAME" "$FUNCNAME 1 1" |
|---|
| 387 | return |
|---|
| 388 | fi |
|---|
| 389 | # Error si no se reciben 2 parametros. |
|---|
| 390 | [ $# = 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
|---|
| 391 | |
|---|
| 392 | # Obtener tipo de sistema de archivos y montarlo. |
|---|
| 393 | MNTDIR=$(ogMount $1 $2) || return $? |
|---|
| 394 | TYPE=$(ogGetOsType $1 $2) || return $? |
|---|
| 395 | |
|---|
| 396 | # Sistema Operativo en la primera línea de la salida |
|---|
| 397 | ogGetOsVersion $1 $2 | awk -F ':' '{print $2}' |
|---|
| 398 | |
|---|
| 399 | case "$TYPE" in |
|---|
| 400 | Linux) # Software de GNU/Linux. |
|---|
| 401 | # Procesar paquetes dpkg. |
|---|
| 402 | DPKGDIR="${MNTDIR}/var/lib/dpkg" |
|---|
| 403 | if [ -r $DPKGDIR ]; then |
|---|
| 404 | # Proceso de fichero en sistemas de 64 bits. |
|---|
| 405 | awk '/Package:/ {if (pack!="") print pack,vers; |
|---|
| 406 | sub(/-dev$/,"",$2); |
|---|
| 407 | pack=$2} |
|---|
| 408 | /Version:/ {sub(/^.*:/,"",$2); sub(/-.*$/,"",$2); |
|---|
| 409 | vers=$2} |
|---|
| 410 | /Status:/ {if ($2!="install") pack=vers=""} |
|---|
| 411 | END {if (pack!="") print pack,vers} |
|---|
| 412 | ' $DPKGDIR/status | sort | uniq |
|---|
| 413 | fi |
|---|
| 414 | # Procesar paquetes RPM. |
|---|
| 415 | RPMDIR="${MNTDIR}/var/lib/rpm" |
|---|
| 416 | if [ -r $RPMDIR ]; then |
|---|
| 417 | # Listar si está instalado el paquete "rpm" en el cliente. |
|---|
| 418 | if which rpm &>/dev/null; then |
|---|
| 419 | rm -f ${RPMDIR}/__db.* |
|---|
| 420 | rpm --dbpath $RPMDIR -qa --qf "%{NAME} %{VERSION}\n" 2>/dev/null | \ |
|---|
| 421 | awk '$1!~/-devel$/ {sub(/-.*$/,"",$2); print $0}' | sort | uniq |
|---|
| 422 | rm -f ${RPMDIR}/__db.* |
|---|
| 423 | else |
|---|
| 424 | # Obtener el nombre de cada paquete en la BD de RPM. |
|---|
| 425 | python <<<" |
|---|
| 426 | import re; |
|---|
| 427 | import bsddb; |
|---|
| 428 | db=bsddb.hashopen('$RPMDIR/Name','r'); |
|---|
| 429 | for k in db.keys(): |
|---|
| 430 | print re.sub('-devel$','',k);" | sort | uniq |
|---|
| 431 | fi |
|---|
| 432 | fi |
|---|
| 433 | # Procesar paquetes pacman. |
|---|
| 434 | PACMANDIR="${MNTDIR}/var/lib/pacman/local" |
|---|
| 435 | if [ -r $PACMANDIR ]; then |
|---|
| 436 | ls $PACMANDIR | awk -F- '/-/ {print gensub(/-/, " ", NF-2);}' |
|---|
| 437 | fi |
|---|
| 438 | ;; |
|---|
| 439 | Windows) # Software de Windows. |
|---|
| 440 | # Comprobar tipo de proceso del registro de Windows. |
|---|
| 441 | if which hivexregedit &>/dev/null; then |
|---|
| 442 | # Nuevo proceso más rápido basado en "hivexregedit". |
|---|
| 443 | local HIVE TMPFILE |
|---|
| 444 | HIVE=$(ogGetHivePath $MNTDIR software 2>/dev/null) |
|---|
| 445 | if [ -n "$HIVE" ]; then |
|---|
| 446 | # Claves de registro para programas instalados. |
|---|
| 447 | TMPFILE=/tmp/tmp$$ |
|---|
| 448 | trap "rm -f $TMPFILE" 1 2 3 9 15 |
|---|
| 449 | hivexregedit --unsafe-printable-strings --export "$HIVE" '\Microsoft\Windows\CurrentVersion\Uninstall' > $TMPFILE 2>/dev/null |
|---|
| 450 | hivexregedit --unsafe-printable-strings --export "$HIVE" '\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall' >> $TMPFILE 2>/dev/null |
|---|
| 451 | # Mostrar los valores "DisplayName" y "DisplayVersion" para cada clave. |
|---|
| 452 | awk -F\" '$1~/^\[/ {n=""} |
|---|
| 453 | $2~/DisplayName/ {n=$4} |
|---|
| 454 | $2~/DisplayVersion/ {print n,$4} |
|---|
| 455 | ' $TMPFILE | sort | uniq |
|---|
| 456 | rm -f $TMPFILE |
|---|
| 457 | fi |
|---|
| 458 | else |
|---|
| 459 | # Compatibilidad con clientes ogLive antiguos. |
|---|
| 460 | local KEYS KEYS32 PROG VERS |
|---|
| 461 | # Claves de registro para programas instalados: formato "{clave}". |
|---|
| 462 | KEYS=$(ogListRegistryKeys $MNTDIR software '\Microsoft\Windows\CurrentVersion\Uninstall') |
|---|
| 463 | KEYS32=$(ogListRegistryKeys $MNTDIR software '\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall') |
|---|
| 464 | # Mostrar los valores "DisplayName" y "DisplayVersion" para cada clave. |
|---|
| 465 | (for k in $KEYS; do |
|---|
| 466 | PROG=$(ogGetRegistryValue $MNTDIR software "\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\$k\\DisplayName") |
|---|
| 467 | if [ -n "$PROG" ]; then |
|---|
| 468 | VERS=$(ogGetRegistryValue $MNTDIR software "\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\$k\\DisplayVersion") |
|---|
| 469 | echo "$PROG $VERS" |
|---|
| 470 | fi |
|---|
| 471 | done |
|---|
| 472 | for k in $KEYS32; do |
|---|
| 473 | PROG=$(ogGetRegistryValue $MNTDIR software "\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\$k\\DisplayName") |
|---|
| 474 | if [ -n "$PROG" ]; then |
|---|
| 475 | VERS=$(ogGetRegistryValue $MNTDIR software "\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\$k\\DisplayVersion") |
|---|
| 476 | echo "$PROG $VERS" |
|---|
| 477 | fi |
|---|
| 478 | done) | sort | uniq |
|---|
| 479 | fi |
|---|
| 480 | ;; |
|---|
| 481 | MacOS) # Software de Mac OS. |
|---|
| 482 | # Listar directorios de aplicaciones e intentar obtener la versión del fichero .plist (tanto original como descomprimido). |
|---|
| 483 | find "${MNTDIR}/Applications" -type d -name "*.app" -prune -print | \ |
|---|
| 484 | while read k; do |
|---|
| 485 | FILE="$k/Contents/version.plist" |
|---|
| 486 | [ -s "$FILE" ] || FILE="$k/Contents/version.plist.uncompress" |
|---|
| 487 | [ -s "$FILE" ] && VERSION=$(awk -F"[<>]" '/ShortVersionString/ {getline;v=$3} |
|---|
| 488 | END {print v}' "$FILE") |
|---|
| 489 | echo "$(basename "$k" .app) $VERSION" |
|---|
| 490 | done | sort |
|---|
| 491 | ;; |
|---|
| 492 | BSD) # Software de FreeBSD. |
|---|
| 493 | sqlite3 $MNTDIR/var/db/pkg/local.sqlite <<<"SELECT name FROM pkg_search;" 2>/dev/null | \ |
|---|
| 494 | sed 's/\(.*\)-\(.*\)/\1 \2/g' | sort |
|---|
| 495 | ;; |
|---|
| 496 | *) ogRaiseError $OG_ERR_PARTITION "$1, $2" |
|---|
| 497 | return $? ;; |
|---|
| 498 | esac |
|---|
| 499 | } |
|---|