Shell – Hardware identifizieren
Hallöchen,
wieder mal ein kleines Shellskript. Das Skript ist dazu angedacht um signifikante Hardwaredetails herauszufinden. In den meisten Fällen klappt das Skript wirklich gut. An einigen Stellen funktioniert es nicht zu 100% – wichtig ist, dass es mit dem bash Interpreter aufgerufen wird, sonst buggt es.
Das Skript war eine 20 Minuten Arbeit, daher ist es nicht ganz buggfrei. 2-3 kleine Bugs hat es – aber die kann man, wenn man will, entfernen. Dennoch läuft das Skript.
#!/bin/bash
############ CHECK FOR ROOT ############
echo “Prüfe Benutzerrechte …”
if [ $EUID -ne 0 ]; then
echo -e “\t-> Es werden Rootrechte benötigt. Bitte als ‘root’ einloggen.” 1>&2
exit 100
else
echo -e “\t-> ‘root’ Benutzerrechte erkannt.”
fi############ CHECH FOR INTERNET CONNECTIVITY ############
echo “Prüfe Internetverbindung …”
ping -c 2 www.google.com >> /dev/nullif [ $? -eq 0 ]
then
echo -e “\t-> Erfolgreich.”
INTERNETFLAG=”1″
else
echo -e “\t-> Fehler! Keine Internetverbindung. Bitte Netzwerkeinstellungen überprüfen!”
INTERNETFLAG=”0″
fi
echo -e “\t-> Fertig.”if [ “$INTERNETFLAG” == “0” ]
then
exit 100
else############ CHECK FOR LSHW ############
PKG_OK=$(dpkg-query -W –showformat=’${Status}\n’ lshw|grep “install ok installed”)
if [ “” == “$PKG_OK” ]; then
echo “No lshw. Setting up lshw.”
apt-get update -y -qq && apt-get -y –force-yes –yes -qq install lshw
fi############ CHECK FOR SMARTCTL ############
PKG_OK=$(dpkg-query -W –showformat=’${Status}\n’ smartmontools|grep “install ok installed”)
if [ “” == “$PKG_OK” ]; then
echo “No smartctl. Setting up smartmontools.”
apt-get update -y -qq && apt-get -y –force-yes –yes -qq install smartmontools
fi############ QUESTIONING ############
read -p “Bericht per E-Mail versenden? (j/n) [j]: ” SENDSTATUS
if [ $SENDSTATUS == “j” ] || [ -z $SENDSTATUS ]
then
PKG_OK=$(dpkg-query -W –showformat=’${Status}\n’ postfix|grep “install ok installed”)
if [ “” == “$PKG_OK” ]; then
echo “No postfix. Setting up postfix.”
apt-get update -y -qq && apt-get -y –force-yes –yes -qq install postfix
else
service postfix restart
fi
read -p “E-Mailadresse(n) [some@mail.de]: ” MAILADDRESif [ $MAILADDRES == ” ” ] || [ -z $MAILADDRES ]
then
MAILADDRES=’some@mail.de’
fi
firead -p “SMARTCTL ausführen? (j/n) [j]: ” SMARTCTLRUN
############ MAIN ################ SET TIMESTEMP ####
TIMESTAMP=”$(date +%s)”#### CREATE TEMPORARY FILE FOR EVALUATION ####
touch /tmp/evaluation-lshw.$TIMESTAMP
touch /tmp/evaluation-lshw-output.$TIMESTAMP
lshw -quiet -c memory >> /tmp/evaluation-lshw-output.$TIMESTAMP
############ HOSTNAME ############HOSTNAME=”$(cat /etc/hostname)”
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “### MAIN ###” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMPecho “Rechnerbezeichnung: “$HOSTNAME >> /tmp/evaluation-lshw.$TIMESTAMP
############ ETHERNET ################ GET ALL INTERFACES ####
INTERFACES=(“$(ifconfig | cut -d ‘ ‘ -f1 | sed -e ‘/^ *$/d’ | grep -v ‘lo’ | grep -v ‘tun’)”)
INTERFACES_ARR=($INTERFACES)#### GREP MAC ADRESS ####
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “### INTERFACE(S) ###” >> /tmp/evaluation-lshw.$TIMESTAMPfor i in “${INTERFACES_ARR[@]}”
do
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “$i” >> /tmp/evaluation-lshw.$TIMESTAMP
echo -e “\t MAC Addresse:” $(ifconfig $i | grep -o -E ‘([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}’) >> /tmp/evaluation-lshw.$TIMESTAMP
echo -e “\t IP Adresse:” $(ifconfig $i | egrep “(inet Adresse:|inet addr:)” | cut -d ‘:’ -f2 | cut -d ‘ ‘ -f1) >> /tmp/evaluation-lshw.$TIMESTAMP
done
############ MAINBOARD ############
BOARDMANUFACTURE=”$(dmidecode -s baseboard-manufacturer | cut -d ‘ ‘ -f1)”
BOARDTYPE=”$(dmidecode | grep -i product | uniq | cut -d ‘ ‘ -f3)”echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “### MAINBOARD ###” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMPecho “Hersteller: “$BOARDMANUFACTURE >> /tmp/evaluation-lshw.$TIMESTAMP
echo “Typ: “$BOARDTYPE >> /tmp/evaluation-lshw.$TIMESTAMP############ CPU ############
CPUMANUFACTURE=”$(cat /proc/cpuinfo | grep ‘vendor_id’ | cut -d ‘:’ -f2 | uniq)”
CPUTYPE=”$(cat /proc/cpuinfo | grep ‘model name’ | cut -d ‘:’ -f2 | uniq)”
CPUCORES=”$(cat /proc/cpuinfo | grep ‘processor’ | wc -l)”echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “### CPU ###” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMPecho “Hersteller:”$CPUMANUFACTURE >> /tmp/evaluation-lshw.$TIMESTAMP
echo “Modell:”$CPUTYPE >> /tmp/evaluation-lshw.$TIMESTAMP
echo “Cores:”$CPUCORES >> /tmp/evaluation-lshw.$TIMESTAMP
############ RAM ############
RAMBANKS=$(dmidecode -t 17 | grep ‘Handle 0x’ | cut -d ‘ ‘ -f2 | cut -d ‘,’ -f1)
RAMBANKS_ARR=($RAMBANKS)
MAXBANKS=$(cat /tmp/evaluation-lshw-output.$TIMESTAMP | grep ‘bank’ | wc -l)
typeset -i FREEBANKS
FREEBANKS=$(echo “$MAXBANKS”)for RAM in “${RAMBANKS_ARR[@]}”
do
CHECKEMPTY=$(dmidecode -t 17 | grep -A 15 “$RAM” | grep ‘Serial Number:’ | sed ‘s/Serial Number\://g’)if [[ “$CHECKEMPTY” =~ “Empty” ]]
then
FREEBANKS=FREEBANKS-1
fidone
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “### RAM ###” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “Groeße:”$(cat /tmp/evaluation-lshw-output.$TIMESTAMP | grep -A 5 ‘memory’ | egrep “(size:|Größe:)” | cut -d ‘:’ -f2) >> /tmp/evaluation-lshw.$TIMESTAMP
echo “Anzahl Steckplaetze: “$MAXBANKS >> /tmp/evaluation-lshw.$TIMESTAMP
echo “Belegte Steckplaetze: “$FREEBANKS >> /tmp/evaluation-lshw.$TIMESTAMP
############ HDD CHECK ############if [ -z $SMARTCTLRUN ] || [ “$SMARTCTLRUN” == “j” ]
thenecho “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “### HDD – SMARTCTL ###” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP############ DEVICES ############
echo -e “############################## FDISK ##############################”
fdisk -l
echo -e “###################################################################\n”
read -p “Bitte Device(s) angeben (Beispiel – beliebig viele: sda sdb sdc sdd): ” SMARTCTLDEVICE
read -p “Parameter für SMARTCTL? (Leer lassen falls nicht gewünscht – z. B. -t short): ” SMARTCTLPARAM
SMARTCTLDEVICE_ARR=($SMARTCTLDEVICE)for DEVICES in “${SMARTCTLDEVICE_ARR[@]}”
do
ls /dev/$DEVICES >/dev/null 2>&1
if [ $? = 0 ]
then
echo “SMARTCTL für Device $DEVICES wird gestartet …”
touch /tmp/smartctl-output.$TIMESTAMP.$DEVICESsmartctl -a /dev/$DEVICES > /tmp/smartctl-output.$TIMESTAMP.$DEVICES
if [ “$SMARTCTLPARAM” = “-t short” ]
then
echo -e “\t-> Shorttest wird durchgeführt (Device: $DEVICES) – bitte warten …”
sleep 150
smartctl -a /dev/$DEVICES $SMARTCTLPARAM > /tmp/smartctl-output.$TIMESTAMP.$DEVICES
echo -e “\t\t-> Fertig.”
fi### READ SPECI)FICATIONS AND WRITE IT AT THE FIRST LINE ###
SMARTERROR=”$(grep “ATA Error Count:” /tmp/smartctl-output.$TIMESTAMP.$DEVICES)”
### CHERCK FOPR SMARTERRORS ###
if [ -z “$SMARTERROR” ]
then
SMARTERROR=”SMARTERRORS: 0″
fised -i 1i”$(grep “Seek_Error_Rate” /tmp/smartctl-output.$TIMESTAMP.$DEVICES)” /tmp/smartctl-output.$TIMESTAMP.$DEVICES
sed -i 1i”$(grep “Reallocated_Sector_Ct” /tmp/smartctl-output.$TIMESTAMP.$DEVICES)” /tmp/smartctl-output.$TIMESTAMP.$DEVICES
sed -i 1i”$(grep “Power_On_Hours” /tmp/smartctl-output.$TIMESTAMP.$DEVICES)” /tmp/smartctl-output.$TIMESTAMP.$DEVICES
sed -i 1i”$(grep “Raw_Read_Error_Rate” /tmp/smartctl-output.$TIMESTAMP.$DEVICES)” /tmp/smartctl-output.$TIMESTAMP.$DEVICES
sed -i 1i”$SMARTERROR” /tmp/smartctl-output.$TIMESTAMP.$DEVICES
sed -i 1i”$(grep “Serial Number:” /tmp/smartctl-output.$TIMESTAMP.$DEVICES)” /tmp/smartctl-output.$TIMESTAMP.$DEVICES
sed -i 1i”$(grep “Device Model:” /tmp/smartctl-output.$TIMESTAMP.$DEVICES)” /tmp/smartctl-output.$TIMESTAMP.$DEVICES
SERIALNUMBER=”$(grep “Serial Number:” /tmp/smartctl-output.$TIMESTAMP.$DEVICES | uniq)”
SERIALNUMBER=”$(echo $SERIALNUMBER | grep ‘Serial Number:’ | cut -d ‘:’ -f2 | sed ‘s/ //g’)”SERIALSUBJECT=$SERIALSUBJECT’ ‘$SERIALNUMBER
## Lesegeschwindigkeiten (Cache – Buffer) auslesen ##
echo -e “\t-> Lesegeschwindigkeit wird getestet”
for i in {0..2} ; do hdparm -Tt /dev/$DEVICES >> /tmp/smartctl-output.$TIMESTAMP.$DEVICES; doneecho -e “\t-> Erfolgreich beendet”
else
echo “Das Device $DEVICES wurde nicht gefunden und wird übersprungen”
fi
donetypeset -i WRITEMAINFILE
WRITEMAINFILE=1
for DEVICES in “${SMARTCTLDEVICE_ARR[@]}”
do
ls /dev/$DEVICES >/dev/null 2>&1
if [ $? = 0 ]
then
cat /tmp/smartctl-output.$TIMESTAMP.$DEVICES >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “—————————————- NEXT HDD —————————————-” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
fi
donefi
############ EOF ############
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “” >> /tmp/evaluation-lshw.$TIMESTAMP
echo “### EOF ###” >> /tmp/evaluation-lshw.$TIMESTAMP
########## SENDMAIL ############
if [ $SENDSTATUS == “j” ] || [ -z $SENDSTATUS ]
then
sed -i 1i”Subject: HWTEST $(ifconfig ${INTERFACES_ARR[0]} | egrep “(inet Adresse:|inet addr:)” | cut -d ‘:’ -f2 | cut -d ‘ ‘ -f1)” /tmp/evaluation-lshw.$TIMESTAMP
sendmail -t $MAILADDRES < /tmp/evaluation-lshw.$TIMESTAMP
fiecho -e “\t-> E-Mail versendet …”
########## DELETE TEMPORARY EVALUATION FILE ############
#cat /tmp/evaluation-lshw.$TIMESTAMP
rm /tmp/evaluation-lshw.$TIMESTAMP
rm /tmp/evaluation-lshw-output.$TIMESTAMP
echo “Temporäre Dateien entfernt”
fi
Wie gesagt – kurz runtergetippt. Bitte seid nachsichtig. Verbesserungen? Jederzeit gerne in den Kommentaren.
Grüße