#!/bin/sh
echo_red(){
  echo -e "\033[31;5;1m${1}\033[0m"
}
echo_yellow(){
  echo -e "\033[33;5;1m${1}\033[0m"
}
echo_blue(){
  echo -e "\033[34;5;1m${1}\033[0m"
}
echo_green(){
  echo -e "\033[32;5;1m${1}\033[0m"
}


read -p "Please input ip for eth0, like 192.168.0.x: " v_ip_0
read -p "Please input ip for eth0:1, like 192.168.0.(x+200): " v_ip_1

v_eth1_detect=$(ls /etc/sysconfig/network-scripts/ifcfg-eth1)
if [  "$v_eth1_detect" == "/etc/sysconfig/network-scripts/ifcfg-eth1" ]
then
  read -p "Please input ip for eth1, like 172.31.118.x: " v_ip_2
else
  echo_red "please configure eth1 for your rac!!!"
  sleep 2
fi

rm -rf /tmp/0.tmp
rm -rf /tmp/1.tmp
rm -rf /tmp/2.tmp

####for eth0
##################
v_length_0=$(echo $v_ip_0 | tr -d [:blank:] | wc -c)
v_length_0_origin=$(echo $v_ip_0 | wc -c )
if [ ! $v_length_0 -eq $v_length_0_origin ]
then
   echo_red "you input one or more blank for eth0, exit force!!!"
   exit
fi

let 'v_length_0 -= 1'
for i in $(seq 1 $v_length_0)
do
 v_digit_0=$(echo $v_ip_0 | cut -c $i-$i)
case $v_digit_0 in
.|0|1|2|3|4|5|6|7|8|9)
echo 1 >> /tmp/0.tmp
;;
*)
echo 0 >> /tmp/0.tmp
;;
esac
done

if grep -q 0 /tmp/0.tmp
then
 echo_red "please input number for eth0, exit force!!!"
 rm -rf /tmp/0.tmp
 exit
fi

 

####for eth0:1
####################
v_length_1=$(echo $v_ip_1 | tr -d [:blank:] | wc -c)
v_length_1_origin=$(echo $v_ip_1 | wc -c )
if [ ! $v_length_1 -eq $v_length_1_origin ]
then
   echo_red "you input one or more blank for eth0:1 ,force quit"
   exit
fi


let 'v_length_1 -= 1'
for i in $(seq 1 $v_length_1)
do
 v_digit_1=$(echo $v_ip_1 | cut -c $i-$i)
case $v_digit_1 in
.|0|1|2|3|4|5|6|7|8|9)
echo 1 >> /tmp/1.tmp
;;
*)
echo 0 >> /tmp/1.tmp
;;
esac
done

if grep -q 0 /tmp/1.tmp
then
 echo_red "please input number for eth0:1, exit force!!!"
 rm -rf /tmp/1.tmp
 exit
fi

 

####for eth1
#################
v_length_2=$(echo $v_ip_2 | tr -d [:blank:] | wc -c)
v_length_2_origin=$(echo $v_ip_2 | wc -c )
if [ ! $v_length_2 -eq $v_length_2_origin ]
then
   echo_red "you input one or more blank for eth1, exit force!!!"
   exit
fi

let 'v_length_2 -= 1'
for i in $(seq 1 $v_length_2)
do
 v_digit_2=$(echo $v_ip_2 | cut -c $i-$i)
case $v_digit_2 in
.|0|1|2|3|4|5|6|7|8|9)
echo 1 >> /tmp/2.tmp
;;
*)
echo 0 >> /tmp/2.tmp
;;
esac
done

if grep -q 0 /tmp/2.tmp
then
 echo_red "please input number for eth1,exit force!!!"
 rm -rf /tmp/2.tmp
 exit
fi

 

 

cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
NETMASK=255.255.255.0
IPADDR=$v_ip_0
GATEWAY=$v_ip_0
TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes
EOF

cat > /etc/sysconfig/network-scripts/ifcfg-eth0:1 << EOF
DEVICE=eth0:1
BOOTPROTO=none
ONBOOT=yes
NETMASK=255.255.255.0
IPADDR=$v_ip_1
GATEWAY=$v_ip_1
TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes
EOF

if [  "$v_eth1_detect" == "/etc/sysconfig/network-scripts/ifcfg-eth1" ]
then
cat > /etc/sysconfig/network-scripts/ifcfg-eth1 << EOF
DEVICE=eth1
BOOTPROTO=none
ONBOOT=yes
NETMASK=255.255.255.0
IPADDR=$v_ip_2
GATEWAY=$v_ip_2
TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes
EOF
else
  echo_red "skip the eth1"
  sleep 2
fi
service network restart
chkconfig network on
ifconfig