imagestack/tools/pxe_utils/files/late_command.sh
2026-01-28 09:21:36 +01:00

229 lines
6.1 KiB
Bash
Executable file

#!/bin/bash
set -euxo pipefail
# Variables
PXE_API_SERVER="http://10.7.3.101:8888"
LABEL_PROXY_SERVER="http://10.7.5.11:8888"
SNIPE_IT_SERVER="https://computer.z31.it"
WIFI_SSID=""
WIFI_PASSWORD=""
LABEL_PROXY=false
while [[ $# -gt 0 ]]; do
case "$1" in
--table)
LABEL_PROXY=true
shift
;;
*)
echo "Unbekannte Flag/Parameter: $1"
exit 1
;;
esac
done
echo "Starte late_command Ausfuehrung"
cd "$(dirname "$0")"
### CONFIGURATION
# Move poweroff to /usr/bin so Tanja shuts up about it.
echo "Kopiere poweroff in /usr/bin"
cp /sbin/poweroff /usr/bin/poweroff
# Compile additional locales to be able to change language
echo "Generiere zusaetzliche Locales"
add_locale () {
LINE="$1"
grep -qxF "$LINE" "/etc/locale.gen" || echo "$LINE" | sudo tee -a "/etc/locale.gen" >/dev/null
}
add_locale "en_US.UTF-8 UTF-8"
add_locale "fr_FR.UTF-8 UTF-8"
add_locale "ru_RU.UTF-8 UTF-8"
add_locale "ru_UA.UTF-8 UTF-8"
add_locale "fa_IR.UTF-8 UTF-8"
add_locale "ar_SA.UTF-8 UTF-8"
sudo locale-gen
sudo update-locale LANG=de_DE.UTF-8
# Check if a Wifi device is detected and notify if no device is detected
echo "Pruefe ob Wifi erkannt wird."
category=`hostnamectl | grep Chassis | cut -d: -f2`
if nmcli -t -f TYPE device status | grep -qx "wifi"; then
echo "Wifi erkannt."
echo "Verbinde mit Setup Wifi"
nmcli device wifi connect "$WIFI_SSID" password "$WIFI_PASSWORD"
else
if [[ $category =~ "laptop" || $category =~ "notebook" ]]; then\
echo "Wifi wurde nicht erkannt. Warne User"
zenity --error --text "Es wurde kein Wifi Adapter gefunden. Bitte pruefe ob die Treiber richtig installiert sind." --width=500 --height=200
else
echo "Geraet ist kein Laptop. Wifi nicht unbedingt erwartet"
fi
fi
echo "Registriere Geraet in Snipe-IT"
### SNIPE IT AND LABEL PRINT
api_key=$(curl -s "${PXE_API_SERVER}/snipe_api")
# CPU Model
cpu_model=`cat /proc/cpuinfo | grep 'model name' | uniq | cut -c14-39 | sed -e 's/([^()]*)//g' | tr -d ' ' | cut -c -12`
# OS Distribution and Version
if [ -f /etc/os-release ]; then
. /etc/os-release
os_distro="$NAME"
os_version="$VERSION"
else
os_distro="Unknown"
os_version="Unknown"
fi
# Total RAM
total_ram=$(awk '/MemTotal:/ { print int($2/1024/1024+0.5) }' < /proc/meminfo)
# Free space on root (/)
free_space=$(df -h / | awk 'NR==2 { print $4 }')
if [[ $category =~ "laptop" || $category =~ "notebook" ]]; then\
echo "Information ueber Laptop sammeln"
model=2
battery=$(acpi -V | grep '^Battery.*%$' | tail -c 4 || echo "None")
display=`xrandr | awk '/ connected/{print sqrt( ($(NF-2)/10)^2 + ($NF/10)^2 )/2.54}' | cut -c -2 | head -n 1`
else
model=1
fi
# You can now use these variables as needed
# For example, to print them later:
if [[ $(lsblk | grep sr0) ]]; then
odd=ja
else
odd=nein
fi
mac=`ip link | sed -n "/BROADCAST.*state UP/{n;p}" | tail -1 | tr -s " " | cut -d" " -f3`
serialno=`sudo dmidecode -s system-serial-number`
name=`echo $os_distro/$os_version/$cpu_model/$total_ram/$free_space`
anydeskid=4711
echo $name
if [[ $model == 1 ]]; then
post_data() {
cat << EOF
{
"status_id": "2",
"name": "$name",
"model_id": "$model",
"serial": "$serialno",
"_snipeit_mac_address_1": "$mac",
"_snipeit_betriebssystem_2": "$os_distro $os_version",
"_snipeit_festplatte_4": "$free_space",
"_snipeit_prozessor_5": "$cpu_model",
"_snipeit_arbeitsspeicher_6": "$total_ram",
"_snipeit_optisches_laufwerk_7": "$odd",
"_snipeit_anydeskid_10": "$anydeskid"
}
EOF
}
else
post_data() {
cat << EOF
{
"status_id": "2",
"name": "$name",
"model_id": "$model",
"serial": "$serialno",
"_snipeit_mac_address_1": "$mac",
"_snipeit_betriebssystem_2": "$os_distro $os_version",
"_snipeit_festplatte_4": "$free_space",
"_snipeit_prozessor_5": "$cpu_model",
"_snipeit_arbeitsspeicher_6": "$total_ram",
"_snipeit_optisches_laufwerk_7": "$odd",
"_snipeit_display_8": "$display",
"_snipeit_akku_9": "$battery",
"_snipeit_anydeskid_10": "$anydeskid"
}
EOF
}
fi
curl --request GET \
--url "${SNIPE_IT_SERVER}/api/v1/hardware?limit=5&search=${mac}" \
--header 'accept: application/json' \
--header 'authorization: Bearer '$api_key'' \
--header 'content-type: application/x-www-form-urlencoded' > ./check_result.json
asset_tag=$(jq -j .rows[].asset_tag ./check_result.json)
echo $asset_tag
if [ ! -z ${asset_tag} ]
then
echo "Rechner wurde in Snipe-IT bereits registiert. Frage User nach erneuten Label Druck"
if zenity --question --text="Der Rechner ist ist bereits eingetragen \n RE${asset_tag} \n Soll das Label nochmal gedruckt werden?" --width=500 --height=200; then
echo "Generiere Label"
print_data() {
cat << EOF
{
"id": "$asset_tag",
"distribution": "$os_distro",
"version": "$os_version",
"cpu": "$cpu_model",
"memory": "$total_ram",
"disk": "$free_space"
}
EOF
}
echo "Drucke Label"
curl --request POST -d "$(print_data)" "${PXE_API_SERVER}/label"
else
echo "User chose No or closed the dialog"
fi
exit 0
fi
echo Registriere Computer $(post_data)
# curl --request POST \
# --url "${SNIPE_IT_SERVER}/api/v1/hardware" \
# --header 'accept: application/json' \
# --header 'authorization: Bearer '$api_key'' \
# --header 'Content-Type: application/json' \
# --data "$(post_data)" > ./result.json
# get missing information from payload
# asset_tag=$(cat ./result.json | jq -r '.payload.asset_tag')
# result_jq=$(cat ./result.json | jq -r '.status')
# erstellt_am=$(cat ./result.json | jq -r '.payload.created_at')
#
asset_tag=00000
echo "Asset Tag des Rechners: $asset_tag"
echo "Generiere Labeldaten"
print_data() {
cat << EOF
{
"id": "$asset_tag",
"distribution": "$os_distro",
"version": "$os_version",
"cpu": "$cpu_model",
"memory": "$total_ram",
"disk": "$free_space"
}
EOF
}
echo "Drucke Label"
if $LABEL_PROXY; then
echo "PROXY"
curl --request POST -d "$(print_data)" "${LABEL_PROXY_SERVER}/label"
else
echo "PXE"
curl --request POST -d "$(print_data)" "${PXE_API_SERVER}/label"
fi
# | lpr -H 10.200.4.12:631 -P DYMO -o landscape -
zenity --info --text "Label wird gedruckt \n $asset_tag \n Press OK to exit" --width=500 --height=200