chmod +x ip.sh && bash ip.sh

#!/bin/bash
# Function: Set network configuration
show_menu() {
    echo "Choose an option:"
    echo "1. Print current IP info"
    echo "2. Set DHCP"
    echo "3. Set static IP"
    echo "4. Change hostname"
    echo "5. Restart network services"
    echo "6. Exit"
}

print_current_ip_info() {
    echo "Current IP configuration:"
    ip addr show
}

set_dhcp() {
    local interface=$(ip -o link | awk '{print $2,$9}' | grep -v "lo" | awk '{print substr($1, 1, length($1)-1)}')
    echo "Setting DHCP for interface $interface..."
    if [[ -e /etc/debian_version ]]; then
        echo "iface $interface inet dhcp" > /etc/network/interfaces
        /etc/init.d/networking restart
    elif [[ -e /etc/centos-release ]]; then
        echo "BOOTPROTO=dhcp" > /etc/sysconfig/network-scripts/ifcfg-$interface
        systemctl restart network
    else
        echo "Unsupported system type."
    fi
}

get_current_config() {
    local interface=$(ip -o link | awk '{print $2,$9}' | grep -v "lo" | awk '{print substr($1, 1, length($1)-1)}')
    local ip_address=$(ip addr show $interface | grep "inet " | awk '{print $2}' | cut -d/ -f1)
    local gateway=$(ip route | grep default | grep $interface | awk '{print $3}')
    local netmask=$(ip addr show $interface | grep "inet " | awk '{print $2}' | cut -d/ -f2)
    local dns1=$(grep "nameserver" /etc/resolv.conf | awk '{print $2}' | sed -n '1p')
    local dns2=$(grep "nameserver" /etc/resolv.conf | awk '{print $2}' | sed -n '2p')
    echo $ip_address $gateway $netmask $dns1 $dns2
}

configure_static_ip() {
    read_current=$(get_current_config)
    IFS=' ' read -ra ADDR <<< "$read_current"
    current_ip=${ADDR[0]}
    current_gateway=${ADDR[1]}
    current_netmask=${ADDR[2]}
    current_dns1=${ADDR[3]}
    current_dns2=${ADDR[4]}

    echo "Please enter new settings (press enter to use current settings):"
    read -e -p "Enter IP address (current IP is $current_ip): " ip_address
    ip_address=${ip_address:-$current_ip}  # Use current setting if input is empty

    read -e -p "Enter gateway (current gateway is $current_gateway): " gateway
    gateway=${gateway:-$current_gateway}  # Use current setting if input is empty

    read -e -p "Enter subnet mask (current subnet mask is $current_netmask): " netmask
    netmask=${netmask:-$current_netmask}  # Use current setting if input is empty

    read -e -p "Enter DNS server 1 (current DNS1 is $current_dns1): " dns1
    dns1=${dns1:-$current_dns1}  # Use current setting if input is empty

    read -e -p "Enter DNS server 2 (current DNS2 is $current_dns2): " dns2
    dns2=${dns2:-$current_dns2}  # Use current setting if input is empty

    configure_network "$ip_address" "$gateway" "$netmask" "$dns1" "$dns2"
}


configure_network() {
    local ip_address=$1
    local gateway=$2
    local netmask=$3
    local dns1=$4
    local dns2=$5

    local interface=$(ip -o link | awk '{print $2,$9}' | grep -v "lo" | awk '{print substr($1, 1, length($1)-1)}')
    local interface_file="/etc/network/interfaces"
    local cfg_file="/etc/sysconfig/network-scripts/ifcfg-$interface"

    echo "Configuring network for static IP on interface $interface..."

    if [[ -e /etc/debian_version ]]; then
        echo "auto $interface" > $interface_file
        echo "iface $interface inet static" >> $interface_file
        echo "address $ip_address" >> $interface_file
        echo "netmask $netmask" >> $interface_file
        echo "gateway $gateway" >> $interface_file
        echo "dns-nameservers $dns1 $dns2" >> $interface_file
        /etc/init.d/networking restart
    elif [[ -e /etc/centos-release ]]; then
        echo "DEVICE=$interface" > $cfg_file
        echo "BOOTPROTO=static" >> $cfg_file
        echo "ONBOOT=yes" >> $cfg_file
        echo "IPADDR=$ip_address" >> $cfg_file
        echo "NETMASK=$netmask" >> $cfg_file
        echo "GATEWAY=$gateway" >> $cfg_file
        echo "DNS1=$dns1" >> $cfg_file
        echo "DNS2=$dns2" >> $cfg_file
        systemctl restart network
    else
        echo "Unsupported system type."
        exit 1
    fi
}

configure_hostname() {
    read -e -p "Enter hostname: " hostname
    echo "Setting hostname to $hostname"
    hostnamectl set-hostname $hostname
}

restart_network_services() {
    if [[ -e /etc/debian_version ]]; then
        /etc/init.d/networking restart
    elif [[ -e /etc/centos-release ]]; then
        systemctl restart network
    else
        echo "Unsupported system type."
        exit 1
    fi
}

# Main program starts here
echo "Starting network configuration script..."

# Require admin privileges
if [ "$(id -u)" != "0" ]; then
    echo "This script requires administrator privileges." 1>&2
    exit 1
fi

while true; do
    show_menu
    read -e -p "Enter your choice: " choice
    case $choice in
        1) print_current_ip_info ;;
        2) set_dhcp ;;
        3) configure_static_ip ;;
        4) configure_hostname ;;
        5) restart_network_services ;;
        6) echo "Exiting..."; break ;;
        *) echo "Invalid option. Please try again." ;;
    esac
done

echo "Configuration complete! A restart may be required to apply changes."