hetzner-proxmox-single/script/network_config.sh

159 lines
5 KiB
Bash
Raw Normal View History

2023-09-04 18:38:00 +02:00
#!/bin/bash
# Function to prompt for input with a default value
prompt_input() {
local prompt=$1
local default=$2
read -p "$prompt [$default]: " input
echo "${input:-$default}"
}
2023-12-15 16:58:18 +01:00
# Function to create bridge interface text for additional IP and internal bridges
create_bridge_text() {
local ip=$1
local bridge_id=$2
2023-12-15 15:37:53 +01:00
local mac_address=$3
2023-12-16 12:57:32 +01:00
local external_bridge_id=$bridge_id
local internal_bridge_id=$((bridge_id * 100))
2023-12-15 16:58:18 +01:00
2023-12-16 13:02:34 +01:00
# WAN bridge configuration with MAC address and public IP
2023-12-15 16:58:18 +01:00
local bridge_config="
auto vmbr${external_bridge_id}
iface vmbr${external_bridge_id} inet static
address ${ip}
netmask ${NETMASK}
bridge_ports none
bridge_stp off
bridge_fd 0
2023-12-15 15:37:53 +01:00
hwaddress ether ${mac_address}
2023-12-16 13:02:34 +01:00
#WAN ${external_bridge_id}
2023-12-16 12:57:32 +01:00
"
2023-12-15 16:58:18 +01:00
2023-12-16 13:02:34 +01:00
# LAN bridge configuration without an IP, as it's for internal network only
2023-12-15 16:58:18 +01:00
bridge_config+="
auto vmbr${internal_bridge_id}
iface vmbr${internal_bridge_id} inet manual
bridge_ports none
bridge_stp off
bridge_fd 0
2023-12-16 13:02:34 +01:00
#LAN ${internal_bridge_id}
2023-12-16 12:57:32 +01:00
"
2023-12-15 16:58:18 +01:00
echo "$bridge_config"
}
# Collect inputs
MAINSERVERIP=$(prompt_input "MAIN_SERVER_IP" "192.168.0.1")
GATEWAYADDRESS=$(prompt_input "MAIN_SERVER_GATEWAY_ADDRESS" "192.168.0.254")
NETMASK=$(prompt_input "NETMASK" "255.255.255.0")
BROADCASTIP=$(prompt_input "BROADCASTIP" "192.168.0.255")
ADD_IP_ADDRESSES=$(prompt_input "ADDITIONAL_IP_ADDRESSES (comma-separated)" "")
2023-12-15 15:37:53 +01:00
MAC_ADDRESSES=$(prompt_input "MAC_ADDRESSES for additional IPs (comma-separated)" "")
NETWORK_INTERFACE=$(prompt_input "NETWORK_INTERFACE" "eth0")
# Display inputs for confirmation
2023-12-15 13:15:42 +01:00
echo "---------------------------------------------------------------------"
echo "You have entered the following configuration:"
echo "MAIN_SERVER_IP: $MAINSERVERIP"
echo "MAIN_SERVER_GATEWAY_ADDRESS: $GATEWAYADDRESS"
echo "NETMASK: $NETMASK"
echo "BROADCASTIP: $BROADCASTIP"
echo "ADDITIONAL_IP_ADDRESSES: $ADD_IP_ADDRESSES"
2023-12-15 15:37:53 +01:00
echo "MAC_ADDRESSES: $MAC_ADDRESSES"
echo "NETWORK_INTERFACE: $NETWORK_INTERFACE"
2023-12-15 12:14:48 +01:00
2023-12-15 13:15:42 +01:00
echo "---------------------------------------------------------------------"
read -p "Is this correct? [yes/no]: " confirmation
if [[ $confirmation != [Yy]* ]]; then
echo "Exiting without changes."
exit
fi
2023-12-15 15:37:53 +01:00
# Split ADD_IP_ADDRESSES and MAC_ADDRESSES into arrays
IFS=',' read -ra ADDR <<<"$ADD_IP_ADDRESSES"
2023-12-15 15:37:53 +01:00
IFS=',' read -ra MACS <<<"$MAC_ADDRESSES"
2023-12-16 13:54:47 +01:00
# Generate dynamic routing rules
additional_routes=""
for add_ip in "${ADDR[@]}"; do
additional_routes+=" up ip route add $add_ip dev ${NETWORK_INTERFACE}
"
done
# Initialize the interfaces file content
interfaces_content="
2023-09-04 18:38:00 +02:00
### Hetzner Online GmbH installimage
source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback
iface lo inet6 loopback
2023-12-16 13:54:47 +01:00
# Main network interface configuration
iface ${NETWORK_INTERFACE} inet manual
up ip route add -net ${GATEWAYADDRESS} netmask ${NETMASK} gw ${GATEWAYADDRESS} vmbr0
up sysctl -w net.ipv4.ip_forward=1
up sysctl -w net.ipv4.conf.${NETWORK_INTERFACE}.send_redirects=0
up sysctl -w net.ipv6.conf.all.forwarding=1
$additional_routes
up ip route add 192.168.0.0/16 via ${MAINSERVERIP} dev vmbr0
up ip route add 172.16.0.0/12 via ${MAINSERVERIP} dev vmbr0
up ip route add 10.0.0.0/8 via ${MAINSERVERIP} dev vmbr0
2023-09-04 18:38:00 +02:00
auto vmbr0
iface vmbr0 inet static
2023-12-15 16:58:18 +01:00
address ${MAINSERVERIP}
netmask 32
gateway ${GATEWAYADDRESS}
broadcast ${BROADCASTIP}
bridge-ports ${NETWORK_INTERFACE}
bridge-stp off
bridge-fd 0
pointopoint ${GATEWAYADDRESS}
2023-12-16 13:02:34 +01:00
#Main IP configuration
"
2023-09-04 18:38:00 +02:00
2023-12-15 16:58:18 +01:00
# Append bridge interfaces for each additional IP and MAC address and create internal bridges
for i in "${!ADDR[@]}"; do
2023-12-16 12:57:32 +01:00
# Increment bridge_id for each additional IP
bridge_id=$((i + 1))
interfaces_content+=$(create_bridge_text "${ADDR[i]}" "$bridge_id" "${MACS[i]}")
2023-09-04 18:38:00 +02:00
done
2023-12-15 12:58:37 +01:00
# Save the new configuration to a temporary file
echo "$interfaces_content" > /tmp/new_interfaces
# Display the current network configuration
2023-12-15 13:15:42 +01:00
echo "---------------------------------------------------------------------"
2023-12-15 12:58:37 +01:00
echo "Current network configuration (/etc/network/interfaces):"
cat /etc/network/interfaces
2023-12-15 16:58:18 +01:00
echo ""
2023-12-15 12:58:37 +01:00
# Display the new network configuration
2023-12-15 13:15:42 +01:00
echo "---------------------------------------------------------------------"
2023-12-15 12:58:37 +01:00
echo "New network configuration:"
cat /tmp/new_interfaces
2023-12-15 16:58:18 +01:00
echo ""
2023-12-15 12:58:37 +01:00
# Show the differences
2023-12-15 13:15:42 +01:00
echo "---------------------------------------------------------------------"
2023-12-15 12:58:37 +01:00
echo "Configuration differences:"
diff /etc/network/interfaces /tmp/new_interfaces
2023-12-15 16:58:18 +01:00
echo ""
# Confirm before applying changes
2023-12-15 13:15:42 +01:00
echo "---------------------------------------------------------------------"
read -p "Apply this network configuration? [yes/no]: " apply_conf
2023-09-04 18:38:00 +02:00
if [[ $apply_conf == [Yy]* ]]; then
2023-12-15 13:29:50 +01:00
timestamp=$(date +%Y%m%d-%H%M%S)
mv /etc/network/interfaces /etc/network/interfaces.bak-$timestamp
mv /tmp/new_interfaces /etc/network/interfaces
2023-12-16 12:57:32 +01:00
echo "The network can be restarted with the following command: '/etc/init.d/networking' restart or 'systemctl restart networking'"
else
echo "Exiting without applying changes."
2023-12-15 13:29:50 +01:00
rm /tmp/new_interfaces
fi