#!/bin/bash
# Variables Exec = Add or delete secondary IP, pass 'add' or 'del'
# Variables SecIPS = Secondary IP list, pass "x.x.x.x x.x.x.x ..."
# Variables SecMask = Secondary IP netmask, pass "255.255.255.0"
# set date 2023/12/11 
# Last test date 2023/12/29
Exec=$1
SecIPs=$2
SecMask=$3
if [ "${Exec}" == "add" ];then
if [ "$#" -lt 3 ];then
echo "Syntax error: The parameters passed are abnormal.
Please pass three position variables.
    \$1 == Add or delete secondary IP, pass 'add' or 'del'
    \$2 == Secondary IP list, pass \"192.168.1.3 192.168.1.4 ...\"
    \$3 == Secondary IP netmask, pass \"255.255.255.0\"
Excmple:
    sh $0 add \"192.168.1.3 192.168.1.4\" \"255.255.255.0\""; exit 2
fi
elif [ "${Exec}" == "del" ];then
if [ "$#" -lt 2 ];then
echo "Syntax error: The parameters passed are abnormal.
Please pass three position variables.
    \$1 == Add or delete secondary IP, pass 'add' or 'del'
    \$2 == Secondary IP list, pass \"192.168.1.3 192.168.1.4 ...\"
Excmple:
    sh $0 del \"192.168.1.3 192.168.1.4\" " ; exit 2
fi
else
  echo "Unknow Operator? Please pass 'add' or 'del'";exit 1
fi

CentOS() {
# del Secondary IP
if [[ $1 == "del" ]];then
for ip in ${SecIPs}; do
    ip addr del $(ip a | awk /${ip}\\//'{print $2}') dev $(ip a | awk /${ip}\\//'{print $NF}')
    rm -f $(grep -lr "${ip}" /etc/sysconfig/network-scripts/)
done; return 0
fi
# add Secondary IP
echo $1
add_or_no
nic_name=$(ip -o -4 route show to default | awk '{print $5}')
nic_dir="/etc/sysconfig/network-scripts/"
for ip in ${SecIPs}; do
ip_name=$(echo ${ip} | awk -F . '{print $3$4}')
cat <<EOF > "${nic_dir}/ifcfg-${nic_name}:${ip_name}"
DEVICE=${nic_name}:${ip_name}
Type=Ethernet
BOOTPROTO=none
NM_CONTROLLED=no
ONBOOT=yes
IPADDR=${ip}
NETMASK=${SecMask}
EOF
ifup ${nic_name}:${ip_name}
done
}
Debian() {
# del Secondary IP
if [ "$1" = "del" ];then
for ip in ${SecIPs}; do
    find /etc/network/interfaces.d/ -type f -exec grep -l "${ip}" {} \; \
    | sed 's/\.cfg$//' | xargs -I {} sh -c 'interface_name=$(basename {} | cut -d- -f2-);ifdown $interface_name;rm -f {}.cfg'
done; return 0
fi
# add Secondary IP
add_or_no
nic_name=$(ip -o -4 route show to default | awk '{print $5}')
nic_dir="/etc/network/interfaces.d"
file_count=$(ls "$nic_dir"/"$nic_name"* 2>/dev/null | wc -l)
if [ ${file_count} -eq 0 ]; then
cat <<AOF > /etc/network/interfaces.d/${nic_name}.cfg
auto ${nic_name}
iface ${nic_name} inet dhcp
AOF
sed -i "/^auto ${nic_name}/d; /^iface ${nic_name}/d" /etc/network/interfaces
ifdown ${nic_name} && ifup ${nic_name}
fi

for ip in ${SecIPs}; do
ip_name=$(echo ${ip} | awk -F . '{print $3$4}')
cat <<EOF > "${nic_dir}/${nic_name}:${ip_name}.cfg"
auto ${nic_name}:${ip_name}
iface ${nic_name}:${ip_name} inet static
    address ${ip}
    netmask ${SecMask}
EOF
ifup "${nic_name}:${ip_name}"
done
}
Ubuntu16() {
# del Secondary IP
if [ "$1" = "del" ];then
for ip in ${SecIPs}; do
    find /etc/network/interfaces.d/ -type f -exec grep -l "${ip}" {} \; \
    | sed 's/\.cfg$//' | xargs -I {} sh -c 'interface_name=$(basename {} | cut -d- -f2-);ifdown $interface_name;rm -f {}.cfg'
done; return 0
fi
# add Secondary IP
add_or_no
nic_name=$(ip -o -4 route show to default | awk '{print $5}')
nic_dir="/etc/network/interfaces.d"
file_count=$(ls "$nic_dir"/"$nic_name"* 2>/dev/null | wc -l)
if [ ${file_count} -eq 0 ]; then
cat <<AOF > /etc/network/interfaces.d/${nic_name}.cfg
auto ${nic_name}
iface ${nic_name} inet dhcp
AOF
sed -i "/^auto ${nic_name}/d; /^iface ${nic_name}/d" /etc/network/interfaces
ifdown ${nic_name} && ifup ${nic_name}
fi

for ip in ${SecIPs}; do
ip_name=$(echo ${ip} | awk -F . '{print $3$4}')
cat <<EOF > "${nic_dir}/${nic_name}:${ip_name}.cfg"
auto ${nic_name}:${ip_name}
iface ${nic_name}:${ip_name} inet static
    address ${ip}
    netmask ${SecMask}
EOF
ifup "${nic_name}:${ip_name}"
done
}
Ubuntu() {
# del Secondary IP
if [ "$1" = "del" ];then
for ip in ${SecIPs}; do
    rm -f $(grep -li "${ip}/" /etc/netplan/*) && netplan apply
done; return 0
fi
# add Secondary IP
add_or_no
GATEWAY=$(ip route list | awk '/default/{print $3}')
FIRST_IP=$(ip a |grep `ip route |awk '/proto kernel/{print $NF}'`/ |awk '{print $2}')
NIC_NAME=$(ip route list | awk '/default/{print $5}')
SecIP_Prefix=${FIRST_IP##*/}
###
#run_code=$(grep -rl "dhcp4: true" /etc/netplan)
#if [ -n "$run_code" ];then
#FIRST_NIC_CFG=$(grep -rl "dhcp4: true" /etc/netplan)
FIRST_NIC_CFG=$(grep -rl "${NIC_NAME}" /etc/netplan)
DEC_SecIP="$(grep -rl ${FIRST_IP} ${FIRST_NIC_CFG})"
if [ -z ${DEC_SecIP} ];then
mkdir -p /var/lib/NetworkManager/
DataServer=$(grep 'SERVER_ADDRESS' /run/systemd/netif/leases/* | head -n 1 | awk -F = '{print $NF}')
cat <<EOF > /var/lib/NetworkManager/aorta.lease
# Created by aorta-assist on instance boot automatically, do not edit
#
option dhcp-server-identifier ${DataServer};
EOF

cat <<EOF > ${FIRST_NIC_CFG}
# Created by aorta-assist on instance boot automatically, do not edit.
#
network:
  ethernets:
    ${NIC_NAME}:
      dhcp4: false
      addresses:
        - ${FIRST_IP}
      gateway4: ${GATEWAY}
      nameservers:
        addresses:
          - 8.8.8.8
  version: 2
EOF
fi
for SecIP in ${SecIPs}; do
ip_name=$(echo ${SecIP} | awk -F . '{print $3$4}')
cat <<EOF > /etc/netplan/${ip_name}-secondary.yaml
# Created by aorta-assist on instance boot automatically, do not edit.
#
network:
  ethernets:
    ${NIC_NAME}:
      dhcp4: false
      addresses:
        - ${SecIP}/${SecIP_Prefix}
  version: 2
EOF
done; netplan apply
}
OpenSUSE() {
# del Secondary IP
if [[ $1 == "del" ]];then
for ip in ${SecIPs}; do
    nic_name=$(ip -o -4 route show to default | awk '{print $5}')
    sed -i "/$ip/,/NETMASK/d" /etc/sysconfig/network/ifcfg-${nic_name}
    ifdown ${nic_name} && ifup ${nic_name}
done
fi
# add Secondary IP
add_or_no
nic_name=$(ip -o -4 route show to default | awk '{print $5}')
nic_dir="/etc/sysconfig/network"
for ip in ${SecIPs}; do
    ip_name=$(echo ${ip} | awk -F . '{print $3$4}')
    echo "IPADDR_${ip_name}=${ip}" >> $nic_dir/ifcfg-${nic_name}
    echo "NETMASK_${ip_name}=${SecMask}" >> $nic_dir/ifcfg-${nic_name}
done
ifdown ${nic_name} && ifup ${nic_name}
}
add_or_no() {
echo "======== $# ========="
}
# CentOS
if grep -qi "centos" /etc/os-release; then
    CentOS ${Exec}
fi
# Debian
if grep -qi "ID=debian" /etc/os-release; then
    Debian ${Exec}
fi
# Ubuntu
if grep -qi "ubuntu" /etc/os-release; then
    if grep -qi "16.04" /etc/os-release; then
        Ubuntu16 ${Exec}
    else
        Ubuntu ${Exec}
    fi
fi
# OpenSUSE
if grep -qi "opensuse" /etc/os-release; then
    OpenSUSE ${Exec}
fi
# Kill Self
rm -f $0
