-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm-boot.sh
54 lines (47 loc) · 1.65 KB
/
vm-boot.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
#/var/lib/cloud/scripts/per-boot
get_hostname(){
#login values
PROX_USERNAME=USER@pve
PROX_PASSWORD="geheimesPasswort"
HOST="https://proxmoxhost.tld"
#get all mac adresses
MAC_ADDRESS=($(cat /sys/class/net/*/address))
#get ticket
DATA=`curl -s4 -k -d "username=$PROX_USERNAME&password=$PROX_PASSWORD" $HOST/api2/json/access/ticket`
TICKET=$(echo "${DATA}" | jq -r .data.ticket )
CSRF=$(echo "${DATA}" | jq -r .data.CSRFPreventionToken)
#get all vms
DATA=$(curl -s4k -b "PVEAuthCookie=$TICKET" $HOST/api2/json/cluster/resources | jq -S -r '(.data[] |select(.type=="qemu")) ')
NODE=($(echo "${DATA}" | jq -r .node))
VMID=($(echo "${DATA}" | jq -r .vmid))
NAME=($(echo "${DATA}" | jq -r .name | sed s/'VM.[0-9]*'/unnamed/g))
echo "own mac"$MAC_ADDRESS
#get interface mac address
for ((i = 0 ; i < ${#VMID[@]} ; i++)); do
DATA=$(curl -s4k -b "PVEAuthCookie=$TICKET" $HOST/api2/json/nodes/${NODE[$i]}/qemu/${VMID[$i]}/config)
MAC=$(echo "${DATA}" | jq .data.net0 | cut -d "=" -f2 | cut -d "," -f1)
#compare mac adress
if [[ "${MAC_ADDRESS[@]}" =~ "${MAC,,}" ]]; then
echo $MAC
echo "${NAME[$i]}"
#set hostame
if [[ ! "${NAME[$i]}" == "null" ]];then
if [[ -n "${NAME[$i]}" ]];then
if [[ ! $(cat /etc/hostname) = "${NAME[$i]}" ]];then
oldname=$(cat /etc/hostname)
sed -i -e "s/$oldname/${NAME[$i]}/g" /etc/hosts
hostnamectl set-hostname "${NAME[$i]}"
HOSTNAME="${NAME[$i]}"
reboot
fi
fi
fi
break
fi
done
}
main(){
get_hostname
}
main