You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
5.4 KiB

#!/bin/bash
# Script for hardware post on snipeIT and print label by angestöpselt e.V.
# v0.4
######### YOUR SETTINGS HERE #########
PRINTER_IP=<PRINTER_IP>
PXESERVER_IP=<PXESERVER_IP>
######### DON'T CHANGE ANYTHING BELOW THIS LINE #########
if [ "$EUID" -ne 0 ]
then echo "Bitte mit root Rechten starten"
exit
fi
asset__tag=`find /home/computerspende/RE* -type f -printf "%f\n"`
if test -f "$asset__tag"; then
zenity --info --text "Der Rechner wurde bereits eingetragen \n\nRechnernr: $asset__tag" --width=500 --height=200
fi
export LANG=C
LOG=/home/computerspende/auto-install.log
# looking for anydesk source list
anydesk_source=/etc/apt/sources.list.d/anydesk-stable.list
if test -f "$anydesk_source"; then
echo "$anydesk_source exists." >> ${LOG}
else
wget -qO - https://keys.anydesk.com/repos/DEB-GPG-KEY | apt-key add -
echo "deb http://deb.anydesk.com/ all main" > /etc/apt/sources.list.d/anydesk-stable.list
fi
apt-get update
# install dependencies
apt-get install -y jq curl wkhtmltopdf zenity anydesk | tee -a ${LOG}
# prepare for publishing computer to snipe-IT
API_KEY=$(curl ${PXESERVER_IP}/legacy/.api_key)
category=`hostnamectl | grep Chassis | cut -c 21-`
hddvar=`cat "/sys/class/block/sda/size" | awk '{print $1 / 1024 / 1024 }'`
hdd=${hddvar%%.*}
memory=`(grep MemTotal /proc/meminfo | awk '{print int($2 / 1000000 + 0.5)}')`
#os=`hostnamectl | grep Operating | cut -c 21- | tr -d ' '`
os=`hostname | awk -F- '{print $1}'`
version=`cat /etc/os-release | grep VERSION_ID | grep -oP '(?<=").*(?=")'`
cpu=`cat /proc/cpuinfo | grep 'model name' | uniq | cut -c14-39 | sed -e 's/([^()]*)//g' | tr -d ' ' | cut -c -19`
mac=`ip link | sed -n "/BROADCAST.*state UP/{n;p}" | tail -1 | tr -s " " | cut -d" " -f3`
anydeskid=`anydesk --get-id`
# name is for a nice label with some data
name=`echo $os/$version/$cpu/$memory/$hdd`
# Check the category for required field: model_id and fill laptop variables
if [[ $category = "laptop" || $category = "notebook" ]]; then
model=2
battery=`acpi -V | grep '^Battery.*%$' | tail -c 4`
display=`xrandr | awk '/ connected/{print sqrt( ($(NF-2)/10)^2 + ($NF/10)^2 )/2.54}' | cut -c -2`
else
model=1
fi
# Check if an optical drive is available
if [[ $(lsblk | grep sr0) ]]; then
odd=ja
else
odd=nein
fi
## variables are set, finally post to snipe-IT
curl --request POST \
--url https://orga.z31.it/api/v1/hardware \
--header 'accept: application/json' \
--header 'authorization: Bearer '$API_KEY'' \
--header 'content-type: application/x-www-form-urlencoded' \
--data 'status_id=2&name='$name'&model_id='$model'&_snipeit_festplatte_4='$hdd'GB&_snipeit_arbeitsspeicher_6='$memory'GB&_snipeit_betriebssystem_2='$os$version'&_snipeit_prozessor_5='$cpu'&_snipeit_mac_address_1='$mac'&_snipeit_optisches_laufwerk_7='$odd'&_snipeit_display_8='$display'&_snipeit_akku_9='$battery'&_snipeit_anydeskid_10='$anydeskid'' > /tmp/result.json
# get missing information from payload
asset_tag=$(cat /tmp/result.json | jq -r '.payload.asset_tag')
result_jq=$(cat /tmp/result.json | jq -r '.status')
erstellt_am=$(cat /tmp/result.json | jq -r '.payload.created_at')
echo "${category} RE${asset_tag}, erstellt am ${erstellt_am}" >> ${LOG}
echo "MAC-Adresse ${mac} | Anydesk-ID ${anydeskid}" >> ${LOG}
touch "/home/computerspende/RE${asset_tag}"
echo "erstellt am: ${erstellt_am}" >> /home/computerspende/RE${asset_tag}
# checking if the asset has been created and display an error from the snipeIT api
for result in $result_jq
do
case $result in
success)
## This part is creating a file with html content for the printer
cat >/tmp/rechner.html <<EOL
<div>
<p style="float: left;"><img src="http://${PXESERVER_IP}/angestoepselt.png" height="80px" width="80px"></p>
</div>
<div>
<p style="font-size:15px">angestoepselt e.V.<br>Passwort: csw</p>
<b> <p style="font-size:23px"><b>RE${asset_tag}</p></b>
<p style="margin-left:5%; font-size:12px;">${name}</p>
</div>
EOL
exec 2>/dev/null # vernichtet nervige Fehlermeldungen
zenity --question --title "Label drucken" --text "\nEs wurde folgender PC eingetragen: \n\nRechnernr: $asset_tag \nTyp: $category \nOS: $os $version \nCPU: $cpu \nRAM: $memory GB \n\nWillst du jetzt das Label drucken?" --no-wrap --ok-label "Ja" --cancel-label "Nein"
if [ "$?" -eq "0" ]; then
## wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb
# parsing the variables into html file and make a nice looking pdf for printing
# page-heigt and page-width are 99012 Large Adress Labels, you can set this by default in cups Print Options with "Media Size"
wkhtmltopdf --page-height 36mm --page-width 89mm /tmp/rechner.html /tmp/rechner.pdf
echo 'Label drucken' >> ${LOG}
## change the ip of the printer host
# -P is the printer name of cups -> https://CUPS_SERVER_IP:631/printers/
lpr -H ${PRINTER_IP}:631 -P DYMO -o landscape /tmp/rechner.pdf
echo 'Label wurde gedruckt' >> ${LOG}
else
echo "es wurde nichts gedruckt" >> ${LOG}
fi
;;
null)
zenity --info --text "Das hat nicht geklappt bitte führe das Script von Hand aus.\nBenutze bitte im Terminal:\n\ncurl -sSL http://postpc.z31.it | bash" --width=500 --height=200
zenity --error --text "Fehlermeldung: \n\n$(cat /tmp/result.json | jq -r '.error')" --width=500 --height=200
;;
esac
done
zenity --question --title "PC ausschalten?" --text "Soll der PC ausgeschaltet werden?"
if [ "$?" -eq "0" ]; then
shutdown now
fi
exit 0