Repoo Init Version 1
This commit is contained in:
parent
45c3188690
commit
d320675690
152
hetzner-tools.sh
Normal file
152
hetzner-tools.sh
Normal file
@ -0,0 +1,152 @@
|
||||
#!/bin/bash
|
||||
# 2024 by Maik Schmalle
|
||||
|
||||
HETZNER_DNS_API="" # Hetzner DNS API
|
||||
HETZNER_CLOD_API="" # Hetzner Cloud API
|
||||
ZONEID="" # Zoneid
|
||||
SERVER_TYPE="cx22" # Servertyp (Buchstaben müssen klein geschrieben werden)
|
||||
SERVER_IMAGE="ubuntu-24.04" # Betriebssystem
|
||||
SERVER_SSHKEY="" # SSH-KEY Name aus dem Adminbereich bei Hetzner-Cloud
|
||||
SERVER_LOCATION="fsn1" #nbg1 = Nürnberg, fsn1 = Falkenstein, hel1 = Helsinki, ash = Ashborne, hil = Hillsboro, sin = Singapur
|
||||
|
||||
SUBDOMAIN="$2" # Subdomain
|
||||
IP="$3" # IP des Servers
|
||||
ID="" #Variable für verschiedene Funktionen
|
||||
|
||||
# ID einer Subdomain rausfinden
|
||||
function dns_id_sub {
|
||||
# JSON-Daten mit Curl abrufen und parsen
|
||||
curl https://dns.hetzner.com/api/v1/records?zone_id=$ZONEID -H 'Auth-API-Token: '$HETZNER_DNS_API > /tmp/dns.json
|
||||
ID=$(cat /tmp/dns.json | jq -r --arg name "$SUBDOMAIN" '.records[] | select(.name == $name) | .id')
|
||||
|
||||
# Ergebnis ausgeben
|
||||
if [ -n "$ID" ]; then
|
||||
echo "ID von $SUBDOMAIN ist $ID"
|
||||
else
|
||||
echo "Name nicht gefunden"
|
||||
fi
|
||||
}
|
||||
|
||||
# Subdomain löschen
|
||||
function dns_del {
|
||||
dns_id_sub
|
||||
curl -X "DELETE" "https://dns.hetzner.com/api/v1/records/$ID" \
|
||||
-H 'Auth-API-Token: '$HETZNER_DNS_API
|
||||
}
|
||||
|
||||
# Subdomain hinzufügen
|
||||
function dns_add {
|
||||
curl -X "POST" "https://dns.hetzner.com/api/v1/records" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-H 'Auth-API-Token: '$HETZNER_DNS_API \
|
||||
-d $'{"value": "'$IP'", "ttl": 86400, "type": "A", "name": "'$SUBDOMAIN'", "zone_id": "'$ZONEID'"}'
|
||||
}
|
||||
|
||||
# Server hinzufügen
|
||||
function server_add {
|
||||
curl \
|
||||
-o /tmp/server.json \
|
||||
-X POST \
|
||||
-H "Authorization: Bearer $HETZNER_CLOD_API" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"automount":false,"server_type": "'$SERVER_TYPE'", "#firewalls":[{"firewall":38}],"image":"'$SERVER_IMAGE'","labels":{"environment":"prod","example.com/my":"label","just-a-key":""},"location":"'$SERVER_LOCATION'","name":"test","networks":[3025198],"ssh_keys":["'$SERVER_SSHKEY'"],"start_after_create":true,"user_data":"#cloud-config\nruncmd:\n- [touch, /root/cloud-init-worked]\n"}' \
|
||||
"https://api.hetzner.cloud/v1/servers"
|
||||
}
|
||||
|
||||
# Server ID anzeigen
|
||||
function server_id {
|
||||
curl -H "Authorization: Bearer $HETZNER_CLOD_API" "https://api.hetzner.cloud/v1/servers" > /tmp/server.json
|
||||
ID=$(cat /tmp/server.json | jq -r --arg name "$SUBDOMAIN" '.servers[] | select(.name == $name) | .id')
|
||||
|
||||
# Ergebnis ausgeben
|
||||
if [ -n "$ID" ]; then
|
||||
echo "ID von $SUBDOMAIN ist $ID"
|
||||
else
|
||||
echo "Name nicht gefunden"
|
||||
fi
|
||||
}
|
||||
|
||||
# Server löschen
|
||||
function server_del {
|
||||
server_id
|
||||
curl \
|
||||
-X DELETE \
|
||||
-H "Authorization: Bearer $HETZNER_CLOD_API" \
|
||||
"https://api.hetzner.cloud/v1/servers/$ID"
|
||||
}
|
||||
|
||||
# Server hinzufügen und passenden SUBDOMAIN Eintrag anzulegen
|
||||
function server_dns_add {
|
||||
server_add
|
||||
IP=$(jq -r '.server.public_net.ipv4.ip' "/tmp/server.json")
|
||||
|
||||
# Ergebnis ausgeben
|
||||
if [ -n "$IP" ]; then
|
||||
echo "IP von $SUBDOMAIN ist $IP"
|
||||
else
|
||||
echo "Name nicht gefunden"
|
||||
fi
|
||||
dns_add
|
||||
}
|
||||
|
||||
# der Help Text
|
||||
function help {
|
||||
echo " Ein keines Programm was die Heztzner API benutzt um eienn Server und Subdomain erstellen."
|
||||
echo ""
|
||||
echo " hetzer.sh [option] {Subdomain} {IPV4}"
|
||||
echo " "
|
||||
echo " Option:"
|
||||
echo " --dns_add Subdomain hinzufügen"
|
||||
echo " --dns_del Subdomain löschen"
|
||||
echo " --dns_id ID einer Subdomain anzeigen"
|
||||
echo " "
|
||||
echo " --server_add Server hinzufügen und Subdomain erstellen"
|
||||
echo " --server_del Server löschen und Subdomain enfernen"
|
||||
echo " "
|
||||
echo " --server_dns_add Server mit dazugehörigen DNS eintrag erstellen"
|
||||
echo " "
|
||||
echo " --help diese Hilfe anzeigen"
|
||||
}
|
||||
|
||||
# der Hauptbereich
|
||||
case "$1" in
|
||||
--help)
|
||||
help
|
||||
;;
|
||||
|
||||
--dns_id)
|
||||
dns_id_sub
|
||||
rm /tmp/dns.json
|
||||
;;
|
||||
|
||||
--dns_add)
|
||||
dns_add
|
||||
;;
|
||||
|
||||
--dns_del)
|
||||
dns_del
|
||||
;;
|
||||
|
||||
--server_id)
|
||||
server_id
|
||||
rm /tmp/server.json
|
||||
;;
|
||||
|
||||
--server_add)
|
||||
server_add
|
||||
;;
|
||||
|
||||
--server_del)
|
||||
server_del
|
||||
;;
|
||||
|
||||
--server_dns_add)
|
||||
server_dns_add
|
||||
rm /tmp/server.json
|
||||
;;
|
||||
|
||||
*)
|
||||
help
|
||||
;;
|
||||
esac
|
||||
exit 0
|
Loading…
Reference in New Issue
Block a user