Skip to content

Instantly share code, notes, and snippets.

@internetztube
Last active September 18, 2023 10:03
Show Gist options
  • Save internetztube/0d6e093a4dd2b812ccfdeafe21635947 to your computer and use it in GitHub Desktop.
Save internetztube/0d6e093a4dd2b812ccfdeafe21635947 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Install Hetzner Cloud Floating IP on Ubuntu 20.4 or Ubuntu 22.04 machine.
# https://docs.hetzner.com/de/cloud/floating-ips/persistent-configuration/
# Check if the script is being run as root or with sudo privileges
if [ "$(id -u)" -ne 0 ]; then
echo "This script requires root or sudo privileges to run."
exit 1
fi
is_ipv4() {
echo "$1" | grep -E -q "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$"
}
is_ipv6() {
echo "$1" | grep -E -q "^[a-fA-F0-9:]+$"
}
ip_type() {
if is_ipv4 "$1"; then
echo "4"
elif is_ipv6 "$1"; then
echo "6"
else
echo "Invalid"
fi
}
is_ip_valid() {
if is_ipv4 "$1" || is_ipv6 "$1"; then
return 0
else
return 1
fi
}
get_ip() {
read -p "Enter the floating IP address (IPv4/IPv6): " IP_ADDRESS
if ! is_ip_valid "$IP_ADDRESS"; then
echo "Invalid IP Address!"
exit 1
fi
echo "$IP_ADDRESS"
}
IP_ADDRESS=$(get_ip)
IP_TYPE=$(ip_type "$IP_ADDRESS")
UBUNTU_VERSION=$(lsb_release -a 2>/dev/null | grep "Release" | awk '{print $2}')
if [ "$UBUNTU_VERSION" = "20.04" ] || [ "$UBUNTU_VERSION" = "22.04" ]; then
echo "Nice! You're running Ubuntu 20.04 or 22.04!"
FILE_NAME="/etc/netplan/60-floating-ip-$IP_ADDRESS.yaml"
FILE_CONTENT="network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses:
- $IP_ADDRESS/$([ "$IP_TYPE" = "4" ] && echo "32" || echo "64")
"
touch "$FILE_NAME"
echo "$FILE_CONTENT" > "$FILE_NAME"
echo "File created! $FILE_NAME"
echo "Now you need to run: sudo netplan apply"
else
echo "Your Ubuntu Version is not supported!"
fi
@internetztube
Copy link
Author

internetztube commented Sep 17, 2023

Use it like this:

curl -O https://gist.githubusercontent.com/internetztube/0d6e093a4dd2b812ccfdeafe21635947/raw/22c0a89beb8c13f775baee459edb822a5cd28bcd/forge-install-floating-ip.sh \
&& sudo sh forge-install-floating-ip.sh \
&& rm -f forge-install-floating-ip.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment