YJWANG

Bash-Gen Bond (본딩 ifcfg파일 자동 생성) 본문

90.Programming

Bash-Gen Bond (본딩 ifcfg파일 자동 생성)

왕영주 2020. 8. 27. 23:15

사용 법

# bash gen_bond.sh eth0 eth1 bond0 192.168.10.10 255.255.255.0

script 내용

ip입력하는 부분엔 정규식으로 정합성 체크를 진행한다. Bonding mode는 default로 1이므로 상황에 맞게 수정하여 진행한다.
스크립트 초반에 read로 mode를 받아 실행하는 것도 방법일 듯 하다.

#! /bin/bash

errMsg="
./gen_bond.sh <slave1> <slave2> <bond_name> [<bond_IP> <bond_netmask> [<bond GW>]]

# Examples
./gen_bond.sh eth1 eth2 bond0
./gen_bond.sh eth1 eth2 bond0 192.168.0.33 255.255.255.0
./gen_bond.sh eth1 eth2 bond0 192.168.0.33 255.255.255.0 192.168.0.1
"

# check invalid argument
if [ $# -ne 6 ] && [ $# -ne 5 ] && [ $# -ne 3 ] ; then
    echo "$errMsg"
    exit 1
fi

# check vaild interface
links=$(echo /sys/class/net/* |sed "s/\/sys\/class\/net\///g")

for i in $1 $2; do
    for link in $links; do
        if [ "$link" = "$i" ]; then
            break
        else
            link=""
        fi
    done

    if [ "$link" = "" ]; then
        echo -e "interface \"$i\" does not exist \n"
        echo "available interface lists"
        echo "========================="
        echo "$links"
        echo "========================="
        exit 1
    fi
done

# make backup directory if it's not exist
if [ ! -d "/etc/sysconfig/network-scripts/bak" ]; then
    mkdir "/etc/sysconfig/network-scripts/bak"
fi

# backup ifcfg iles if it's already exist
for i in $1 $2 $3;do
    if [ -e "/etc/sysconfig/network-scripts/ifcfg-$i" ]; then
        cp "/etc/sysconfig/network-scripts/ifcfg-$i" "/etc/sysconfig/network-scripts/bak/ifcfg-$i-$(date +"%Y%m%d-%H%M%S")"
    fi
done

# make slave-1 interface configuration
cat << EOF > "/etc/sysconfig/network-scripts/ifcfg-$1"
TYPE=Ethernet
BOOTPROTO=none
NAME=$1
DEVICE=$1
ONBOOT=yes
MASTER=$3
SLAVE=yes
EOF

# make slave-2 interface configuration
cat << EOF > "/etc/sysconfig/network-scripts/ifcfg-$2"
TYPE=Ethernet
BOOTPROTO=none
NAME=$2
DEVICE=$2
ONBOOT=yes
MASTER=$3
SLAVE=yes
EOF

# make master interface configuration
cat << EOF > "/etc/sysconfig/network-scripts/ifcfg-$3"
TYPE=Bond
BOOTPROTO=none
NAME=$3
DEVICE=$3
ONBOOT=yes
BONDING_OPTS="mode=1 miimon=100"
EOF

# check vaild ip address and input ip address in master interface
ipv4RegEx="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
subReEx="^(((255\.){3}(255|254|252|248|240|224|192|128|0+))|((255\.){2}(255|254|252|248|240|224|192|128|0+)\.0)|((255\.)(255|254|252|248|240|224|192|128|0+)(\.0+){2})|((255|254|252|248|240|224|192|128|0+)(\.0+){3}))$"

if [ "$4" != "" ] && [ "$5" != "" ]; then
    if [[ $4 =~ $ipv4RegEx ]]; then
        echo "IPADDR=$4" >> "/etc/sysconfig/network-scripts/ifcfg-$3"
    else
        echo "\"$4\" is not valid IP"
        exit 1
    fi

    if [[ $5 =~ $subReEx ]]; then
            echo "NETMASK=$5" >> "/etc/sysconfig/network-scripts/ifcfg-$3"
    else
        echo "\"$5\" is not valid subnet"
        exit 1
    fi
fi

# set master interface gateway
if [ "$6" != "" ]; then
        if [[ $6 =~ $ipv4RegEx ]]; then
        echo "GATEWAY=$6" >> "/etc/sysconfig/network-scripts/ifcfg-$3"
        else
                echo "\"$6\" is not valid IP"
                exit 1
        fi
fi
반응형