From b11ebfa255255ca1fbfc9d0093067f3caed21baa Mon Sep 17 00:00:00 2001 From: Lucky <14868134+L-U-C-K-Y@users.noreply.github.com> Date: Fri, 15 Dec 2023 11:39:04 +0100 Subject: [PATCH] support multiple ip addresses and show a prompt for network_config.sh --- script/network_config.sh | 115 ++++++++++++++++++++++++--------------- 1 file changed, 71 insertions(+), 44 deletions(-) diff --git a/script/network_config.sh b/script/network_config.sh index 72880f4..871cf10 100755 --- a/script/network_config.sh +++ b/script/network_config.sh @@ -1,13 +1,56 @@ #!/bin/bash -read -p "MAIN_SERVER_IP: " MAINSERVERIP -read -p "MAIN_SERVER_GATEWAY_ADRESS: " GATEWAYADRESS -read -p "NETMASK: " NETMASK -read -p "BROADCASTIP: " BROADCASTIP -read -p "ADDITIONAL_IP_ADRESS: " ADD_IP_ADRESS -read -p "NETWORK_INTERFACE: " NETWORK_INTERFACE +# 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}" +} -echo " +# Function to create bridge interface text for additional IP +create_bridge_text() { + local ip=$1 + local bridge_id=$2 + echo " +auto vmbr${bridge_id} +iface vmbr${bridge_id} inet static + address ${ip} + netmask ${NETMASK} + bridge_ports none + bridge_stp off + bridge_fd 0 +#LAN${bridge_id}" +} + +# 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)" "") +NETWORK_INTERFACE=$(prompt_input "NETWORK_INTERFACE" "eth0") + +# Display inputs for confirmation +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" +echo "NETWORK_INTERFACE: $NETWORK_INTERFACE" +read -p "Is this correct? [yes/no]: " confirmation + +if [[ $confirmation != [Yy]* ]]; then + echo "Exiting without changes." + exit +fi + +# Split ADD_IP_ADDRESSES into an array +IFS=',' read -ra ADDR <<<"$ADD_IP_ADDRESSES" + +# Initialize the interfaces file content +interfaces_content=" ### Hetzner Online GmbH installimage source /etc/network/interfaces.d/* @@ -16,63 +59,47 @@ auto lo iface lo inet loopback iface lo inet6 loopback - iface ${NETWORK_INTERFACE} inet manual - - up ip route add -net up ip route add -net ${GATEWAYADRESS} netmask ${NETMASK} gw ${GATEWAYADRESS} vmbr0 + up ip route add -net 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 - up ip route add 192.168.0.0/16 via ${ADD_IP_ADRESS} dev vmbr0 - up ip route add 172.16.0.0/12 via ${ADD_IP_ADRESS} dev vmbr0 - up ip route add 10.0.0.0/8 via ${ADD_IP_ADRESS} dev vmbr0 - + up ip route add 192.168.0.0/16 via ${ADDR[0]} dev vmbr0 + up ip route add 172.16.0.0/12 via ${ADDR[0]} dev vmbr0 + up ip route add 10.0.0.0/8 via ${ADDR[0]} dev vmbr0 iface ${NETWORK_INTERFACE} inet6 static address 2a01:4f8:110:5143::2 netmask 64 gateway fe80::1 - auto vmbr0 iface vmbr0 inet static address ${MAINSERVERIP} netmask 32 - gateway ${GATEWAYADRESS} + gateway ${GATEWAYADDRESS} broadcast ${BROADCASTIP} bridge-ports ${NETWORK_INTERFACE} bridge-stp off bridge-fd 0 - pointopoint ${GATEWAYADRESS} + pointopoint ${GATEWAYADDRESS} #WAN +" - -# Virtual switch for DMZ -# (connect your firewall/router KVM instance and private DMZ hosts here) -auto vmbr1 -iface vmbr1 inet manual - bridge_ports none - bridge_stp off - bridge_fd 0 -#LAN0 - -" >interfaces - -cat interfaces - -while true; do - read -p "Config correct? [yes][no]: " yn - case $yn in - [Yy]*) - echo "" - break - ;; - [Nn]*) exit ;; - *) echo "Please answer yes or no." ;; - esac +# Append bridge interfaces for each additional IP +for i in "${!ADDR[@]}"; do + interfaces_content+=$(create_bridge_text "${ADDR[i]}" "$((i + 1))") done -mv /etc/network/interfaces /etc/network/interfaces.old -mv interfaces /etc/network/interfaces +echo "$interfaces_content" >interfaces -echo "The network can be restarted with the following command: /etc/init.d/networking restart " +# Confirm before applying changes +read -p "Apply this network configuration? [yes/no]: " apply_conf + +if [[ $apply_conf == [Yy]* ]]; then + mv /etc/network/interfaces /etc/network/interfaces.old + mv interfaces /etc/network/interfaces + echo "The network can be restarted with the following command: /etc/init.d/networking restart" +else + echo "Exiting without applying changes." +fi