#!/bin/bash

# Variables for the new interface configuration
INTERFACE="eth1"                   # New interface name (adjust to your needs)
STATIC_IP="192.168.10.10/24"       # Static IP for the new interface
GATEWAY="192.168.10.1"             # Gateway for the new interface
DNS_SERVERS="8.8.8.8 1.1.1.1"      # DNS servers

# Backup the current /etc/network/interfaces file before modifying it
echo "Backing up the current /etc/network/interfaces file..."
sudo cp /etc/network/interfaces /etc/network/interfaces.bak

# Adding the new interface configuration to /etc/network/interfaces
echo "Injecting new interface configuration into /etc/network/interfaces..."

# Check if the interface already exists in the config
if grep -q "iface $INTERFACE" /etc/network/interfaces; then
    echo "Interface $INTERFACE already exists in /etc/network/interfaces. Exiting."
    exit 1
fi

# Add the static IP configuration for the new interface
echo -e "\n# Configuration for $INTERFACE" | sudo tee -a /etc/network/interfaces
echo "iface $INTERFACE inet static" | sudo tee -a /etc/network/interfaces
echo "    address $STATIC_IP" | sudo tee -a /etc/network/interfaces
echo "    gateway $GATEWAY" | sudo tee -a /etc/network/interfaces
echo "    dns-nameservers $DNS_SERVERS" | sudo tee -a /etc/network/interfaces

# Restart networking service to apply changes
echo "Restarting networking service..."
sudo systemctl restart networking

# Confirm the network interface settings
echo "Network configuration for $INTERFACE:"
cat /etc/network/interfaces | grep -A 6 "$INTERFACE"

# Test connectivity
echo "Testing network connectivity on $INTERFACE..."
ping -c 4 8.8.8.8
ping -c 4 google.com

echo "Network setup for $INTERFACE completed."
